view.py 6.9 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. 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. class NodeView(urwid.WidgetWrap):
  35. def __init__(self, info):
  36. self.name = info
  37. self.text = urwid.Text(f"{self.name}")
  38. super().__init__(self.text)
  39. self._w = urwid.AttrWrap(self._w, None)
  40. self.update_w()
  41. def selectable(self):
  42. return True
  43. def keypress(self, size, key):
  44. #if key in ('q'):
  45. # raise urwid.ExitMainLoop()
  46. return key
  47. def update_w(self):
  48. self._w.focus_attr = 'line'
  49. def get_widget(self):
  50. return "NodeView"
  51. def get_name(self):
  52. return self.name
  53. class ConnectView(urwid.WidgetWrap):
  54. def __init__(self, info):
  55. self.name = info
  56. self.text = urwid.Text(f"{self.name}")
  57. super().__init__(self.text)
  58. self._w = urwid.AttrWrap(self._w, None)
  59. self.update_w()
  60. def selectable(self):
  61. return True
  62. def keypress(self, size, key):
  63. #if key in ('q'):
  64. # raise urwid.ExitMainLoop()
  65. return key
  66. def update_w(self):
  67. self._w.focus_attr = 'line'
  68. def get_widget(self):
  69. return "ConnectView"
  70. def get_name(self):
  71. return self.name
  72. class SlotView(urwid.WidgetWrap):
  73. def __init__(self, info):
  74. self.name = info
  75. self.text = urwid.Text(f"{self.name}")
  76. super().__init__(self.text)
  77. self._w = urwid.AttrWrap(self._w, None)
  78. self.update_w()
  79. def selectable(self):
  80. return True
  81. def keypress(self, size, key):
  82. #if key in ('q'):
  83. # raise urwid.ExitMainLoop()
  84. return key
  85. def update_w(self):
  86. self._w.focus_attr = 'line'
  87. def get_widget(self):
  88. return "SlotView"
  89. def get_name(self):
  90. return self.name
  91. class View():
  92. palette = [
  93. ('body','light gray','black', 'standout'),
  94. ("line","dark cyan","black","standout"),
  95. ]
  96. def __init__(self, data):
  97. self.data = data
  98. info_text = urwid.Text("")
  99. self.pile = urwid.Pile([info_text])
  100. scroll = ScrollBar(Scrollable(self.pile))
  101. rightbox = urwid.LineBox(scroll)
  102. self.listbox_content = []
  103. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  104. self.list = LeftList(self.listwalker)
  105. leftbox = urwid.LineBox(self.list)
  106. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  107. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  108. async def update_view(self):
  109. while True:
  110. names = []
  111. for item in self.listwalker.contents:
  112. name = item.get_name()
  113. names.append(name)
  114. for name, values in self.data.nodes.items():
  115. if name in names:
  116. continue
  117. else:
  118. widget = NodeView(name)
  119. self.listwalker.contents.append(widget)
  120. info = values["result"]
  121. channels = info["channels"]
  122. channel_lookup = {}
  123. for channel in channels:
  124. id = channel["id"]
  125. channel_lookup[id] = channel
  126. for channel in channels:
  127. if channel["session"] != "inbound":
  128. continue
  129. widget = ConnectView("inbound")
  130. self.listwalker.contents.append(widget)
  131. url = channel["url"]
  132. widget = SlotView(f" {url}")
  133. self.listwalker.contents.append(widget)
  134. widget = ConnectView(" outbound")
  135. self.listwalker.contents.append(widget)
  136. for i, id in enumerate(info["outbound_slots"]):
  137. if id == 0:
  138. widget = SlotView(f" {i}: none")
  139. self.listwalker.contents.append(widget)
  140. continue
  141. assert id in channel_lookup
  142. url = channel_lookup[id]["url"]
  143. widget = SlotView(f" {i}: {url}")
  144. self.listwalker.contents.append(widget)
  145. for channel in channels:
  146. if channel["session"] != "seed":
  147. continue
  148. widget = ConnectView("seed")
  149. self.listwalker.contents.append(widget)
  150. url = channel["url"]
  151. widget = SlotView(f" {url}")
  152. self.listwalker.contents.append(widget)
  153. for channel in channels:
  154. if channel["session"] != "manual":
  155. continue
  156. widget = ConnectView("manual")
  157. self.listwalker.contents.append(widget)
  158. url = channel["url"]
  159. widget = SlotView(f" {url}")
  160. self.listwalker.contents.append(widget)
  161. await asyncio.sleep(0.1)
  162. async def render_info(self):
  163. while True:
  164. await asyncio.sleep(0.1)
  165. self.pile.contents.clear()
  166. focus_w = self.list.get_focus()
  167. match focus_w[0].get_widget():
  168. case "NodeView":
  169. self.pile.contents.append((
  170. urwid.Text(f"Node selected"), self.pile.options()))
  171. case "ConnectView":
  172. self.pile.contents.append((
  173. urwid.Text("Connection selected"), self.pile.options()))
  174. case "SlotView":
  175. self.pile.contents.append((
  176. urwid.Text("Slot selected"), self.pile.options()))