TelegramTUI/src/viewchat.cpp

74 lines
1.6 KiB
C++

#include "viewchat.h"
#include <memory>
#include <ncurses.h>
#include <Log.h>
#include "message.h"
#include "tgtui.h"
ViewChat::ViewChat(TgTUI& tgtui) : View(tgtui) {}
ViewChat::~ViewChat() {}
void ViewChat::setChat(const SlimChat* chat) {
this->chat = chat;
}
int64_t ViewChat::getChat() const {
return this->chat->chatId;
}
void 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 std::string timeStr = FormatTime(msg->sendDate);
std::string msgText = msg->text;
// find and remove first \n
std::size_t nPos = msgText.find('\n');
if(nPos != std::string::npos) {
msgText.resize(nPos);
}
if(MessageType::TEXT != msg->type) {
msgText = '<' + convertMessageType(msg->type) + '>';
}
uint32_t colLimit = std::min<int>(msgText.size(), maxCols - (FormatLen + 3));
if(msgText.size() > colLimit) {
msgText.resize(colLimit);
msgText.append("...");
}
::mvprintw(maxRows - row -1, 0, "[%s] %c (% 17li) %s", timeStr.c_str(), direction, msg->sender, msgText.c_str());
}
}
int ViewChat::keyIn(int key) {
switch (key) {
case KEY_LEFT:
case 'q':
return 0;
}
return -1;
}
void ViewChat::updateMessages(const std::vector<std::shared_ptr<Message>>& msgs) {
// TODO: force a repaint somehow?
messages = msgs;
}