view.py 6.5 KB

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