view.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 asyncio
  19. import time
  20. from scroll import ScrollBar, Scrollable
  21. loop = asyncio.get_event_loop()
  22. class ServiceWidget(urwid.TreeWidget):
  23. def get_display_text(self):
  24. return self.get_node().get_value()['name']
  25. class ChildNode(urwid.TreeNode):
  26. def load_widget(self):
  27. return ServiceWidget(self)
  28. # Service/ session
  29. class ServiceNode(urwid.ParentNode):
  30. def load_widget(self):
  31. return ServiceWidget(self)
  32. def load_child_keys(self):
  33. data = self.get_value()
  34. return range(len(data['session']))
  35. def load_child_node(self, key):
  36. childdata = self.get_value()['session'][key]
  37. childdepth = self.get_depth() + 1
  38. if 'session' in childdata:
  39. childclass = ServiceNode
  40. else:
  41. childclass = ChildNode
  42. return childclass(childdata, parent=self, key=key, depth=childdepth)
  43. class Dnetview:
  44. palette = [
  45. ('body','light gray','black', 'standout'),
  46. ("line","dark cyan","black","standout"),
  47. ]
  48. def __init__(self, data=None):
  49. self.topnode = ServiceNode(data)
  50. self.listbox = urwid.Columns([urwid.TreeListBox(urwid.TreeWalker(self.topnode))])
  51. self.listbox.offset_rows = 1
  52. list_frame = urwid.LineBox(self.listbox)
  53. pile = urwid.Pile([])
  54. loop.create_task(get_info(pile))
  55. scroll = ScrollBar(Scrollable(pile))
  56. scroll_frame = urwid.LineBox(scroll)
  57. columns = urwid.Columns([list_frame, scroll_frame], focus_column=0)
  58. self.view = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  59. def main(self):
  60. self.loop = urwid.MainLoop(self.view, self.palette,
  61. event_loop=urwid.AsyncioEventLoop(loop=loop),
  62. unhandled_input=self.unhandled_input)
  63. self.loop.run()
  64. def unhandled_input(self, k):
  65. if k in ('q','Q'):
  66. raise urwid.ExitMainLoop()
  67. def get_example_tree():
  68. tree = {"name":"service","session":[]}
  69. for i in range(2):
  70. tree['session'].append({"name":f"session{str(i)}"})
  71. tree['session'][i]['session']=[]
  72. for j in range(2):
  73. tree['session'][i]['session'].append({"name":"connection"+
  74. str(i) + "." + str(j)})
  75. return tree
  76. async def get_info(pile):
  77. while True:
  78. await asyncio.sleep(0.5)
  79. t = time.localtime()
  80. current_time = time.strftime("%H:%M:%S", t)
  81. text = urwid.Text(f"{current_time}: recv ping-pong")
  82. pile.contents.append((text, pile.options()))
  83. if __name__ == '__main__':
  84. sample = get_example_tree()
  85. Dnetview(sample).main()