view.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. import datetime as dt
  21. from scroll import ScrollBar, Scrollable
  22. from model import Model
  23. #----------------------------------------------------------------------
  24. # TODO:
  25. # * create a dictionary that stores:
  26. # * channel[id] = index
  27. # * index = listwalker.contents[i]
  28. # * sort data by ID, constantly update listwalker_contents[i]
  29. # * if it's a null id, render empty info
  30. # -------------------------------------------------------------------
  31. event_loop = asyncio.get_event_loop()
  32. class LeftList(urwid.ListBox):
  33. def focus_next(self):
  34. try:
  35. self.body.set_focus(self.body.get_next(
  36. self.body.get_focus()[1])[1])
  37. except:
  38. pass
  39. def focus_previous(self):
  40. try:
  41. self.body.set_focus(self.body.get_prev(
  42. self.body.get_focus()[1])[1])
  43. except:
  44. pass
  45. class NodeView(urwid.WidgetWrap):
  46. def __init__(self, info):
  47. self.name = info
  48. self.text = urwid.Text(f"{self.name}")
  49. super().__init__(self.text)
  50. self._w = urwid.AttrWrap(self._w, None)
  51. self.update_w()
  52. def selectable(self):
  53. return True
  54. def keypress(self, size, key):
  55. #if key in ('q'):
  56. # raise urwid.ExitMainLoop()
  57. return key
  58. def update_w(self):
  59. self._w.focus_attr = 'line'
  60. def get_widget(self):
  61. return "NodeView"
  62. def get_name(self):
  63. return self.name
  64. class ConnectView(urwid.WidgetWrap):
  65. def __init__(self, node, kind):
  66. self.name = (f"{node}", f"{kind}")
  67. self.text = urwid.Text(f" {kind}")
  68. super().__init__(self.text)
  69. self._w = urwid.AttrWrap(self._w, None)
  70. self.update_w()
  71. def selectable(self):
  72. return True
  73. def keypress(self, size, key):
  74. return key
  75. def update_w(self):
  76. self._w.focus_attr = 'line'
  77. def get_widget(self):
  78. return "ConnectView"
  79. def get_name(self):
  80. return self.name
  81. class SlotView(urwid.WidgetWrap):
  82. def __init__(self, node, num, info):
  83. self.num = num
  84. self.name = (f"{node}", f"{num}")
  85. #self.name = info[0]
  86. self.addr = info
  87. if len(num) == 1:
  88. self.text = urwid.Text(f" {num}: {self.addr}")
  89. else:
  90. self.text = urwid.Text(f" {self.addr}")
  91. super().__init__(self.text)
  92. self._w = urwid.AttrWrap(self._w, None)
  93. self.update_w()
  94. def selectable(self):
  95. return True
  96. def keypress(self, size, key):
  97. return key
  98. def update_w(self):
  99. self._w.focus_attr = 'line'
  100. def get_widget(self):
  101. return "SlotView"
  102. def get_name(self):
  103. return self.name
  104. def get_addr(self):
  105. return self.addr
  106. class View():
  107. palette = [
  108. ('body','light gray','default', 'standout'),
  109. ('line','dark cyan','default','standout'),
  110. ]
  111. def __init__(self, model):
  112. self.model = model
  113. info_text = urwid.Text("")
  114. self.pile = urwid.Pile([info_text])
  115. scroll = ScrollBar(Scrollable(self.pile))
  116. rightbox = urwid.LineBox(scroll)
  117. self.listbox_content = []
  118. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  119. self.list = LeftList(self.listwalker)
  120. leftbox = urwid.LineBox(self.list)
  121. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  122. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  123. async def update_view(self):
  124. online = []
  125. while True:
  126. await asyncio.sleep(0.1)
  127. for index, item in enumerate(self.listwalker.contents):
  128. online.append(item.get_name())
  129. for node, values in self.model.nodes.items():
  130. if node in online:
  131. continue
  132. else:
  133. widget = NodeView(node)
  134. self.listwalker.contents.append(widget)
  135. outbounds = values.outbound
  136. inbound = values.inbound
  137. manual = values.manual
  138. seed = values.seed
  139. if len(outbounds) != 0:
  140. widget = ConnectView(node, "outbound")
  141. self.listwalker.contents.append(widget)
  142. for i, info in outbounds.items():
  143. widget = SlotView(node, i, info)
  144. self.listwalker.contents.append(widget)
  145. if len(inbound) != 0:
  146. widget = ConnectView(node, "inbound")
  147. self.listwalker.contents.append(widget)
  148. for i, info in inbound.items():
  149. widget = SlotView(node, i, info)
  150. self.listwalker.contents.append(widget)
  151. #logging.debug(len(self.listwalker.contents))
  152. if len(seed) != 0:
  153. widget = ConnectView(node, "seed")
  154. self.listwalker.contents.append(widget)
  155. if len(manual) != 0:
  156. widget = ConnectView(node, "manual")
  157. self.listwalker.contents.append(widget)
  158. for index, item in enumerate(self.listwalker.contents):
  159. name = item.get_name()
  160. if name in self.model.info.event.keys():
  161. postfix = name[1]
  162. match postfix:
  163. case "outbound":
  164. # Outhound event info (displayed in render_info())
  165. continue
  166. case "inbound":
  167. continue
  168. case _:
  169. # Slot event info
  170. value = self.model.info.event.get(name)
  171. widget = SlotView(node, postfix, value)
  172. self.listwalker.contents[index] = widget
  173. async def render_info(self):
  174. while True:
  175. await asyncio.sleep(0.1)
  176. self.pile.contents.clear()
  177. focus_w = self.list.get_focus()
  178. if focus_w[0] is None:
  179. continue
  180. else:
  181. match focus_w[0].get_widget():
  182. case "NodeView":
  183. self.pile.contents.append((
  184. urwid.Text(f"Node selected"),
  185. self.pile.options()))
  186. case "ConnectView":
  187. name = focus_w[0].get_name()
  188. if name in self.model.info.event.keys():
  189. values = self.model.info.event.get(name)
  190. self.pile.contents.append((
  191. urwid.Text(f" {values}"),
  192. self.pile.options()))
  193. case "SlotView":
  194. addr = focus_w[0].get_addr()
  195. if addr in self.model.info.msgs.keys():
  196. values = self.model.info.msgs.get(addr)
  197. for value in values:
  198. time = value[0]
  199. event = value[1]
  200. msg = value[2]
  201. self.pile.contents.append((urwid.Text(
  202. f"{time}: {event}: {msg}"),
  203. self.pile.options()))