cursesio.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 based on internal list of lines"""
  49. self.stdscr.clear()
  50. self.paintStatus(self.statusText)
  51. i = 0
  52. index = len(self.lines) - 1
  53. while i < (self.rows - 3) and index >= 0:
  54. self.stdscr.addstr(self.rows - 3 - i, 0, self.lines[index], curses.A_NORMAL)
  55. i = i + 1
  56. index = index - 1
  57. self.printLogLine(self.logLine)
  58. self.stdscr.refresh()
  59. def paintStatus(self, text):
  60. if len(text) > self.cols:
  61. raise RuntimeError("TextTooLongError")
  62. self.stdscr.addstr(
  63. self.rows - 2, 0, text + " " * (self.cols - len(text)), curses.A_STANDOUT
  64. )
  65. def printLogLine(self, text):
  66. self.stdscr.addstr(
  67. 0, 0, text + " " * (self.cols - len(text)), curses.A_STANDOUT
  68. )
  69. def doRead(self):
  70. """Input is ready!"""
  71. curses.noecho()
  72. c = self.stdscr.getch() # read a character
  73. if c == curses.KEY_BACKSPACE:
  74. self.searchText = self.searchText[:-1]
  75. elif c == curses.KEY_ENTER or c == 10:
  76. text = self.searchText
  77. self.searchText = ""
  78. self.print_line(">> %s" % text)
  79. try:
  80. if self.callback:
  81. self.callback.on_line(text)
  82. except Exception as e:
  83. self.print_line(str(e))
  84. self.stdscr.refresh()
  85. elif isprint(c):
  86. if len(self.searchText) == self.cols - 2:
  87. return
  88. self.searchText = self.searchText + chr(c)
  89. self.stdscr.addstr(
  90. self.rows - 1,
  91. 0,
  92. self.searchText + (" " * (self.cols - len(self.searchText) - 2)),
  93. )
  94. self.paintStatus(self.statusText + " %d" % len(self.searchText))
  95. self.stdscr.move(self.rows - 1, len(self.searchText))
  96. self.stdscr.refresh()
  97. def logPrefix(self):
  98. return "CursesStdIO"
  99. def close(self):
  100. """clean up"""
  101. curses.nocbreak()
  102. self.stdscr.keypad(0)
  103. curses.echo()
  104. curses.endwin()
  105. class Callback:
  106. def __init__(self, stdio):
  107. self.stdio = stdio
  108. def on_line(self, text):
  109. self.stdio.print_line(text)
  110. def main(stdscr):
  111. screen = CursesStdIO(stdscr) # create Screen object
  112. callback = Callback(screen)
  113. screen.set_callback(callback)
  114. stdscr.refresh()
  115. reactor.addReader(screen)
  116. reactor.run()
  117. screen.close()
  118. if __name__ == "__main__":
  119. curses.wrapper(main)