Skip to content

Instantly share code, notes, and snippets.

@nakamuray
Created September 21, 2012 05:02
Show Gist options
  • Save nakamuray/3759809 to your computer and use it in GitHub Desktop.
Save nakamuray/3759809 to your computer and use it in GitHub Desktop.
ここまで読んだ
#!/usr/bin/python
# vim: fileencoding=utf-8
'''ここまで読んだ
標準入力を読み出し、標準出力に書き込む。
エンターが押されたら、マーカーを表示する。
'''
import os
import sys
import tty
from threading import Thread
def find_tty():
for f in (sys.stderr, sys.stdout, sys.stdin):
if f.isatty():
ttyname = os.ttyname(f.fileno())
break
else:
raise Exception("can't find tty")
return ttyname
def cat(input, output):
#for buf in iter(lambda: input.read(4096), ''):
for buf in iter(input.readline, ''):
output.write(buf)
output.flush()
def marking():
t = find_tty()
f = open(t, 'r')
# disable tty echo back
tty.setcbreak(f.fileno())
# to avoid buffering, use iter
for _ in iter(f.readline, ''):
print bold(u'---- ここまで読んだ ----').encode('utf-8')
sys.stdout.flush()
def bold(text):
return u'\x1b[1m' + text + u'\x1b[22m'
def main():
try:
t = Thread(target=marking)
t.setDaemon(True)
t.start()
cat(sys.stdin, sys.stdout)
except KeyboardInterrupt:
pass
except IOError, e:
if e.errno == 32:
# Broken Pipe
pass
else:
raise
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment