view.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, info):
  66. self.name = info
  67. self.text = urwid.Text(f"{self.name}")
  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, info):
  83. self.name = info
  84. self.text = urwid.Text(f"{self.name}")
  85. super().__init__(self.text)
  86. self._w = urwid.AttrWrap(self._w, None)
  87. self.update_w()
  88. def selectable(self):
  89. return True
  90. def keypress(self, size, key):
  91. return key
  92. def update_w(self):
  93. self._w.focus_attr = 'line'
  94. def get_widget(self):
  95. return "SlotView"
  96. def get_name(self):
  97. return self.name
  98. class View():
  99. palette = [
  100. ('body','light gray','black', 'standout'),
  101. ("line","dark cyan","black","standout"),
  102. ]
  103. def __init__(self, model):
  104. self.model = model
  105. info_text = urwid.Text("")
  106. self.pile = urwid.Pile([info_text])
  107. scroll = ScrollBar(Scrollable(self.pile))
  108. rightbox = urwid.LineBox(scroll)
  109. self.listbox_content = []
  110. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  111. self.list = LeftList(self.listwalker)
  112. leftbox = urwid.LineBox(self.list)
  113. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  114. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  115. async def update_view(self):
  116. names = []
  117. while True:
  118. await asyncio.sleep(0.1)
  119. for item in self.listwalker.contents:
  120. name = item.get_name()
  121. names.append(name)
  122. for name, values in self.model.nodes.items():
  123. # Update events
  124. if name in names:
  125. for key, value in values.outbounds.items():
  126. if len(value) == 1:
  127. continue
  128. else:
  129. slot = SlotView(f" {key}: {str(value[1])}")
  130. self.listwalker.contents[int(key)] = widget
  131. # Update get_info()
  132. else:
  133. widget = NodeView(name)
  134. self.listwalker.contents.append(widget)
  135. outbounds = values.outbounds
  136. logging.debug("outbounds", outbounds)
  137. inbound = values.inbound
  138. manual = values.manual
  139. seed = values.seed
  140. if len(outbounds) != 0:
  141. widget = ConnectView(" outbound")
  142. self.listwalker.contents.append(widget)
  143. for num, info in outbounds.items():
  144. widget = SlotView(f" {num}: {info[0]}")
  145. self.listwalker.contents.append(widget)
  146. if len(inbound) != 0:
  147. widget = ConnectView(" inbound")
  148. self.listwalker.contents.append(widget)
  149. if len(seed) != 0:
  150. widget = ConnectView(" seed")
  151. self.listwalker.contents.append(widget)
  152. if len(manual) != 0:
  153. widget = ConnectView(" manual")
  154. self.listwalker.contents.append(widget)
  155. async def render_info(self):
  156. while True:
  157. await asyncio.sleep(0.1)
  158. self.pile.contents.clear()
  159. focus_w = self.list.get_focus()
  160. match focus_w[0].get_widget():
  161. case "NodeView":
  162. self.pile.contents.append((
  163. urwid.Text(f"Node selected"),
  164. self.pile.options()))
  165. case "ConnectView":
  166. self.pile.contents.append((
  167. urwid.Text("Connection selected"),
  168. self.pile.options()))
  169. case "SlotView":
  170. numbered_name = focus_w[0].get_name()
  171. # Remove numbering
  172. name = numbered_name[7:]
  173. if name in self.model.info.msgs.keys():
  174. values = (self.model.info.msgs.get(name))
  175. for value in values:
  176. nano = (int(value[0]))
  177. time = (dt.datetime
  178. .fromtimestamp(nano/1000000000)
  179. .strftime('%Y-%m-%d %H:%M:%S.%f'))
  180. event = value[1]
  181. msg = value[2]
  182. self.pile.contents.append((urwid.Text(
  183. f"{time}: {event}: {msg}"),
  184. self.pile.options()))