diff options
author | Tom <tom@tomforb.es> | 2015-10-30 00:33:16 +0000 |
---|---|---|
committer | Tom <tom@tomforb.es> | 2015-10-30 00:33:16 +0000 |
commit | 885d786b4d48558cbd0b3b9412b9cd66f62bd05a (patch) | |
tree | 5d4f512eecd9495ae04dcc777f6fba2d2eb857b4 | |
parent | e1574bb1c4b201406ca6ab55779b3aa0cbfb2f5f (diff) |
Attempt to read a key to reset the graph. Didn't work.attempt-readkey
-rw-r--r-- | gping/pinger.py | 19 | ||||
-rw-r--r-- | gping/utils.py (renamed from gping/termsize.py) | 49 |
2 files changed, 56 insertions, 12 deletions
diff --git a/gping/pinger.py b/gping/pinger.py index 8c27dc1..2466843 100644 --- a/gping/pinger.py +++ b/gping/pinger.py @@ -6,6 +6,7 @@ import sys import platform import re from itertools import islice +import threading from colorama import Fore, init init() @@ -15,10 +16,10 @@ buff = deque(maxlen=400) Point = namedtuple("Point", "x y") try: - from gping.termsize import get_terminal_size + from gping.utils import get_terminal_size, getch except ImportError: # python 2 compatibility - from termsize import get_terminal_size + from utils import get_terminal_size, getch try: from colorama.ansitowin32 import winterm @@ -311,7 +312,21 @@ def _run(): else: it = linux_ping + def _reset_thread(): + while True: + x = getch() + # This seems to trap ctrl+c :( + if x in ("c", " "): + should_clear.set() + + should_clear = threading.Event() + thread = threading.Thread(target=_reset_thread, daemon=True) + thread.start() + for line in it(options): + if should_clear.is_set(): + buff.clear() + buff.appendleft(line) width, height = get_terminal_size() diff --git a/gping/termsize.py b/gping/utils.py index ebd8014..37783f7 100644 --- a/gping/termsize.py +++ b/gping/utils.py @@ -4,8 +4,35 @@ import shlex import struct import platform import subprocess - - + + +def _find_getch(): + try: + import termios + except ImportError: + # Non-POSIX. Return msvcrt's (Windows') getch. + import msvcrt + return msvcrt.getch + + # POSIX system. Create and return a getch that manipulates the tty. + import sys, tty + + def _getch(): + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(fd) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + + return _getch + + +getch = _find_getch() + + def get_terminal_size(): """ getTerminalSize() - get width and height of console @@ -23,10 +50,10 @@ def get_terminal_size(): if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'): tuple_xy = _get_terminal_size_linux() if tuple_xy is None: - tuple_xy = (80, 25) # default value + tuple_xy = (80, 25) # default value return tuple_xy - - + + def _get_terminal_size_windows(): try: from ctypes import windll, create_string_buffer @@ -45,7 +72,7 @@ def _get_terminal_size_windows(): return sizex, sizey except Exception: pass - + def _get_terminal_size_tput(): # get terminal width @@ -56,8 +83,8 @@ def _get_terminal_size_tput(): return (cols, rows) except Exception: pass - - + + def _get_terminal_size_linux(): def ioctl_GWINSZ(fd): try: @@ -68,6 +95,7 @@ def _get_terminal_size_linux(): return cr except Exception: pass + cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) if not cr: try: @@ -82,7 +110,8 @@ def _get_terminal_size_linux(): except Exception: return None return int(cr[1]), int(cr[0]) - + + if __name__ == "__main__": sizex, sizey = get_terminal_size() - print("Width: {}, Height: {}".format(sizex, sizey))
\ No newline at end of file + print("Width: {}, Height: {}".format(sizex, sizey)) |