view.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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
  21. from scroll import ScrollBar, Scrollable
  22. from model import Model
  23. event_loop = asyncio.get_event_loop()
  24. class LeftList(urwid.ListBox):
  25. def focus_next(self):
  26. try:
  27. self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
  28. except:
  29. pass
  30. def focus_previous(self):
  31. try:
  32. self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
  33. except:
  34. pass
  35. class NodeView(urwid.WidgetWrap):
  36. def __init__(self, info):
  37. self.name = info
  38. self.text = urwid.Text(f"{self.name}")
  39. super().__init__(self.text)
  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 get_widget(self):
  51. return "NodeView"
  52. def get_name(self):
  53. return self.name
  54. class ConnectView(urwid.WidgetWrap):
  55. def __init__(self, info):
  56. self.name = info
  57. self.text = urwid.Text(f"{self.name}")
  58. super().__init__(self.text)
  59. self._w = urwid.AttrWrap(self._w, None)
  60. self.update_w()
  61. def selectable(self):
  62. return True
  63. def keypress(self, size, key):
  64. #if key in ('q'):
  65. # raise urwid.ExitMainLoop()
  66. return key
  67. def update_w(self):
  68. self._w.focus_attr = 'line'
  69. def get_widget(self):
  70. return "ConnectView"
  71. def get_name(self):
  72. return self.name
  73. class SlotView(urwid.WidgetWrap):
  74. def __init__(self, info):
  75. self.name = info
  76. self.text = urwid.Text(f"{self.name}")
  77. super().__init__(self.text)
  78. self._w = urwid.AttrWrap(self._w, None)
  79. self.update_w()
  80. def selectable(self):
  81. return True
  82. def keypress(self, size, key):
  83. #if key in ('q'):
  84. # raise urwid.ExitMainLoop()
  85. return key
  86. def update_w(self):
  87. self._w.focus_attr = 'line'
  88. def get_widget(self):
  89. return "SlotView"
  90. def get_name(self):
  91. return self.name
  92. class View():
  93. palette = [
  94. ('body','light gray','black', 'standout'),
  95. ("line","dark cyan","black","standout"),
  96. ]
  97. def __init__(self, model):
  98. self.model = model
  99. info_text = urwid.Text("")
  100. self.pile = urwid.Pile([info_text])
  101. scroll = ScrollBar(Scrollable(self.pile))
  102. rightbox = urwid.LineBox(scroll)
  103. self.listbox_content = []
  104. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  105. self.list = LeftList(self.listwalker)
  106. leftbox = urwid.LineBox(self.list)
  107. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  108. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  109. async def update_view(self):
  110. while True:
  111. names = []
  112. for item in self.listwalker.contents:
  113. name = item.get_name()
  114. names.append(name)
  115. for name, values in self.model.nodes.items():
  116. if name in names:
  117. continue
  118. else:
  119. widget = NodeView(name)
  120. self.listwalker.contents.append(widget)
  121. outbounds = values.outbounds
  122. inbound = values.inbound
  123. manual = values.manual
  124. seed = values.seed
  125. if len(outbounds) != 0:
  126. widget = ConnectView(" outbound")
  127. self.listwalker.contents.append(widget)
  128. for num, name in outbounds.items():
  129. widget = SlotView(f" {num}: {name}")
  130. self.listwalker.contents.append(widget)
  131. if len(inbound) != 0:
  132. widget = ConnectView(" inbound")
  133. self.listwalker.contents.append(widget)
  134. if len(seed) != 0:
  135. widget = ConnectView(" seed")
  136. self.listwalker.contents.append(widget)
  137. if len(manual) != 0:
  138. widget = ConnectView(" manual")
  139. self.listwalker.contents.append(widget)
  140. await asyncio.sleep(0.1)
  141. async def render_info(self):
  142. while True:
  143. await asyncio.sleep(0.1)
  144. self.pile.contents.clear()
  145. focus_w = self.list.get_focus()
  146. match focus_w[0].get_widget():
  147. case "NodeView":
  148. self.pile.contents.append((
  149. urwid.Text(f"Node selected"),
  150. self.pile.options()))
  151. case "ConnectView":
  152. self.pile.contents.append((
  153. urwid.Text("Connection selected"),
  154. self.pile.options()))
  155. case "SlotView":
  156. name = focus_w[0].get_name()
  157. # Remove the prepend
  158. name = name[7:]
  159. if name in self.model.info.msgs.keys():
  160. values = (
  161. self.model.info.msgs.get(name)
  162. )
  163. for value in values:
  164. nanotime = (
  165. int(value[0])
  166. )
  167. time = (
  168. datetime.datetime.fromtimestamp(
  169. nanotime/1000000000).strftime(
  170. '%Y-%m-%d %H:%M:%S.%f')
  171. )
  172. event = value[1]
  173. msg = value[2]
  174. #logging.debug(values)
  175. self.pile.contents.append((
  176. urwid.Text(
  177. f"{time}: {event}: {msg}"),
  178. self.pile.options()))