view.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import urwid
  18. import logging
  19. import asyncio
  20. from scroll import ScrollBar, Scrollable
  21. from model import NodeInfo
  22. event_loop = asyncio.get_event_loop()
  23. class LeftList(urwid.ListBox):
  24. def focus_next(self):
  25. try:
  26. self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
  27. except:
  28. pass
  29. def focus_previous(self):
  30. try:
  31. self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
  32. except:
  33. pass
  34. def load_info(self):
  35. return InfoWidget(self)
  36. class ServiceView(urwid.WidgetWrap):
  37. def __init__(self, info):
  38. test = urwid.Text(f"{info}")
  39. super().__init__(test)
  40. self._w = urwid.AttrWrap(self._w, None)
  41. self.update_w()
  42. def selectable(self):
  43. return True
  44. def keypress(self, size, key):
  45. #if key in ('q'):
  46. # raise urwid.ExitMainLoop()
  47. return key
  48. def update_w(self):
  49. self._w.focus_attr = 'line'
  50. def name(self):
  51. return "ServiceView"
  52. class SessionView(urwid.WidgetWrap):
  53. def __init__(self, info):
  54. test = urwid.Text(f"{info}")
  55. super().__init__(test)
  56. self._w = urwid.AttrWrap(self._w, None)
  57. self.update_w()
  58. def selectable(self):
  59. return True
  60. def keypress(self, size, key):
  61. #if key in ('q'):
  62. # raise urwid.ExitMainLoop()
  63. return key
  64. def update_w(self):
  65. self._w.focus_attr = 'line'
  66. def name(self):
  67. return "SessionView"
  68. class ConnectView(urwid.WidgetWrap):
  69. def __init__(self, info):
  70. test = urwid.Text(f"{info}")
  71. super().__init__(test)
  72. self._w = urwid.AttrWrap(self._w, None)
  73. self.update_w()
  74. def selectable(self):
  75. return True
  76. def keypress(self, size, key):
  77. #if key in ('q'):
  78. # raise urwid.ExitMainLoop()
  79. return key
  80. def update_w(self):
  81. self._w.focus_attr = 'line'
  82. def name(self):
  83. return "ConnectView"
  84. class View():
  85. palette = [
  86. ('body','light gray','black', 'standout'),
  87. ("line","dark cyan","black","standout"),
  88. ]
  89. def __init__(self, data=NodeInfo):
  90. #logging.debug(f"dnetview init {data}")
  91. info_text = urwid.Text("")
  92. self.pile = urwid.Pile([info_text])
  93. scroll = ScrollBar(Scrollable(self.pile))
  94. rightbox = urwid.LineBox(scroll)
  95. self.service_info = urwid.Text("")
  96. widget = ServiceView(self.service_info)
  97. self.session_info = urwid.Text("")
  98. widget2 = SessionView(self.session_info)
  99. self.connect_info = urwid.Text("")
  100. widget3 = ConnectView(self.connect_info)
  101. self.listbox_content = [widget, widget2, widget3]
  102. self.listbox = LeftList(urwid.SimpleListWalker(self.listbox_content))
  103. leftbox = urwid.LineBox(self.listbox)
  104. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  105. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  106. async def update_view(self, data=NodeInfo):
  107. while True:
  108. await asyncio.sleep(0.1)
  109. self.service_info = urwid.Text("")
  110. async def render_info(self, channels):
  111. while True:
  112. await asyncio.sleep(0.1)
  113. self.pile.contents.clear()
  114. focus_w = self.listbox.get_focus()
  115. match focus_w[0].name():
  116. case "ServiceView":
  117. self.pile.contents.append((urwid.Text(f""), self.pile.options()))
  118. case "SessionView":
  119. self.pile.contents.append((urwid.Text("2"), self.pile.options()))
  120. case "ConnectView":
  121. self.pile.contents.append((urwid.Text("3"), self.pile.options()))