diff --git a/.idea/.idea.tgcli/.idea/workspace.xml b/.idea/.idea.tgcli/.idea/workspace.xml index e1f83c2..4ee8035 100644 --- a/.idea/.idea.tgcli/.idea/workspace.xml +++ b/.idea/.idea.tgcli/.idea/workspace.xml @@ -1,7 +1,12 @@ - + + + + + + @@ -115,7 +121,7 @@ - + diff --git a/telegram/Command.cs b/telegram/Command.cs index c9872c6..aa6cfc7 100644 --- a/telegram/Command.cs +++ b/telegram/Command.cs @@ -11,182 +11,179 @@ namespace telegram { public static void ParseCommand(string command) { - var split = command.Split(" "); - if (split[0].Equals("open") || split[0].Equals("o")) + while (true) { - if (split.Length < 2) - return; - - var query = command.Substring(split[0].Length); - - var chatId = searchChatId(query); - if (chatId == 0) - return; - - currentChatId = 0; - currentChatUserId = 0; - currentUserOnline = false; - currentUserTyping = false; - currentUserRead = false; - ctsTyping?.Cancel(); - - var chat = getChat(chatId); - if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat) + var split = command.Split(" "); + if (split[0].Equals("open") || split[0].Equals("o")) { - currentChatUserId = privChat.UserId; - } + if (split.Length < 2) return; - currentChatId = chat.Id; - prefix = $"[{chat.Title}"; - lock (_lock) - { - messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}"); - messageQueue.Add($"{Ansi.Yellow}" + - $"[tgcli] You have {chat.UnreadCount} unread message" + - $"{(chat.UnreadCount == 1 ? "." : "s.")}"); - while (getHistory(chatId, 50).Count == 1) + var query = command.Substring(split[0].Length); + + var chatId = searchChatId(query); + if (chatId == 0) return; + + currentChatId = 0; + currentChatUserId = 0; + currentUserOnline = false; + currentUserTyping = false; + currentUserRead = false; + ctsTyping?.Cancel(); + + var chat = getChat(chatId); + if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat) { - getHistory(chatId, 10); + currentChatUserId = privChat.UserId; } - if (chat.UnreadCount >= 5) - { - var capped = chat.UnreadCount > 50; - getHistory(chatId, capped ? 50 : chat.UnreadCount).ForEach(AddMessageToQueue); - if (capped) - messageQueue.Add($"{Ansi.Yellow}[tgcli] " + - $"Showing 50 of {chat.UnreadCount} unread messages."); - } - else if (chat.UnreadCount > 0) - { - var unreads = getHistory(chatId, chat.UnreadCount); - var rest = getHistory(chatId, 5 - unreads.Count, unreads.First().Id); - rest.ForEach(AddMessageToQueue); - messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---"); - unreads.ForEach(AddMessageToQueue); - } - else - { - getHistory(chatId).ForEach(AddMessageToQueue); - } - } - - markRead(chat.Id, getHistory(chat.Id).First().Id); - var history = getHistory(currentChatId, 50); - var last = history.LastOrDefault(p => p.IsOutgoing); - if (last == null) - { - currentUserRead = true; - return; - } - - lastMessage = last; - currentUserRead = isMessageRead(last.ChatId, last.Id); - } - else if (split[0].Equals("close") || split[0].Equals("c")) - { - if (split.Length != 1 || currentChatId == 0) - return; - currentChatId = 0; - currentChatUserId = 0; - currentUserOnline = false; - currentUserTyping = false; - currentUserRead = false; - ctsTyping?.Cancel(); - lastMessage = null; - prefix = "[tgcli"; - - - lock (_lock) - { - messageQueue.Add($"{Ansi.Yellow}[tgcli] Closing chat."); - var count = missedMessages.Count; - if (count == 0) - return; - messageQueue.Add($"{Ansi.Yellow}" + - $"[tgcli] You have {count} missed message" + - $"{(count == 1 ? "." : "s.")}"); - messageQueue.AddRange(missedMessages); - missedMessages.Clear(); - } - } - else if (split[0].Equals("history") || split[0].Equals("h")) - { - if (split.Length != 1 && split.Length != 2 || currentChatId == 0) - { + currentChatId = chat.Id; + prefix = $"[{chat.Title}"; lock (_lock) - messageQueue.Add($"{Ansi.Red}[tgcli] " + - "No chat selected. Select a chat with /open "); - return; - } - - var history = split.Length > 1 && int.TryParse(split[1], out var limit) - ? getHistory(currentChatId, limit) - : getHistory(currentChatId); - - lock (_lock) - { - messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " + - $"{getChat(currentChatId).Title}"); - } - - foreach (var msg in history) - { - AddMessageToQueue(msg); - } - } - else if (split[0].Equals("clear") || split[0].Equals("cl")) - { - lock (_lock) - { - Console.Clear(); - } - } - else if (split[0].Equals("quit") || split[0].Equals("q")) - { - quitting = true; - } - else if (split[0].Equals("unreads") || split[0].Equals("u")) - { - var unreads = getUnreadChats(split.Length == 2 && split[1].Equals("all")); - - lock (_lock) - { - messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats."); - unreads.ForEach(chat => { - string line; - if (chat.UnreadCount == 0) - line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}"; + messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}"); + messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}"); + while (getHistory(chatId, 50).Count == 1) + { + getHistory(chatId, 10); + } + + if (chat.UnreadCount >= 5) + { + var capped = chat.UnreadCount > 50; + getHistory(chatId, capped ? 50 : chat.UnreadCount).ForEach(AddMessageToQueue); + if (capped) messageQueue.Add($"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages."); + } + else if (chat.UnreadCount > 0) + { + var unreads = getHistory(chatId, chat.UnreadCount); + var rest = getHistory(chatId, 5 - unreads.Count, unreads.First().Id); + rest.ForEach(AddMessageToQueue); + messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---"); + unreads.ForEach(AddMessageToQueue); + } else - line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}"; - messageQueue.Add(line); - }); - } - } - else if (split[0].Equals("edit") || split[0].Equals("e")) - { - if (currentChatId == 0) - return; + { + getHistory(chatId).ForEach(AddMessageToQueue); + } + } - if (split.Length == 1) - { - currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text; - return; - } - - if (lastMessage == null) - { - //try to find last message + markRead(chat.Id, getHistory(chat.Id).First().Id); var history = getHistory(currentChatId, 50); var last = history.LastOrDefault(p => p.IsOutgoing); if (last == null) + { + currentUserRead = true; return; + } + lastMessage = last; + currentUserRead = isMessageRead(last.ChatId, last.Id); + } + else if (split[0].Equals("cu") || split[0].Equals("closeunread")) + { + if (currentChatId == 0) return; + markUnread(currentChatId); + command = "close"; + continue; + } + else if (split[0].Equals("close") || split[0].Equals("c")) + { + if (split.Length != 1 || currentChatId == 0) return; + currentChatId = 0; + currentChatUserId = 0; + currentUserOnline = false; + currentUserTyping = false; + currentUserRead = false; + ctsTyping?.Cancel(); + lastMessage = null; + prefix = "[tgcli"; + + + lock (_lock) + { + messageQueue.Add($"{Ansi.Yellow}[tgcli] Closing chat."); + var count = missedMessages.Count; + if (count == 0) return; + messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {count} missed message" + $"{(count == 1 ? "." : "s.")}"); + messageQueue.AddRange(missedMessages); + missedMessages.Clear(); + } + } + else if (split[0].Equals("history") || split[0].Equals("h")) + { + if (split.Length != 1 && split.Length != 2 || currentChatId == 0) + { + lock (_lock) messageQueue.Add($"{Ansi.Red}[tgcli] " + "No chat selected. Select a chat with /open "); + return; + } + + var history = split.Length > 1 && int.TryParse(split[1], out var limit) + ? getHistory(currentChatId, limit) + : getHistory(currentChatId); + + lock (_lock) + { + messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " + $"{getChat(currentChatId).Title}"); + } + + foreach (var msg in history) + { + AddMessageToQueue(msg); + } + } + else if (split[0].Equals("clear") || split[0].Equals("cl")) + { + lock (_lock) + { + Console.Clear(); + } + } + else if (split[0].Equals("quit") || split[0].Equals("q")) + { + quitting = true; + } + else if (split[0].Equals("unreads") || split[0].Equals("u")) + { + var unreads = getUnreadChats(split.Length == 2 && split[1].Equals("all")); + + lock (_lock) + { + messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats."); + unreads.ForEach(chat => + { + string line; + if (chat.UnreadCount == 0) + line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}"; + else + line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}"; + messageQueue.Add(line); + }); + } + } + else if (split[0].Equals("edit") || split[0].Equals("e")) + { + if (currentChatId == 0) return; + + if (split.Length == 1) + { + currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text; + return; + } + + if (lastMessage == null) + { + //try to find last message + var history = getHistory(currentChatId, 50); + var last = history.LastOrDefault(p => p.IsOutgoing); + if (last == null) return; + lastMessage = last; + } + + var message = command.Substring(split[0].Length); + editMessage(message, lastMessage); } - var message = command.Substring(split[0].Length); - editMessage(message, lastMessage); + break; } } } diff --git a/telegram/Util.cs b/telegram/Util.cs index 40a791e..3ad317a 100644 --- a/telegram/Util.cs +++ b/telegram/Util.cs @@ -142,6 +142,7 @@ namespace telegram { if (string.IsNullOrWhiteSpace(message)) return; + message = message.Replace("⏎", "\n"); client.ExecuteAsync(new SendMessage { ChatId = chatId, @@ -187,6 +188,15 @@ namespace telegram }); } + public static void markUnread(long chatId) + { + client.ExecuteAsync(new ToggleChatIsMarkedAsUnread + { + ChatId = chatId, + IsMarkedAsUnread = true, + }); + } + public static long searchChatId(string query) { try diff --git a/telegram/tgcli.cs b/telegram/tgcli.cs index 033219e..b913751 100644 --- a/telegram/tgcli.cs +++ b/telegram/tgcli.cs @@ -14,8 +14,7 @@ namespace telegram * reply to x messages ago * cap length & truncate extremely long chat names! * make emojis not break terminal -> :shrug: and stuff - * mark chat as unread - * some way of typing newlines + * make typing newlines actually good (inputline as list?) * fix history not fully displaying on channel open (query returning less results than expected?) * waaay more error messages instead of just doing nothing * make Util.getActualStringWidth better @@ -179,7 +178,6 @@ namespace telegram Thread.Sleep(1); } - Command.ParseCommand("u"); ScreenUpdate(); while (!quitting) MainLoop(); @@ -293,9 +291,16 @@ namespace telegram break; default: { + if (key.Key == ConsoleKey.N && key.Modifiers.HasFlag(ConsoleModifiers.Control)) + { + currentInputLine += "⏎"; + ScreenUpdate(); + return; + } if (!specialKeys.Contains(key.Key)) { currentInputLine += key.KeyChar; + //TODO currentInputLine = currentInputLine.Replace(":xd:", "😂"); ScreenUpdate(); }