From dafaacafc47f5c8c1e834ab175930805c9b75b87 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Thu, 23 Nov 2023 20:25:00 +0100 Subject: [PATCH] use cdk --- Makefile | 2 +- inc/tgtui.h | 16 +----- inc/view.h | 28 ---------- inc/viewchat.h | 28 ---------- inc/viewchatlist.h | 26 --------- src/tgtui.cpp | 124 ++++++++++++++++++++++++++----------------- src/view.cpp | 21 -------- src/viewchat.cpp | 73 ------------------------- src/viewchatlist.cpp | 64 ---------------------- 9 files changed, 76 insertions(+), 306 deletions(-) delete mode 100644 inc/view.h delete mode 100644 inc/viewchat.h delete mode 100644 inc/viewchatlist.h delete mode 100644 src/view.cpp delete mode 100644 src/viewchat.cpp delete mode 100644 src/viewchatlist.cpp diff --git a/Makefile b/Makefile index c5520e9..5999bfc 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ TDLIBAF = $(TDLIBF)lib/ TDLIBA = $(TDLIBF)lib/libtdclient.a $(TDLIBF)lib/libtdcore.a $(TDLIBF)lib/libtdapi.a $(TDLIBF)lib/libtddb.a $(TDLIBF)lib/libtdactor.a $(TDLIBF)lib/libtdsqlite.a $(TDLIBF)lib/libtdnet.a $(TDLIBF)lib/libtdutils.a INCLUDES = -I$(LOGF) $(addprefix -I, $(INCFS)) -I$(TDLIBF)include -LDFLAGS = -pthread -lz -lssl -lcrypto -lncurses +LDFLAGS = -pthread -lz -lssl -lcrypto -lncurses -lcdk SRCFILES = $(shell find $(SRCF) -name "*.cpp") OBJFILES = $(patsubst $(SRCF)%, $(BUILDDIR)%, $(patsubst %.cpp, %.o, $(SRCFILES))) $(LOGO) diff --git a/inc/tgtui.h b/inc/tgtui.h index d1dec3e..b2839fb 100644 --- a/inc/tgtui.h +++ b/inc/tgtui.h @@ -6,9 +6,6 @@ #include "tgclient.h" #include "message.h" -class View; -class ViewChat; -class ViewChatList; class TgTUI { public: @@ -22,11 +19,6 @@ public: const std::vector& getChats(); private: - enum class ViewMode { - ChatList = 0, - Chat = 1, - }; - void initDoneCB(); void threadLoop(); @@ -34,16 +26,10 @@ private: void handleNewChat(objptr chat); void handleChatMessages(const std::vector>& msgs); - void switchToView(ViewMode vm); - TGClient tgclient; - ViewMode currentViewMode; - View* currentView = nullptr; - ViewChatList* viewChatList = nullptr; - ViewChat* viewChat = nullptr; - std::vector chats; + std::vector chatsItemList; std::vector> messages; bool shouldRun = false; diff --git a/inc/view.h b/inc/view.h deleted file mode 100644 index b37f132..0000000 --- a/inc/view.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include - -class TgTUI; - -class View { -public: - static std::string FormatTime(time_t t); - - View(TgTUI& tgtui); - virtual ~View(); - - virtual void open(); - virtual void close(); - - virtual void paint() = 0; - - // return -2 -> exit - // return -1 -> keep current ViewMode - // return 0 <= -> switch to other ViewMode - virtual int keyIn(int key); - -protected: - TgTUI& tgtui; - - int maxRows, maxCols; -}; diff --git a/inc/viewchat.h b/inc/viewchat.h deleted file mode 100644 index 000c333..0000000 --- a/inc/viewchat.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -#include "message.h" -#include "slimchat.h" -#include "view.h" - -class ViewChat : public View { -public: - ViewChat(TgTUI& tgtui); - virtual ~ViewChat(); - - void setChat(const SlimChat* chat); - int64_t getChat() const; - - virtual void paint() override; - - virtual int keyIn(int key) override; - - void updateMessages(const std::vector>& messages); - -private: - const SlimChat* chat = nullptr; - - std::vector> messages; -}; diff --git a/inc/viewchatlist.h b/inc/viewchatlist.h deleted file mode 100644 index cf93260..0000000 --- a/inc/viewchatlist.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include - -#include "slimchat.h" -#include "view.h" - -class ViewChatList : public View { -public: - ViewChatList(TgTUI& tgtui); - - virtual void open() override; - virtual void close() override; - - virtual void paint() override; - - virtual int keyIn(int key) override; - - int64_t getSelectedChatId(); - -private: - std::vector chats; - int32_t currentChatOffset = 0; - int32_t selectedChatRow = 0; -}; \ No newline at end of file diff --git a/src/tgtui.cpp b/src/tgtui.cpp index 6d07290..8fa2247 100644 --- a/src/tgtui.cpp +++ b/src/tgtui.cpp @@ -1,28 +1,29 @@ #include "tgtui.h" +#include +#include #include +#include #include #include +#include #include "config.h" -#include "view.h" -#include "viewchat.h" -#include "viewchatlist.h" namespace pl = std::placeholders; -TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) { - viewChatList = new ViewChatList(*this); - viewChat = new ViewChat(*this); +static std::string 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); } +TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) {} + TgTUI::~TgTUI() { stop(); - - delete viewChatList; - delete viewChat; } void TgTUI::run() { @@ -71,41 +72,55 @@ const std::vector& TgTUI::getChats() { void TgTUI::initDoneCB() { shouldRun = true; - currentView = viewChatList; - currentViewMode = ViewMode::ChatList; tuiThread = std::thread(&TgTUI::threadLoop, this); } void TgTUI::threadLoop() { // init ncurses - ::initscr(); + WINDOW* cursesWin = ::initscr(); // ::start_color(); ::cbreak(); // Disable line buffering ::keypad(stdscr, TRUE); // Enable special keys like arrow keys ::noecho(); // Don't print characters to the screen - currentView->open(); + CDKSCREEN* cdkScr = ::initCDKScreen(cursesWin); + + // build scroll + chatsItemList.clear(); + 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]; + item[0] = item[1] = ' '; + std::memcpy(item+2, name.c_str(), name.size() +1); + chatsItemList.push_back(item); + } + + CDKSCROLL* scroll = newCDKScroll(cdkScr, LEFT, TOP, RIGHT, 0, 40, "Chats:", chatsItemList.data(), chatsItemList.size(), FALSE, A_REVERSE, TRUE, FALSE); while(shouldRun) { - ::clear(); + activateCDKScroll(scroll, nullptr); - currentView->paint(); - - ::refresh(); - - int ch = getch(); - int newMode = currentView->keyIn(ch); - if(newMode == -2) { - // exit + if (scroll->exitType == vESCAPE_HIT) { shouldRun = false; - } else if(newMode == -1) { - continue; - } else { - switchToView((ViewMode) newMode); + } else if (scroll->exitType == vNORMAL) { + // build / activate chat view + int itemIndex = getCDKScrollCurrentItem(scroll); + int64_t chatId = chats.at(itemIndex).chatId; + (void) chatId; } } - currentView->close(); + destroyCDKScroll(scroll); + + chats.clear(); + for(char* item : chatsItemList) { + delete[] item; + } tgclient.stop(); @@ -113,7 +128,6 @@ void TgTUI::threadLoop() { ::nocbreak(); ::echo(); ::endwin(); - } void TgTUI::handleNewChat(objptr chat) { @@ -122,35 +136,45 @@ void TgTUI::handleNewChat(objptr chat) { void TgTUI::handleChatMessages(const std::vector>& msgs) { messages = msgs; - viewChat->updateMessages(msgs); + // TODO } -void TgTUI::switchToView(ViewMode to) { - // close old mode - if(currentViewMode == ViewMode::Chat) { - tgclient.closeChat(viewChat->getChat()); - } +/* +ViewMode ViewChat::paint() { + static const size_t FormatLen = 45; - // prepare new mode - if(to == ViewMode::Chat) { - int64_t chatid = viewChatList->getSelectedChatId(); - auto it = std::find_if(chats.begin(), chats.end(), [chatid](const SlimChat& sc){ - return sc.chatId == chatid; - }); + getmaxyx(stdscr, maxRows, maxCols); - if(it != chats.end()) { - viewChat->setChat(&*it); - tgclient.openChat(chatid); + ::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 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); } - tgclient.getLastMessages(chatid, std::bind(&TgTUI::handleChatMessages, this, pl::_1)); + 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()); } - switch(to) { - case ViewMode::ChatList: currentView = viewChatList; break; - case ViewMode::Chat: currentView = viewChat; break; - } - - currentViewMode = to; + return ViewMode::Same; } +*/ \ No newline at end of file diff --git a/src/view.cpp b/src/view.cpp deleted file mode 100644 index 6e3d8f3..0000000 --- a/src/view.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#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() {} - -void View::open() {} - -void View::close() {} - -int View::keyIn(int) { - return -1; -} diff --git a/src/viewchat.cpp b/src/viewchat.cpp deleted file mode 100644 index d59cd12..0000000 --- a/src/viewchat.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "viewchat.h" - -#include - -#include - -#include - -#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 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(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>& msgs) { - // TODO: force a repaint somehow? - - messages = msgs; -} diff --git a/src/viewchatlist.cpp b/src/viewchatlist.cpp deleted file mode 100644 index ece3d22..0000000 --- a/src/viewchatlist.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "viewchatlist.h" - -#include - -#include "tgtui.h" - -ViewChatList::ViewChatList(TgTUI& tgtui) : View(tgtui) {} - -void ViewChatList::open() { - chats = tgtui.getChats(); - currentChatOffset = 0; - selectedChatRow = 0; -} - -void ViewChatList::close() { - chats.clear(); -} - -void ViewChatList::paint() { - ::printw("Chats:\n"); - - getmaxyx(stdscr, maxRows, maxCols); - for(int row = 1; row < maxRows-1 && currentChatOffset + row < chats.size(); ++row) { - int selection = ' '; - const bool currentRowSelected = (row == selectedChatRow + currentChatOffset +1); - if(currentRowSelected) { - selection = '#'; - attron(A_REVERSE); - } - - ::mvprintw(row, 0, "%c %s\n", selection, chats.at(currentChatOffset + row -1).name.c_str()); - - if(currentRowSelected) { - attroff(A_REVERSE); - } - } -} - -int ViewChatList::keyIn(int key) { - switch (key) { - case KEY_UP: - if (selectedChatRow > 0) { - selectedChatRow--; - } - break; - case KEY_DOWN: - if (selectedChatRow < maxRows - 2) { - selectedChatRow++; - } - break; - case KEY_RIGHT: - case KEY_ENTER: - return 1; - case KEY_LEFT: - case 'q': - return -2; - } - - return -1; -} - -int64_t ViewChatList::getSelectedChatId() { - return chats.at(selectedChatRow + currentChatOffset).chatId; -}