|
| 1 | +# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +import time |
| 4 | +import adafruit_dang as curses |
| 5 | + |
| 6 | +from irc_client import IRCClient |
| 7 | + |
| 8 | +ANSI_BLACK_ON_GREY = chr(27) + "[30;100m" |
| 9 | +ANSI_RESET = chr(27) + "[0m" |
| 10 | + |
| 11 | + |
| 12 | +class Window: |
| 13 | + """ |
| 14 | + Terminal Window class that supports basic scrolling. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, n_rows, n_cols, row=0, col=0): |
| 18 | + self.n_rows = n_rows |
| 19 | + self.n_cols = n_cols |
| 20 | + self.row = row |
| 21 | + self.col = col |
| 22 | + |
| 23 | + @property |
| 24 | + def bottom(self): |
| 25 | + return self.row + self.n_rows - 1 |
| 26 | + |
| 27 | + def up(self, cursor): # pylint: disable=invalid-name |
| 28 | + if cursor.row == self.row - 1 and self.row > 0: |
| 29 | + self.row -= 1 |
| 30 | + |
| 31 | + def down(self, buffer, cursor): |
| 32 | + if cursor.row == self.bottom + 1 and self.bottom < len(buffer) - 1: |
| 33 | + self.row += 1 |
| 34 | + |
| 35 | + def horizontal_scroll(self, cursor, left_margin=5, right_margin=2): |
| 36 | + n_pages = cursor.col // (self.n_cols - right_margin) |
| 37 | + self.col = max(n_pages * self.n_cols - right_margin - left_margin, 0) |
| 38 | + |
| 39 | + def translate(self, cursor): |
| 40 | + return cursor.row - self.row, cursor.col - self.col |
| 41 | + |
| 42 | + |
| 43 | +def irc_client_main( |
| 44 | + stdscr, |
| 45 | + radio, |
| 46 | + irc_config, |
| 47 | + terminal_tilegrid=None, |
| 48 | + audio_interface=None, |
| 49 | + beep_wave=None, |
| 50 | +): |
| 51 | + # pylint: disable=too-many-locals, too-many-branches, too-many-statements |
| 52 | + """ |
| 53 | + Main curses IRC client application loop. |
| 54 | + """ |
| 55 | + irc_client = IRCClient( |
| 56 | + radio, irc_config, audio_interface=audio_interface, beep_wave=beep_wave |
| 57 | + ) |
| 58 | + irc_client.connect() |
| 59 | + # irc_client.join() |
| 60 | + |
| 61 | + window = Window(terminal_tilegrid.height, terminal_tilegrid.width) |
| 62 | + stdscr.erase() |
| 63 | + img = [None] * window.n_rows |
| 64 | + status_bar = { |
| 65 | + "user_message": None, |
| 66 | + "user_message_shown_time": 0, |
| 67 | + } |
| 68 | + |
| 69 | + cur_row_index = 0 |
| 70 | + |
| 71 | + user_input = "" |
| 72 | + |
| 73 | + def show_user_message(message): |
| 74 | + """ |
| 75 | + Show a status message to the user |
| 76 | + """ |
| 77 | + status_bar["user_message"] = message + ( |
| 78 | + " " * (window.n_cols - 1 - len(message)) |
| 79 | + ) |
| 80 | + status_bar["user_message_shown_time"] = time.monotonic() |
| 81 | + |
| 82 | + def setline(row, line): |
| 83 | + """ |
| 84 | + Set a line of text in the terminal window. |
| 85 | + """ |
| 86 | + if img[row] == line: |
| 87 | + return |
| 88 | + img[row] = line |
| 89 | + line += " " * (window.n_cols - len(line) - 1) |
| 90 | + stdscr.addstr(row, 0, line) |
| 91 | + |
| 92 | + def get_page(row_index): |
| 93 | + """ |
| 94 | + Get a page of messages from the message buffer. |
| 95 | + """ |
| 96 | + page_len = window.n_rows - 2 |
| 97 | + |
| 98 | + page_start = max((len(irc_client.message_buffer) + row_index) - page_len, 0) |
| 99 | + page_end = page_start + page_len |
| 100 | + |
| 101 | + page = irc_client.message_buffer[page_start:page_end] |
| 102 | + return page |
| 103 | + |
| 104 | + # pylint: disable=too-many-nested-blocks |
| 105 | + try: |
| 106 | + # main application loop |
| 107 | + while True: |
| 108 | + lastrow = 0 |
| 109 | + lines_added = irc_client.update() |
| 110 | + |
| 111 | + cur_page = get_page(cur_row_index) |
| 112 | + |
| 113 | + if lines_added > 0 and len(cur_page) < window.n_rows - 2: |
| 114 | + cur_row_index = max(cur_row_index - lines_added, 0) |
| 115 | + cur_page = get_page(cur_row_index) |
| 116 | + |
| 117 | + for row, line in enumerate(cur_page): |
| 118 | + lastrow = row |
| 119 | + setline(row, line) |
| 120 | + |
| 121 | + for row in range(lastrow + 1, window.n_rows - 2): |
| 122 | + setline(row, "") |
| 123 | + |
| 124 | + user_input_row = window.n_rows - 2 |
| 125 | + if user_input: |
| 126 | + setline(user_input_row, user_input) |
| 127 | + else: |
| 128 | + setline(user_input_row, " " * (window.n_cols - 1)) |
| 129 | + |
| 130 | + user_message_row = terminal_tilegrid.height - 1 |
| 131 | + if status_bar["user_message"] is None: |
| 132 | + message = f" {irc_config['username']} | {irc_config['server']} | {irc_config['channel']}" # pylint: disable=line-too-long |
| 133 | + message += " " * (terminal_tilegrid.width - len(message) - 1) |
| 134 | + line = f"{ANSI_BLACK_ON_GREY}{message}{ANSI_RESET}" |
| 135 | + else: |
| 136 | + line = f"{ANSI_BLACK_ON_GREY}{status_bar['user_message']}{ANSI_RESET}" |
| 137 | + if status_bar["user_message_shown_time"] + 3.0 < time.monotonic(): |
| 138 | + status_bar["user_message"] = None |
| 139 | + setline(user_message_row, line) |
| 140 | + |
| 141 | + # read from the keyboard |
| 142 | + k = stdscr.getkey() |
| 143 | + if k is not None: |
| 144 | + if len(k) == 1 and " " <= k <= "~": |
| 145 | + user_input += k |
| 146 | + |
| 147 | + elif k == "\n": # enter key pressed |
| 148 | + if not user_input.startswith("/"): |
| 149 | + print(f"sending: {user_input}") |
| 150 | + irc_client.send_message(user_input) |
| 151 | + user_input = "" |
| 152 | + else: # slash commands |
| 153 | + parts = user_input.split(" ", 1) |
| 154 | + if parts[0] in {"/j", "/join"}: |
| 155 | + if len(parts) >= 2 and parts[1] != "": |
| 156 | + if parts[1] != irc_client.config["channel"]: |
| 157 | + irc_client.join(parts[1]) |
| 158 | + user_input = "" |
| 159 | + else: |
| 160 | + show_user_message("Already in channel") |
| 161 | + user_input = "" |
| 162 | + |
| 163 | + else: |
| 164 | + show_user_message( |
| 165 | + "Invalid /join arg. Use: /join <channel>" |
| 166 | + ) |
| 167 | + user_input = "" |
| 168 | + elif parts[0] == "/msg": |
| 169 | + to_user, message_to_send = parts[1].split(" ", 1) |
| 170 | + irc_client.send_dm(to_user, message_to_send) |
| 171 | + user_input = "" |
| 172 | + elif parts[0] == "/beep": |
| 173 | + to_user = parts[1] |
| 174 | + message_to_send = "*Beep*\x07" |
| 175 | + irc_client.send_dm(to_user, message_to_send) |
| 176 | + user_input = "" |
| 177 | + elif parts[0] == "/op": |
| 178 | + user_to_op = parts[1] |
| 179 | + irc_client.op(user_to_op) |
| 180 | + user_input = "" |
| 181 | + elif parts[0] == "/deop": |
| 182 | + user_to_op = parts[1] |
| 183 | + irc_client.deop(user_to_op) |
| 184 | + user_input = "" |
| 185 | + elif parts[0] == "/kick": |
| 186 | + user_to_kick = parts[1] |
| 187 | + irc_client.kick(user_to_kick) |
| 188 | + user_input = "" |
| 189 | + elif parts[0] == "/ban": |
| 190 | + user_to_ban = parts[1] |
| 191 | + irc_client.ban(user_to_ban) |
| 192 | + user_input = "" |
| 193 | + elif parts[0] == "/unban": |
| 194 | + user_to_unban = parts[1] |
| 195 | + irc_client.unban(user_to_unban) |
| 196 | + user_input = "" |
| 197 | + elif parts[0] == "/whois": |
| 198 | + user_to_check = parts[1] |
| 199 | + irc_client.whois(user_to_check) |
| 200 | + user_input = "" |
| 201 | + |
| 202 | + elif k in ("KEY_BACKSPACE", "\x7f", "\x08"): |
| 203 | + user_input = user_input[:-1] |
| 204 | + elif k == "KEY_UP": |
| 205 | + page_len = window.n_rows - 2 |
| 206 | + if len(irc_client.message_buffer) > page_len: |
| 207 | + page_start = ( |
| 208 | + len(irc_client.message_buffer) + cur_row_index |
| 209 | + ) - page_len |
| 210 | + if page_start > 0: |
| 211 | + cur_row_index -= 1 |
| 212 | + elif k == "KEY_DOWN": |
| 213 | + if cur_row_index < 0: |
| 214 | + cur_row_index += 1 |
| 215 | + |
| 216 | + elif k == "KEY_PGUP": |
| 217 | + page_len = window.n_rows - 2 |
| 218 | + if len(irc_client.message_buffer) > page_len: |
| 219 | + page_start = ( |
| 220 | + len(irc_client.message_buffer) + cur_row_index |
| 221 | + ) - page_len |
| 222 | + if page_start > 0: |
| 223 | + cur_row_index -= 6 |
| 224 | + elif k == "KEY_PGDN": |
| 225 | + if cur_row_index <= 0: |
| 226 | + cur_row_index = cur_row_index + 6 |
| 227 | + else: |
| 228 | + print(f"unknown key: {k}") |
| 229 | + |
| 230 | + except KeyboardInterrupt as exc: |
| 231 | + irc_client.disconnect() |
| 232 | + raise KeyboardInterrupt from exc |
| 233 | + |
| 234 | + |
| 235 | +def run_irc_client( |
| 236 | + radio, irc_config, terminal, terminal_tilegrid, audio_interface=None, beep_wave=None |
| 237 | +): |
| 238 | + """ |
| 239 | + Entry point to run the curses IRC client application. |
| 240 | + """ |
| 241 | + return curses.custom_terminal_wrapper( |
| 242 | + terminal, |
| 243 | + irc_client_main, |
| 244 | + radio, |
| 245 | + irc_config, |
| 246 | + terminal_tilegrid, |
| 247 | + audio_interface, |
| 248 | + beep_wave, |
| 249 | + ) |
0 commit comments