Commit f47f7997 authored by Antti Tanhuanpää's avatar Antti Tanhuanpää

Interactive mode

parent 657888e5
......@@ -4,6 +4,7 @@ from os import path
import shlex
import subprocess
import sys
import termios
linters = {
......@@ -17,6 +18,81 @@ class NoLinterFound(Exception):
pass
class Terminal:
class ReadChar:
def __init__(self, file):
self.file = file
def readchar(self, default=None):
b = self.file.read(1)
c = b.decode()
if c.isprintable():
return c
return default
def __init__(self, dev='/dev/tty'):
self.dev = dev
self.file = None
self.tcattrs = None
def __enter__(self):
self.file = open(self.dev, 'rb', buffering=0)
try:
self.set_non_canon()
except Exception as exc:
self.reset()
raise exc
return self.ReadChar(self.file)
def __exit__(self, exc_type, exc_value, exc_tb):
self.reset()
return False
def reset(self):
file = self.file
tcattrs = self.tcattrs
self.file = None
self.tcattrs = None
if file:
if tcattrs:
self.set_tcattrs(tcattrs, fd=file.fileno())
file.close()
def set_non_canon(self):
fd = self.file.fileno()
self.tcattrs = termios.tcgetattr(fd)
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = self.tcattrs
lflag &= ~(termios.ECHO | termios.ICANON)
self.set_tcattrs([iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
def set_tcattrs(self, attrs, fd=None):
if fd is None:
fd = self.file.fileno()
termios.tcsetattr(fd, termios.TCSAFLUSH, attrs)
def get_files():
files = []
while True:
line = sys.stdin.readline()
if not line:
break
filename = line.strip()
files.append(filename)
return files
def get_linter(filename):
try:
_, extension = filename.rsplit('.', 1)
......@@ -45,17 +121,67 @@ def lint(filename, require_linter=False):
return ret == 0
def main(*args):
ret = True
while True:
line = sys.stdin.readline()
if not line:
break
def lint_all(files):
success = False
filename = line.strip()
ret &= lint(filename)
while not success:
success = True
for filename in files:
success &= lint(filename)
if not success:
retry, skip = prompt_for_retry()
if not retry:
success = skip
break
print(file=sys.stderr)
return success
def prompt_for_retry():
with Terminal() as term:
while True:
print('(A)bort, (I)gnore, (R)etry: ',
end='', file=sys.stderr, flush=True)
ans = term.readchar('r')
print(ans, file=sys.stderr)
try:
return {
'a': (False, False),
'i': (False, True),
'r': (True, False),
}[ans.lower()]
except KeyError:
pass
def main():
from signal import signal, SIGINT, SIGTERM
class Exit(Exception):
pass
exit_signals = (SIGTERM, SIGINT)
def sig_handler(signum, frame):
if signum in exit_signals:
raise Exit
for signum in exit_signals:
signal(signum, sig_handler)
try:
files = get_files()
success = lint_all(files)
except Exit:
success = False
return 1 ^ success
return 1 ^ ret
if __name__ == '__main__':
sys.exit(main(*sys.argv[1:]))
sys.exit(main())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment