diff --git a/inc/view.h b/inc/view.h index 9a490c7..b37f132 100644 --- a/inc/view.h +++ b/inc/view.h @@ -1,9 +1,13 @@ #pragma once +#include + class TgTUI; class View { public: + static std::string FormatTime(time_t t); + View(TgTUI& tgtui); virtual ~View(); diff --git a/src/view.cpp b/src/view.cpp index 18293f9..6e3d8f3 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -1,5 +1,13 @@ #include "view.h" +#include + +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() {} diff --git a/src/viewchat.cpp b/src/viewchat.cpp index 36758b7..d59cd12 100644 --- a/src/viewchat.cpp +++ b/src/viewchat.cpp @@ -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 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(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(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()); } }