cursesio.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import curses
  15. import curses.wrapper
  16. from curses.ascii import isprint
  17. from twisted.internet import reactor
  18. class CursesStdIO:
  19. def __init__(self, stdscr, callback=None):
  20. self.statusText = "Synapse test app -"
  21. self.searchText = ""
  22. self.stdscr = stdscr
  23. self.logLine = ""
  24. self.callback = callback
  25. self._setup()
  26. def _setup(self):
  27. self.stdscr.nodelay(1) # Make non blocking
  28. self.rows, self.cols = self.stdscr.getmaxyx()
  29. self.lines = []
  30. curses.use_default_colors()
  31. self.paintStatus(self.statusText)
  32. self.stdscr.refresh()
  33. def set_callback(self, callback):
  34. self.callback = callback
  35. def fileno(self):
  36. """ We want to select on FD 0 """
  37. return 0
  38. def connectionLost(self, reason):
  39. self.close()
  40. def print_line(self, text):
  41. """ add a line to the internal list of lines"""
  42. self.lines.append(text)
  43. self.redraw()
  44. def print_log(self, text):
  45. self.logLine = text
  46. self.redraw()
  47. def redraw(self):
  48. """ method for redisplaying lines
  49. based on internal list of lines """
  50. self.stdscr.clear()
  51. self.paintStatus(self.statusText)
  52. i = 0
  53. index = len(self.lines) - 1
  54. while i < (self.rows - 3) and index >= 0:
  55. self.stdscr.addstr(self.rows - 3 - i, 0, self.lines[index], curses.A_NORMAL)
  56. i = i + 1
  57. index = index - 1
  58. self.printLogLine(self.logLine)
  59. self.stdscr.refresh()
  60. def paintStatus(self, text):
  61. if len(text) > self.cols:
  62. raise RuntimeError("TextTooLongError")
  63. self.stdscr.addstr(
  64. self.rows - 2, 0, text + " " * (self.cols - len(text)), curses.A_STANDOUT
  65. )
  66. def printLogLine(self, text):
  67. self.stdscr.addstr(
  68. 0, 0, text + " " * (self.cols - len(text)), curses.A_STANDOUT
  69. )
  70. def doRead(self):
  71. """ Input is ready! """
  72. curses.noecho()
  73. c = self.stdscr.getch() # read a character
  74. if c == curses.KEY_BACKSPACE:
  75. self.searchText = self.searchText[:-1]
  76. elif c == curses.KEY_ENTER or c == 10:
  77. text = self.searchText
  78. self.searchText = ""
  79. self.print_line(">> %s" % text)
  80. try:
  81. if self.callback:
  82. self.callback.on_line(text)
  83. except Exception as e:
  84. self.print_line(str(e))
  85. self.stdscr.refresh()
  86. elif isprint(c):
  87. if len(self.searchText) == self.cols - 2:
  88. return
  89. self.searchText = self.searchText + chr(c)
  90. self.stdscr.addstr(
  91. self.rows - 1,
  92. 0,
  93. self.searchText + (" " * (self.cols - len(self.searchText) - 2)),
  94. )
  95. self.paintStatus(self.statusText + " %d" % len(self.searchText))
  96. self.stdscr.move(self.rows - 1, len(self.searchText))
  97. self.stdscr.refresh()
  98. def logPrefix(self):
  99. return "CursesStdIO"
  100. def close(self):
  101. """ clean up """
  102. curses.nocbreak()
  103. self.stdscr.keypad(0)
  104. curses.echo()
  105. curses.endwin()
  106. class Callback:
  107. def __init__(self, stdio):
  108. self.stdio = stdio
  109. def on_line(self, text):
  110. self.stdio.print_line(text)
  111. def main(stdscr):
  112. screen = CursesStdIO(stdscr) # create Screen object
  113. callback = Callback(screen)
  114. screen.set_callback(callback)
  115. stdscr.refresh()
  116. reactor.addReader(screen)
  117. reactor.run()
  118. screen.close()
  119. if __name__ == "__main__":
  120. curses.wrapper(main)