aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorvg <vgm+dev@devys.org>2024-04-24 17:48:16 +0200
committervg <vgm+dev@devys.org>2024-04-24 17:48:16 +0200
commitdb166f4847e68417b1938d9a451d449a1726f894 (patch)
tree37a032f45f87ab3e9808895e0259943dd7ca8a86 /scripts
parente95f2f6ad1f174f0f1a3b86f030cdfb86be13a67 (diff)
downloadscripts-db166f4847e68417b1938d9a451d449a1726f894.tar.gz
scripts-db166f4847e68417b1938d9a451d449a1726f894.tar.bz2
scripts-db166f4847e68417b1938d9a451d449a1726f894.zip
improve display of fancy_sleep
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/fancy_sleep.py52
1 files changed, 30 insertions, 22 deletions
diff --git a/scripts/fancy_sleep.py b/scripts/fancy_sleep.py
index e65ad94..342f4c4 100755
--- a/scripts/fancy_sleep.py
+++ b/scripts/fancy_sleep.py
@@ -117,6 +117,7 @@ NUMBERS = [i.replace('.', ' ') for i in (
.
""",
)]
+NUMBERS_LINES = 5
def print_translated(string):
@@ -188,39 +189,44 @@ def test_parse_time():
assert parse_time('8d 6s') == parse_time('8d6s')
+def get_hms_from_secs(seconds):
+ hours = int(seconds / 3600)
+ minutes = int(seconds / 60) % 60
+ seconds = int(seconds) % 60
+ return hours, minutes, seconds
+
+
+def get_hms_string_from_secs(seconds):
+ hours, minutes, seconds = get_hms_from_secs(seconds)
+ time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
+ return time_string
+
+
def fancy_sleep_display(target_time):
- print()
- print('\n'*5, '\x1b[5A', sep='', end='')
- original_termios_attr = termios.tcgetattr(sys.stdin)
+ original_termios_attr = termios.tcgetattr(sys.stdout)
noecho_termios_attr = original_termios_attr[:]
noecho_termios_attr[3] &= ~termios.ECHO
- termios.tcsetattr(sys.stdin, termios.TCSANOW, noecho_termios_attr)
+ termios.tcsetattr(sys.stdout, termios.TCSANOW, noecho_termios_attr)
try:
while True:
- delta = target_time.timestamp() - time.time()
+ delta = target_time - time.time()
if delta <= 0:
break
- hours = int(delta / 3600)
- minutes = int(delta / 60) % 60
- seconds = int(delta) % 60
-
- print('\x1b[s', end='') # save cursor
- time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
- #print('\rremainining time: %s' % time_string, end='')
- print_translated(time_string)
- print('\x1b[u', end='') # restore cursor
-
+ #print('\x1b[s', end='') # save cursor
+ print_translated(get_hms_string_from_secs(delta))
+ print(f'\x1b[{NUMBERS_LINES}A', end='') # go up 5 lines up
+ #print('\x1b[u', end='') # restore cursor
time.sleep(1)
finally:
- print()
- termios.tcsetattr(sys.stdin, termios.TCSANOW, original_termios_attr)
+ print('\x1b[5B', end='') # go up 5 lines down
+ termios.tcsetattr(sys.stdout, termios.TCSANOW, original_termios_attr)
def main():
'function called only when script invoked directly on command line'
args = docopt.docopt(__doc__)
-
- curtime = datetime.datetime.now()
+ isatty = sys.stdout.isatty()
+ curtime = datetime.datetime.now().replace(microsecond=0)
timedelta = parse_time(args['TIME'])
target_time = curtime + timedelta
@@ -228,17 +234,19 @@ def main():
print('ERROR: TIME is unparsable or equals to 0', file=sys.stderr)
sys.exit(1)
- #print('Launched at:', curtime.strftime('%F %T'))
if args['--target']:
print('Run until:', target_time.strftime('%F %T'))
sys.stdout.flush()
try:
- if not sys.stdout.isatty():
+ if not isatty:
time.sleep(target_time.timestamp() - time.time())
else:
- fancy_sleep_display(target_time)
+ fancy_sleep_display(int(target_time.timestamp()))
except KeyboardInterrupt:
+ if isatty:
+ print('slept for', get_hms_string_from_secs(
+ time.time() - int(curtime.timestamp())))
sys.exit(130)
if args['COMMAND']: