Update nn
Direct input into Y/N questions
This commit is contained in:
@@ -6,6 +6,8 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import termios
|
||||||
|
import tty
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -273,13 +275,23 @@ def apply_renames(plan):
|
|||||||
|
|
||||||
|
|
||||||
def confirm(prompt='Apply renames? [y/n] '):
|
def confirm(prompt='Apply renames? [y/n] '):
|
||||||
"""Ask the user for confirmation."""
|
"""Ask the user for confirmation, reacting to a single keypress."""
|
||||||
while True:
|
print(prompt, end='', flush=True)
|
||||||
answer = input(prompt).strip().lower()
|
if not sys.stdin.isatty():
|
||||||
if answer == 'y':
|
return sys.stdin.read(1).strip().lower() == 'y'
|
||||||
return True
|
|
||||||
if answer == 'n':
|
fd = sys.stdin.fileno()
|
||||||
return False
|
old_settings = termios.tcgetattr(fd)
|
||||||
|
try:
|
||||||
|
# cbreak (not raw) keeps Ctrl-C as SIGINT and newlines well behaved.
|
||||||
|
tty.setcbreak(fd)
|
||||||
|
while (key := sys.stdin.read(1).lower()) not in ('y', 'n', ''):
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
||||||
|
|
||||||
|
print(key)
|
||||||
|
return key == 'y'
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -337,4 +349,8 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('\nAborted.')
|
||||||
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user