view.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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','black', 'standout'),
  109. ("line","dark cyan","black","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.outbounds
  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. if len(seed) != 0:
  149. widget = ConnectView(node, "seed")
  150. self.listwalker.contents.append(widget)
  151. if len(manual) != 0:
  152. widget = ConnectView(node, "manual")
  153. self.listwalker.contents.append(widget)
  154. for index, item in enumerate(self.listwalker.contents):
  155. name = item.get_name()
  156. if name in self.model.info.event.keys():
  157. slot_num = name[1]
  158. match slot_num:
  159. case "outbound":
  160. continue
  161. case "inbound":
  162. continue
  163. case _:
  164. value = self.model.info.event.get(name)
  165. widget = SlotView(node, slot_num, value)
  166. self.listwalker.contents[index] = widget
  167. async def render_info(self):
  168. while True:
  169. await asyncio.sleep(0.1)
  170. self.pile.contents.clear()
  171. focus_w = self.list.get_focus()
  172. if focus_w[0] is None:
  173. continue
  174. else:
  175. match focus_w[0].get_widget():
  176. case "NodeView":
  177. self.pile.contents.append((
  178. urwid.Text(f"Node selected"),
  179. self.pile.options()))
  180. case "ConnectView":
  181. name = focus_w[0].get_name()
  182. if name in self.model.info.event.keys():
  183. values = self.model.info.event.get(name)
  184. self.pile.contents.append((
  185. urwid.Text(f" {values}"),
  186. self.pile.options()))
  187. case "SlotView":
  188. addr = focus_w[0].get_addr()
  189. if addr in self.model.info.msgs.keys():
  190. values = self.model.info.msgs.get(addr)
  191. for value in values:
  192. time = value[0]
  193. event = value[1]
  194. msg = value[2]
  195. self.pile.contents.append((urwid.Text(
  196. f"{time}: {event}: {msg}"),
  197. self.pile.options()))