chat view in cdk implemented
This commit is contained in:
parent
dafaacafc4
commit
7eff50e975
|
@ -6,6 +6,7 @@
|
||||||
#include "tgclient.h"
|
#include "tgclient.h"
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
|
|
||||||
|
#include <cdk/cdk.h>
|
||||||
|
|
||||||
class TgTUI {
|
class TgTUI {
|
||||||
public:
|
public:
|
||||||
|
@ -32,6 +33,9 @@ private:
|
||||||
std::vector<char*> chatsItemList;
|
std::vector<char*> chatsItemList;
|
||||||
std::vector<std::shared_ptr<Message>> messages;
|
std::vector<std::shared_ptr<Message>> messages;
|
||||||
|
|
||||||
|
CDKSCROLL* chatsScroll = nullptr;
|
||||||
|
CDKSCROLL* chatScroll = nullptr;
|
||||||
|
|
||||||
bool shouldRun = false;
|
bool shouldRun = false;
|
||||||
std::thread tuiThread;
|
std::thread tuiThread;
|
||||||
};
|
};
|
||||||
|
|
111
src/tgtui.cpp
111
src/tgtui.cpp
|
@ -20,6 +20,30 @@ static std::string FormatTime(time_t t) {
|
||||||
return std::string(timeBuf, len);
|
return std::string(timeBuf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::vector<char*> convertToList(const std::vector<T>& v, std::function<std::string(const T&)> f, bool reverse = false) {
|
||||||
|
std::vector<char*> out;
|
||||||
|
out.resize(v.size(), nullptr);
|
||||||
|
for(size_t i = 0; i < v.size(); ++i) {
|
||||||
|
const T& t = v.at(i);
|
||||||
|
const std::string value = f(t);
|
||||||
|
|
||||||
|
char* item = new char[value.size()+1];
|
||||||
|
std::memcpy(item, value.c_str(), value.size() +1);
|
||||||
|
|
||||||
|
size_t index = (reverse ? (v.size() - i) -1 : i);
|
||||||
|
out.at(index) = item;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clearVec(std::vector<char*> v) {
|
||||||
|
for(char* c : v) {
|
||||||
|
delete[] c;
|
||||||
|
}
|
||||||
|
v.clear();
|
||||||
|
}
|
||||||
|
|
||||||
TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) {}
|
TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) {}
|
||||||
|
|
||||||
TgTUI::~TgTUI() {
|
TgTUI::~TgTUI() {
|
||||||
|
@ -86,41 +110,46 @@ void TgTUI::threadLoop() {
|
||||||
CDKSCREEN* cdkScr = ::initCDKScreen(cursesWin);
|
CDKSCREEN* cdkScr = ::initCDKScreen(cursesWin);
|
||||||
|
|
||||||
// build scroll
|
// build scroll
|
||||||
chatsItemList.clear();
|
{
|
||||||
chatsItemList.reserve(chats.size());
|
chatsItemList.reserve(chats.size());
|
||||||
for(const SlimChat& chat : chats) {
|
|
||||||
std::string name = chat.name;
|
|
||||||
if(name.empty()) {
|
|
||||||
name = std::to_string(chat.chatId);
|
|
||||||
}
|
|
||||||
|
|
||||||
char* item = new char[name.size() +3];
|
std::vector<char*> chatsItemList = convertToList<SlimChat>(chats, [](const SlimChat& chat){
|
||||||
item[0] = item[1] = ' ';
|
std::string name = chat.name;
|
||||||
std::memcpy(item+2, name.c_str(), name.size() +1);
|
if(name.empty()) {
|
||||||
chatsItemList.push_back(item);
|
name = std::to_string(chat.chatId);
|
||||||
|
}
|
||||||
|
|
||||||
|
name.insert(0, " ");
|
||||||
|
return name;
|
||||||
|
});
|
||||||
|
|
||||||
|
chatsScroll = newCDKScroll(cdkScr, LEFT, TOP, RIGHT, 0, 40, "Chats:", chatsItemList.data(), chatsItemList.size(), FALSE, A_REVERSE, TRUE, FALSE);
|
||||||
|
chatScroll = newCDKScroll(cdkScr, 41, TOP, RIGHT, 0, -41, "Chat:", nullptr, 0, FALSE, A_REVERSE, TRUE, FALSE);
|
||||||
|
clearVec(chatsItemList);
|
||||||
}
|
}
|
||||||
|
|
||||||
CDKSCROLL* scroll = newCDKScroll(cdkScr, LEFT, TOP, RIGHT, 0, 40, "Chats:", chatsItemList.data(), chatsItemList.size(), FALSE, A_REVERSE, TRUE, FALSE);
|
|
||||||
|
|
||||||
while(shouldRun) {
|
while(shouldRun) {
|
||||||
activateCDKScroll(scroll, nullptr);
|
activateCDKScroll(chatsScroll, nullptr);
|
||||||
|
|
||||||
if (scroll->exitType == vESCAPE_HIT) {
|
if (chatsScroll->exitType == vESCAPE_HIT) {
|
||||||
shouldRun = false;
|
shouldRun = false;
|
||||||
} else if (scroll->exitType == vNORMAL) {
|
} else if (chatsScroll->exitType == vNORMAL) {
|
||||||
// build / activate chat view
|
// build / activate chat view
|
||||||
int itemIndex = getCDKScrollCurrentItem(scroll);
|
int itemIndex = getCDKScrollCurrentItem(chatsScroll);
|
||||||
int64_t chatId = chats.at(itemIndex).chatId;
|
const SlimChat& chat = chats.at(itemIndex);
|
||||||
(void) chatId;
|
tgclient.openChat(chat.chatId);
|
||||||
|
tgclient.getLastMessages(chat.chatId, std::bind(&TgTUI::handleChatMessages, this, pl::_1));
|
||||||
|
|
||||||
|
activateCDKScroll(chatScroll, nullptr);
|
||||||
|
|
||||||
|
tgclient.closeChat(chat.chatId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyCDKScroll(scroll);
|
destroyCDKScroll(chatsScroll);
|
||||||
|
destroyCDKScroll(chatScroll);
|
||||||
|
|
||||||
chats.clear();
|
chats.clear();
|
||||||
for(char* item : chatsItemList) {
|
|
||||||
delete[] item;
|
|
||||||
}
|
|
||||||
|
|
||||||
tgclient.stop();
|
tgclient.stop();
|
||||||
|
|
||||||
|
@ -136,24 +165,12 @@ void TgTUI::handleNewChat(objptr<td_api::chat> chat) {
|
||||||
|
|
||||||
void TgTUI::handleChatMessages(const std::vector<std::shared_ptr<Message>>& msgs) {
|
void TgTUI::handleChatMessages(const std::vector<std::shared_ptr<Message>>& msgs) {
|
||||||
messages = msgs;
|
messages = msgs;
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Log::debug << "new messages: " << msgs.size();
|
||||||
|
|
||||||
/*
|
std::vector<char*> listEntrys = convertToList<std::shared_ptr<Message>>(msgs, [](const std::shared_ptr<Message>& msg) {
|
||||||
ViewMode ViewChat::paint() {
|
|
||||||
static const size_t FormatLen = 45;
|
|
||||||
|
|
||||||
getmaxyx(stdscr, maxRows, maxCols);
|
|
||||||
|
|
||||||
::printw("Chat: %s (%li)\n", chat->name.c_str(), chat->chatId);
|
|
||||||
|
|
||||||
Log::info << "messages: " << messages.size() << " maxRows: " << maxRows;
|
|
||||||
|
|
||||||
for(uint32_t row = 0; row < maxRows-1 && row < messages.size(); ++row) {
|
|
||||||
std::shared_ptr<Message> msg = messages.at(row);
|
|
||||||
const char direction = ( msg->isOutgoing ? '>' : '<' );
|
const char direction = ( msg->isOutgoing ? '>' : '<' );
|
||||||
const std::string timeStr = FormatTime(msg->sendDate);
|
const std::string timeStr = "[" + FormatTime(msg->sendDate) + "] ";
|
||||||
std::string msgText = msg->text;
|
std::string msgText = msg->text;
|
||||||
|
|
||||||
// find and remove first \n
|
// find and remove first \n
|
||||||
|
@ -163,18 +180,12 @@ ViewMode ViewChat::paint() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(MessageType::TEXT != msg->type) {
|
if(MessageType::TEXT != msg->type) {
|
||||||
msgText = '<' + convertMessageType(msg->type) + '>';
|
msgText = '<' + convertMessageType(msg->type) + "> " + msgText;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t colLimit = std::min<int>(msgText.size(), maxCols - (FormatLen + 3));
|
return timeStr + direction + " (" + std::to_string(msg->sender) + ") " + msgText;
|
||||||
if(msgText.size() > colLimit) {
|
}, true);
|
||||||
msgText.resize(colLimit);
|
setCDKScrollItems(chatScroll, listEntrys.data(), listEntrys.size(), FALSE);
|
||||||
msgText.append("...");
|
setCDKScrollPosition(chatScroll, listEntrys.size()-1);
|
||||||
}
|
clearVec(listEntrys);
|
||||||
|
|
||||||
::mvprintw(maxRows - row -1, 0, "[%s] %c (% 17li) %s", timeStr.c_str(), direction, msg->sender, msgText.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ViewMode::Same;
|
|
||||||
}
|
}
|
||||||
*/
|
|
Loading…
Reference in New Issue