cursesio.py 4.2 KB

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