view.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. from scroll import ScrollBar, Scrollable
  21. from model import Model
  22. event_loop = asyncio.get_event_loop()
  23. class LeftList(urwid.ListBox):
  24. def focus_next(self):
  25. try:
  26. self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
  27. except:
  28. pass
  29. def focus_previous(self):
  30. try:
  31. self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
  32. except:
  33. pass
  34. def load_info(self):
  35. return InfoWidget(self)
  36. class NodeView(urwid.WidgetWrap):
  37. def __init__(self, info):
  38. self.name = info
  39. self.text = urwid.Text(f"{self.name}")
  40. super().__init__(self.text)
  41. self._w = urwid.AttrWrap(self._w, None)
  42. self.update_w()
  43. def selectable(self):
  44. return True
  45. def keypress(self, size, key):
  46. #if key in ('q'):
  47. # raise urwid.ExitMainLoop()
  48. return key
  49. def update_w(self):
  50. self._w.focus_attr = 'line'
  51. def get_widget(self):
  52. return "NodeView"
  53. def get_name(self):
  54. return self.name
  55. class ConnectView(urwid.WidgetWrap):
  56. def __init__(self, info):
  57. self.name = info
  58. self.text = urwid.Text(f"{self.name}")
  59. super().__init__(self.text)
  60. self._w = urwid.AttrWrap(self._w, None)
  61. self.update_w()
  62. def selectable(self):
  63. return True
  64. def keypress(self, size, key):
  65. #if key in ('q'):
  66. # raise urwid.ExitMainLoop()
  67. return key
  68. def update_w(self):
  69. self._w.focus_attr = 'line'
  70. def name(self):
  71. return "ConnectView"
  72. def get_name(self):
  73. return self.name
  74. class SlotView(urwid.WidgetWrap):
  75. def __init__(self, info):
  76. self.name = info
  77. self.text = urwid.Text(f"{self.name}")
  78. super().__init__(self.text)
  79. self._w = urwid.AttrWrap(self._w, None)
  80. self.update_w()
  81. def selectable(self):
  82. return True
  83. def keypress(self, size, key):
  84. #if key in ('q'):
  85. # raise urwid.ExitMainLoop()
  86. return key
  87. def update_w(self):
  88. self._w.focus_attr = 'line'
  89. def name(self):
  90. return "SlotView"
  91. def get_name(self):
  92. return self.name
  93. class View():
  94. palette = [
  95. ('body','light gray','black', 'standout'),
  96. ("line","dark cyan","black","standout"),
  97. ]
  98. def __init__(self, data):
  99. #logging.debug(f"dnetview init {data}")
  100. self.data = data
  101. info_text = urwid.Text("")
  102. self.pile = urwid.Pile([info_text])
  103. scroll = ScrollBar(Scrollable(self.pile))
  104. rightbox = urwid.LineBox(scroll)
  105. #self.node_info = urwid.Text("")
  106. #widget = NodeView(self.node_info)
  107. #self.connect_info = urwid.Text("")
  108. #widget2 = ConnectView(self.connect_info)
  109. #self.slot_info = urwid.Text("")
  110. #widget3 = SlotView(self.slot_info)
  111. self.listbox_content = []
  112. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  113. self.list = LeftList(self.listwalker)
  114. leftbox = urwid.LineBox(self.list)
  115. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  116. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  117. #def update_view(self, loop, data):
  118. # data = self.data
  119. # logging.debug(len(data.nodes))
  120. # for name, values in data.nodes.items():
  121. # self.node_info = urwid.Text(name)
  122. # widget = NodeView(self.node_info)
  123. # self.listbox_content.append(widget)
  124. # #loop.set_alarm_in(2, update_view)
  125. # #await asyncio.sleep(0.1)
  126. async def update_view(self, data):
  127. while True:
  128. names = []
  129. for item in self.listwalker.contents:
  130. name = item.get_name()
  131. names.append(name)
  132. for name, values in data.nodes.items():
  133. if name in names:
  134. continue
  135. else:
  136. widget = NodeView(name)
  137. self.listwalker.contents.append(widget)
  138. info = values["result"]
  139. channels = info["channels"]
  140. channel_lookup = {}
  141. for channel in channels:
  142. id = channel["id"]
  143. channel_lookup[id] = channel
  144. for channel in channels:
  145. if channel["session"] != "inbound":
  146. continue
  147. widget = ConnectView("inbound")
  148. self.listwalker.contents.append(widget)
  149. url = channel["url"]
  150. widget = SlotView(url)
  151. self.listwalker.contents.append(widget)
  152. widget = ConnectView(" outbound")
  153. self.listwalker.contents.append(widget)
  154. for i, id in enumerate(info["outbound_slots"]):
  155. if id == 0:
  156. widget = SlotView(f" {i}: none")
  157. self.listwalker.contents.append(widget)
  158. continue
  159. assert id in channel_lookup
  160. url = channel_lookup[id]["url"]
  161. widget = SlotView(f" {i}: {url}")
  162. self.listwalker.contents.append(widget)
  163. for channel in channels:
  164. if channel["session"] != "seed":
  165. continue
  166. widget = ConnectView("seed")
  167. self.listwalker.contents.append(widget)
  168. url = channel["url"]
  169. widget = SlotView(f" {url}")
  170. self.listwalker.contents.append(widget)
  171. for channel in channels:
  172. if channel["session"] != "manual":
  173. continue
  174. widget = ConnectView("manual")
  175. self.listwalker.contents.append(widget)
  176. url = channel["url"]
  177. widget = SlotView(url)
  178. self.listwalker.contents.append(widget)
  179. print(f" {url}")
  180. await asyncio.sleep(0.1)
  181. async def render_info(self, channels):
  182. while True:
  183. await asyncio.sleep(0.1)
  184. self.pile.contents.clear()
  185. focus_w = self.listbox.get_focus()
  186. match focus_w[0].name():
  187. case "NodeView":
  188. self.pile.contents.append((urwid.Text(f""), self.pile.options()))
  189. case "ConnectView":
  190. self.pile.contents.append((urwid.Text("2"), self.pile.options()))
  191. case "SlotView":
  192. self.pile.contents.append((urwid.Text("3"), self.pile.options()))