瀏覽代碼

implemented curses_tty_wrapper()

Fabian Peter Hammerle 9 年之前
父節點
當前提交
637685826b
共有 1 個文件被更改,包括 18 次插入0 次删除
  1. 18 0
      ioex/__init__.py

+ 18 - 0
ioex/__init__.py

@@ -1,4 +1,22 @@
 import os
+import sys
+
+def curses_tty_wrapper(function, *args):
+    stdin_fd = os.dup(sys.stdin.fileno())
+    stdout_fd = os.dup(sys.stdout.fileno())
+    # set stdin to tty
+    tty_in = open('/dev/tty', 'r')
+    os.dup2(tty_in.fileno(), sys.stdin.fileno())
+    # set stdout to tty
+    tty_out = open('/dev/tty', 'w')
+    os.dup2(tty_out.fileno(), sys.stdout.fileno())
+    # os.system('ls -l /proc/self/fd/')
+    import curses
+    result = curses.wrapper(function, *args)
+    # reset stdin and stdout
+    os.dup2(stdin_fd, sys.stdin.fileno())
+    os.dup2(stdout_fd, sys.stdout.fileno())
+    return result
 
 def raw_input_with_default(prompt, default):
     import readline