improve chatview

This commit is contained in:
mrbesen 2023-11-21 19:46:57 +01:00
parent 80f1df6c6d
commit dc94dbcb23
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 34 additions and 5 deletions

View File

@ -1,9 +1,13 @@
#pragma once
#include <string>
class TgTUI;
class View {
public:
static std::string FormatTime(time_t t);
View(TgTUI& tgtui);
virtual ~View();

View File

@ -1,5 +1,13 @@
#include "view.h"
#include <ctime>
std::string View::FormatTime(time_t t) {
char timeBuf[20];
std::size_t len = std::strftime(timeBuf, 20, "%Y-%m-%d %H:%M:%S", std::localtime(&t));
return std::string(timeBuf, len);
}
View::View(TgTUI& tgtui) : tgtui(tgtui) {}
View::~View() {}

View File

@ -22,20 +22,37 @@ int64_t ViewChat::getChat() const {
}
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(int row = 0; row < maxRows-1 && row < messages.size(); ++row) {
Log::debug << "print msg at: " << row;
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;
::mvprintw(maxRows - row, 0, "[%lu] %c (%li) %*s\n", msg->sendDate, direction, msg->sender, std::min<int>(msg->text.size(), maxCols), msg->text.c_str());
// 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());
}
}