NomadNet/nomadnet/ui/TextUI.py

199 lines
13 KiB
Python
Raw Normal View History

2021-04-08 20:57:31 +02:00
import RNS
2021-09-16 19:54:32 +02:00
import urwid
2021-04-09 22:07:38 +02:00
import time
2021-09-16 19:54:32 +02:00
import os
2021-09-25 17:11:52 +02:00
import platform
2021-04-09 22:07:38 +02:00
2021-05-04 20:53:03 +02:00
import nomadnet
2021-05-04 11:08:31 +02:00
from nomadnet.ui.textui import *
2021-05-04 20:53:03 +02:00
from nomadnet import NomadNetworkApp
COLORMODE_MONO = 1
COLORMODE_16 = 16
COLORMODE_88 = 88
COLORMODE_256 = 256
COLORMODE_TRUE = 2**24
THEME_DARK = 0x01
THEME_LIGHT = 0x02
THEMES = {
THEME_DARK: {
"urwid_theme": [
# Style name # 16-color style # Monochrome style # 88, 256 and true-color style
2021-09-11 12:21:35 +02:00
("heading", "light gray,underline", "default", "underline", "g93,underline", "default"),
("menubar", "black", "light gray", "standout", "#111", "#bbb"),
2022-02-26 18:40:04 +01:00
("scrollbar", "light gray", "default", "default", "#444", "default"),
2021-09-11 12:21:35 +02:00
("shortcutbar", "black", "light gray", "standout", "#111", "#bbb"),
2022-02-26 18:40:04 +01:00
("body_text", "light gray", "default", "default", "#ddd", "default"),
2021-09-11 12:21:35 +02:00
("error_text", "dark red", "default", "default", "dark red", "default"),
("warning_text", "yellow", "default", "default", "#ba4", "default"),
("inactive_text", "dark gray", "default", "default", "dark gray", "default"),
("buttons", "light green,bold", "default", "default", "#00a533", "default"),
("msg_editor", "black", "light cyan", "standout", "#111", "#0bb"),
("msg_header_ok", "black", "light green", "standout", "#111", "#6b2"),
("msg_header_caution", "black", "yellow", "standout", "#111", "#fd3"),
("msg_header_sent", "black", "light gray", "standout", "#111", "#ddd"),
2021-10-07 21:04:11 +02:00
("msg_header_propagated", "black", "light blue", "standout", "#111", "#28b"),
2021-09-11 12:21:35 +02:00
("msg_header_delivered", "black", "light blue", "standout", "#111", "#28b"),
("msg_header_failed", "black", "dark gray", "standout", "#000", "#777"),
("msg_warning_untrusted", "black", "dark red", "standout", "#111", "dark red"),
2021-08-28 21:11:06 +02:00
("list_focus", "black", "light gray", "standout", "#111", "#aaa"),
("list_off_focus", "black", "dark gray", "standout", "#111", "#777"),
2021-08-28 21:01:57 +02:00
("list_trusted", "dark green", "default", "default", "#6b2", "default"),
2021-08-28 21:11:06 +02:00
("list_focus_trusted", "black", "light gray", "standout", "#150", "#aaa"),
2021-08-28 20:12:03 +02:00
("list_unknown", "dark gray", "default", "default", "#bbb", "default"),
("list_normal", "dark gray", "default", "default", "#bbb", "default"),
2021-08-28 21:11:06 +02:00
("list_untrusted", "dark red", "default", "default", "#a22", "default"),
("list_focus_untrusted", "black", "light gray", "standout", "#810", "#aaa"),
2022-02-26 18:40:04 +01:00
("topic_list_normal", "light gray", "default", "default", "#ddd", "default"),
2021-09-11 12:21:35 +02:00
("browser_controls", "light gray", "default", "default", "#bbb", "default"),
2021-08-28 20:12:03 +02:00
("progress_full", "black", "light gray", "standout", "#111", "#bbb"),
("progress_empty", "light gray", "default", "default", "#ddd", "default"),
],
2021-09-17 21:16:30 +02:00
},
THEME_LIGHT: {
"urwid_theme": [
# Style name # 16-color style # Monochrome style # 88, 256 and true-color style
("heading", "dark gray,underline", "default", "underline", "g93,underline", "default"),
("menubar", "black", "dark gray", "standout", "#111", "#bbb"),
2022-02-26 18:40:04 +01:00
("scrollbar", "dark gray", "default", "default", "#444", "default"),
2021-09-17 21:16:30 +02:00
("shortcutbar", "black", "dark gray", "standout", "#111", "#bbb"),
2022-02-26 18:40:04 +01:00
("body_text", "dark gray", "default", "default", "#222", "default"),
2021-09-17 21:16:30 +02:00
("error_text", "dark red", "default", "default", "dark red", "default"),
("warning_text", "yellow", "default", "default", "#ba4", "default"),
("inactive_text", "light gray", "default", "default", "dark gray", "default"),
("buttons", "light green,bold", "default", "default", "#00a533", "default"),
("msg_editor", "black", "dark cyan", "standout", "#111", "#0bb"),
("msg_header_ok", "black", "dark green", "standout", "#111", "#6b2"),
("msg_header_caution", "black", "yellow", "standout", "#111", "#fd3"),
("msg_header_sent", "black", "dark gray", "standout", "#111", "#ddd"),
("msg_header_delivered", "black", "light blue", "standout", "#111", "#28b"),
("msg_header_failed", "black", "dark gray", "standout", "#000", "#777"),
("msg_warning_untrusted", "black", "dark red", "standout", "#111", "dark red"),
("list_focus", "black", "dark gray", "standout", "#111", "#aaa"),
("list_off_focus", "black", "dark gray", "standout", "#111", "#777"),
("list_trusted", "dark green", "default", "default", "#4a0", "default"),
("list_focus_trusted", "black", "dark gray", "standout", "#150", "#aaa"),
("list_unknown", "dark gray", "default", "default", "#444", "default"),
("list_normal", "dark gray", "default", "default", "#444", "default"),
("list_untrusted", "dark red", "default", "default", "#a22", "default"),
("list_focus_untrusted", "black", "dark gray", "standout", "#810", "#aaa"),
2022-02-26 18:40:04 +01:00
("topic_list_normal", "dark gray", "default", "default", "#222", "default"),
2021-09-17 21:16:30 +02:00
("browser_controls", "dark gray", "default", "default", "#444", "default"),
("progress_full", "black", "dark gray", "standout", "#111", "#bbb"),
("progress_empty", "dark gray", "default", "default", "#ddd", "default"),
],
}
2021-05-04 20:53:03 +02:00
}
2021-04-08 20:57:31 +02:00
2021-07-02 13:35:10 +02:00
GLYPHSETS = {
"plain": 1,
"unicode": 2,
"nerdfont": 3
}
2021-09-25 17:11:52 +02:00
if platform.system() == "Darwin":
urm_char = " \uf0e0 "
2021-09-25 17:13:18 +02:00
ur_char = "\uf0e0 "
2021-09-25 17:11:52 +02:00
else:
urm_char = " \uf003 "
2021-09-25 17:13:18 +02:00
ur_char = "\uf003 "
2021-09-25 17:11:52 +02:00
2021-07-02 13:35:10 +02:00
GLYPHS = {
# Glyph name # Plain # Unicode # Nerd Font
("check", "=", "\u2713", "\u2713"),
("cross", "X", "\u2715", "\u2715"),
("unknown", "?", "?", "?"),
2021-09-16 19:54:32 +02:00
("encrypted", "", "\u26BF", "\uf023"),
("plaintext", "!", "!", "\uf06e "),
2021-07-02 13:35:10 +02:00
("arrow_r", "->", "\u2192", "\u2192"),
("arrow_l", "<-", "\u2190", "\u2190"),
("arrow_u", "/\\", "\u2191", "\u2191"),
("arrow_d", "\\/", "\u2193", "\u2193"),
("warning", "!", "\u26a0", "\uf12a"),
("info", "i", "\u2139", "\ufb4d"),
2021-09-25 17:13:18 +02:00
("unread", "[!]", "\u2709", ur_char),
2021-07-02 13:35:10 +02:00
("divider1", "-", "\u2504", "\u2504"),
2021-09-16 19:54:32 +02:00
("peer", "[P]", "\u24c5 ", "\uf415"),
("node", "[N]", "\u24c3 ", "\uf502"),
2021-10-08 17:35:34 +02:00
("page", "", "\u25a4 ", "\uf719 "),
("speed", "", "\u25F7 ", "\uf9c4"),
("decoration_menu", " +", " +", " \uf93a"),
("unread_menu", " !", " \u2709", urm_char),
2021-09-16 19:54:32 +02:00
("globe", "", "", "\uf484"),
("sent", "/\\", "\u2191", "\ufbf4")
2021-07-02 13:35:10 +02:00
}
2021-04-08 20:57:31 +02:00
class TextUI:
def __init__(self):
self.app = NomadNetworkApp.get_shared_instance()
2021-05-12 14:02:44 +02:00
self.app.ui = self
2021-04-08 20:57:31 +02:00
self.loop = None
2021-04-09 22:07:38 +02:00
urwid.set_encoding("UTF-8")
intro_timeout = self.app.config["textui"]["intro_time"]
colormode = self.app.config["textui"]["colormode"]
theme = self.app.config["textui"]["theme"]
mouse_enabled = self.app.config["textui"]["mouse_enabled"]
2021-04-09 22:07:38 +02:00
self.palette = THEMES[theme]["urwid_theme"]
2021-07-02 13:35:10 +02:00
if self.app.config["textui"]["glyphs"] == "plain":
glyphset = "plain"
elif self.app.config["textui"]["glyphs"] == "unicoode":
glyphset = "unicode"
elif self.app.config["textui"]["glyphs"] == "nerdfont":
glyphset = "nerdfont"
else:
glyphset = "unicode"
self.glyphs = {}
for glyph in GLYPHS:
self.glyphs[glyph[0]] = glyph[GLYPHSETS[glyphset]]
2021-04-09 22:07:38 +02:00
self.screen = urwid.raw_display.Screen()
self.screen.register_palette(self.palette)
2021-04-09 22:07:38 +02:00
2021-05-04 20:53:03 +02:00
self.main_display = Main.MainDisplay(self, self.app)
2021-04-09 22:07:38 +02:00
if intro_timeout > 0:
2021-05-04 20:53:03 +02:00
self.intro_display = Extras.IntroDisplay(self.app)
2021-04-09 22:07:38 +02:00
initial_widget = self.intro_display.widget
else:
initial_widget = self.main_display.widget
2021-06-30 23:29:37 +02:00
self.loop = urwid.MainLoop(initial_widget, screen=self.screen, handle_mouse=mouse_enabled)
2021-04-09 22:07:38 +02:00
if intro_timeout > 0:
self.loop.set_alarm_in(intro_timeout, self.display_main)
2021-09-16 19:54:32 +02:00
if "KONSOLE_VERSION" in os.environ:
if colormode > 16:
RNS.log("", RNS.LOG_WARNING, _override_destination = True)
RNS.log("", RNS.LOG_WARNING, _override_destination = True)
RNS.log("You are using the terminal emulator Konsole.", RNS.LOG_WARNING, _override_destination = True)
RNS.log("If you are not seeing the user interface, it is due to a bug in Konsole/urwid.", RNS.LOG_WARNING, _override_destination = True)
RNS.log("", RNS.LOG_WARNING, _override_destination = True)
RNS.log("To circumvent this, use another terminal emulator, or launch nomadnet within a", RNS.LOG_WARNING, _override_destination = True)
RNS.log("screen session, using a command like the following:", RNS.LOG_WARNING, _override_destination = True)
RNS.log("", RNS.LOG_WARNING, _override_destination = True)
RNS.log("screen nomadnet", RNS.LOG_WARNING, _override_destination = True)
RNS.log("", RNS.LOG_WARNING, _override_destination = True)
RNS.log("Press ctrl-c to exit now and try again.", RNS.LOG_WARNING, _override_destination = True)
2021-04-09 22:07:38 +02:00
self.set_colormode(colormode)
2021-09-23 17:20:13 +02:00
self.main_display.start()
2021-04-09 22:07:38 +02:00
self.loop.run()
def set_colormode(self, colormode):
self.colormode = colormode
2021-04-09 22:07:38 +02:00
self.screen.set_terminal_properties(colormode)
self.screen.reset_default_terminal_palette()
def display_main(self, loop, user_data):
self.loop.widget = self.main_display.widget