view.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. from scroll import ScrollBar, Scrollable
  20. event_loop = asyncio.get_event_loop()
  21. class LeftList(urwid.ListBox):
  22. def focus_next(self):
  23. try:
  24. self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
  25. except:
  26. pass
  27. def focus_previous(self):
  28. try:
  29. self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
  30. except:
  31. pass
  32. def load_info(self):
  33. return InfoWidget(self)
  34. class ServiceView(urwid.WidgetWrap):
  35. def __init__(self):
  36. test = urwid.Text("1")
  37. super().__init__(test)
  38. self._w = urwid.AttrWrap(self._w, None)
  39. self.update_w()
  40. def selectable(self):
  41. return True
  42. def keypress(self, size, key):
  43. if key in ('q'):
  44. raise urwid.ExitMainLoop()
  45. return key
  46. def update_w(self):
  47. self._w.focus_attr = 'line'
  48. def name(self):
  49. return "ServiceView"
  50. class SessionView(urwid.WidgetWrap):
  51. def __init__(self):
  52. test = urwid.Text("2")
  53. super().__init__(test)
  54. self._w = urwid.AttrWrap(self._w, None)
  55. self.update_w()
  56. def selectable(self):
  57. return True
  58. def keypress(self, size, key):
  59. if key in ('q'):
  60. raise urwid.ExitMainLoop()
  61. return key
  62. def update_w(self):
  63. self._w.focus_attr = 'line'
  64. def name(self):
  65. return "SessionView"
  66. class ConnectView(urwid.WidgetWrap):
  67. def __init__(self):
  68. test = urwid.Text("3")
  69. super().__init__(test)
  70. self._w = urwid.AttrWrap(self._w, None)
  71. self.update_w()
  72. def selectable(self):
  73. return True
  74. def keypress(self, size, key):
  75. if key in ('q'):
  76. raise urwid.ExitMainLoop()
  77. return key
  78. def update_w(self):
  79. self._w.focus_attr = 'line'
  80. def name(self):
  81. return "ConnectView"
  82. class Dnetview():
  83. palette = [
  84. ('body','light gray','black', 'standout'),
  85. ("line","dark cyan","black","standout"),
  86. ]
  87. def __init__(self, data=None):
  88. info_text = urwid.Text("")
  89. self.pile = urwid.Pile([info_text])
  90. scroll = ScrollBar(Scrollable(self.pile))
  91. rightbox = urwid.LineBox(scroll)
  92. widget = ServiceView()
  93. widget2 = SessionView()
  94. widget3 = ConnectView()
  95. listbox_content = [widget, widget2, widget3]
  96. self.listbox = LeftList(urwid.SimpleListWalker(listbox_content))
  97. leftbox = urwid.LineBox(self.listbox)
  98. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  99. self.view = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  100. async def render_info(self):
  101. while True:
  102. await asyncio.sleep(0.1)
  103. self.pile.contents.clear()
  104. focus_w = self.listbox.get_focus()
  105. match focus_w[0].name():
  106. case "ServiceView":
  107. self.pile.contents.append((urwid.Text("1"), self.pile.options()))
  108. case "SessionView":
  109. self.pile.contents.append((urwid.Text("2"), self.pile.options()))
  110. case "ConnectView":
  111. self.pile.contents.append((urwid.Text("3"), self.pile.options()))