From 10b51405af4d56172791e0e4c6a322317f13d4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 13 Apr 2024 22:24:02 +0200 Subject: [PATCH] Refactored multi-platform handling --- CB/Compat.py | 18 ---------------- CurseBreaker.py | 44 +++++++++++++++++++++------------------ Pipfile.lock | 55 ++++++++++++++++++++++++++++++------------------- 3 files changed, 58 insertions(+), 59 deletions(-) diff --git a/CB/Compat.py b/CB/Compat.py index 70714f4..66d0020 100644 --- a/CB/Compat.py +++ b/CB/Compat.py @@ -36,24 +36,6 @@ def kbhit(self): return dr != [] -def pause(headless): - if headless: - return - elif system == 'Windows': - os.system('pause') - else: - os.system('/bin/bash -c "read -rsp $\'Press any key to continue . . .\n\' -n 1"') - - -def timeout(headless): - if headless: - return - elif system == 'Windows': - os.system('timeout /t 5') - else: - os.system('/bin/bash -c "read -rsp $\'Waiting for 5 seconds, press a key to continue ...\n\' -n 1 -t 5"') - - def clear(): if system == 'Windows': os.system('cls') diff --git a/CurseBreaker.py b/CurseBreaker.py index f2fc541..cba5863 100644 --- a/CurseBreaker.py +++ b/CurseBreaker.py @@ -41,7 +41,7 @@ from CB import __version__ from CB.Core import Core from CB.Wago import WagoUpdater -from CB.Compat import pause, timeout, clear, set_terminal_title, set_terminal_size, KBHit +from CB.Compat import clear, set_terminal_title, set_terminal_size, KBHit from CB.Resources import LOGO, HEADLESS_TERMINAL_THEME if platform.system() == 'Windows': @@ -145,9 +145,8 @@ def start(self): # sourcery skip: low-code-quality logo = Pixels.from_image(image, resize=(40, 40)) self.console.print(Panel(Align.center(logo), border_style='yellow')) self.console.print('') - with Live(Text('Automatic update of all addons will start in 5 seconds.\nPress any button to enter inte' - 'ractive mode.'), console=self.console, refresh_per_second=2) as countdown: - keypress = self.handle_keypress(5, countdown) + keypress = self.handle_keypress('Automatic update of all addons will start in {} seconds.\nPress any bu' + 'tton to enter interactive mode.', 5, True) else: keypress = False if not keypress: @@ -174,9 +173,8 @@ def start(self): # sourcery skip: low-code-quality sys.exit(0) else: self.print_author_reminder() - self.console.print('Press [bold]I[/bold] to enter interactive mode or any other button to close' - ' the application.') - keypress = self.handle_keypress(0) + keypress = self.handle_keypress('Press [bold]I[/bold] to enter interactive mode or any other button' + ' to close the application.', 0, False) if not keypress or keypress.lower() not in [b'i', 'i']: self.core.http.close() sys.exit(0) @@ -224,7 +222,7 @@ def start(self): # sourcery skip: low-code-quality def _auto_update_cleanup(self): self.print_log() self.core.http.close() - pause(self.headless) + self.handle_keypress('Press any button to continue...', 0, False) def _auto_update_install(self, url, changelog): self.console.print('[green]Updating CurseBreaker...[/green]') @@ -284,7 +282,7 @@ def motd_parser(self): else: payload = self.core.http.get('https://cursebreaker.acidweb.dev/motd') if payload.status_code == 200: - self.console.print(Panel(payload.content.decode('UTF-8'), title=':warning: MOTD :warning:', + self.console.print(Panel(payload.content.decode('UTF-8'), title=':megaphone: MOTD :megaphone:', border_style='red')) self.console.print('') @@ -304,35 +302,41 @@ def handle_exception(self, e, table=True): self.console.print(Traceback.from_exception(exc_type=e.__class__, exc_value=e, traceback=e.__traceback__, width=width)) - def handle_countdown(self, n): - msg = (f'Automatic update of all addons will start in {math.ceil(n)} seconds.\n' - f'Press any button to enter interactive mode.') - return Text(msg) - - def handle_keypress(self, wait, countdown=None): + def _handle_keypress_console(self, count, msg, countdown=None): kb = KBHit() starttime = time.time() - keypress = None + keypress = False while True: if kb.kbhit(): keypress = kb.getch() break - elif wait and time.time() - starttime > wait: + elif count and time.time() - starttime > count: break if countdown: - countdown.update(self.handle_countdown(wait - (time.time() - starttime))) + countdown.update(Text(msg.format(math.ceil(count - (time.time() - starttime))))) time.sleep(0.01) kb.set_normal_term() return keypress + def handle_keypress(self, msg, count, live): + if self.headless: + return False + if live: + with Live(Text(msg.format(count)), console=self.console, refresh_per_second=2) as countdown: + keypress = self._handle_keypress_console(count, msg, countdown) + else: + self.console.print(msg) + keypress = self._handle_keypress_console(count, msg) + return keypress + def handle_shutdown(self, message=''): self.core.http.close() if not message: - timeout(self.headless) + self.handle_keypress('\nWaiting for {} seconds, press any button to continue...', 5, True) sys.exit(0) else: self.console.print(message) - pause(self.headless) + self.handle_keypress('\nPress any button to continue...', 0, False) sys.exit(1) def print_header(self): diff --git a/Pipfile.lock b/Pipfile.lock index 6ecf078..cdb26ba 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "61e47a56b2d84b3d8c47b91b666ba7bd11dca251e4fdcec75ad1f610848a66b4" + "sha256": "cbbe80124d3cbae3a7e8ebedf97d37f05ffac22b0c11948a034fa29170859118" }, "pipfile-spec": 6, "requires": { @@ -129,9 +129,13 @@ "version": "==2024.2.2" }, "checksumdir": { - "git": "git+https://github.com/idahogray/checksumdir", + "hashes": [ + "sha256:10bfd7518da5a14b0e9ac03e9ad105f0e70f58bba52b6e9aa2f21a3f73c7b5a8", + "sha256:77687e16da95970c94061c74ef2e13666c4b6e0e8c90a5eaf0c8f7591332cf01" + ], + "index": "pypi", "markers": "python_version >= '3.6' and python_version < '4.0'", - "ref": "d493ef3f81f46f65919916184ca7908bd644a57f" + "version": "==1.2.0" }, "h11": { "hashes": [ @@ -352,6 +356,15 @@ "markers": "python_version >= '3.8'", "version": "==3.0.1" }, + "setuptools": { + "hashes": [ + "sha256:6c1fccdac05a97e598fb0ae3bbed5904ccb317337a51139dcd51453611bbb987", + "sha256:c636ac361bc47580504644275c9ad802c50415c7522212252c033bd15f301f32" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==69.5.1" + }, "six": { "hashes": [ "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", @@ -379,27 +392,27 @@ "develop": { "ruff": { "hashes": [ - "sha256:26071fb530038602b984e3bbe1443ef82a38450c4dcb1344a9caf67234ff9756", - "sha256:28ccf3fb6d1162a73cd286c63a5e4d885f46a1f99f0b392924bc95ccbd18ea8f", - "sha256:2b0c4c70578ef1871a9ac5c85ed7a8c33470e976c73ba9211a111d2771b5f787", - "sha256:4056480f5cf38ad278667c31b0ef334c29acdfcea617cb89c4ccbc7d96f1637f", - "sha256:647f1fb5128a3e24ce68878b8050bb55044c45bb3f3ae4710d4da9ca96ede5cb", - "sha256:732ef99984275534f9466fbc01121523caf72aa8c2bdeb36fd2edf2bc294a992", - "sha256:7c8a2a0e0cab077a07465259ffe3b3c090e747ca8097c5dc4c36ca0fdaaac90d", - "sha256:878ef1a55ce931f3ca23b690b159cd0659f495a4c231a847b00ca55e4c688baf", - "sha256:93699d61116807edc5ca1cdf9d2d22cf8d93335d59e3ff0ca7aee62c1818a736", - "sha256:b11e09439d9df6cc12d9f622065834654417c40216d271f639512d80e80e3e53", - "sha256:b2e79f8e1b6bd5411d7ddad3f2abff3f9d371beda29daef86400d416dedb7e02", - "sha256:c466a52c522e6a08df0af018f550902f154f5649ad09e7f0d43da766e7399ebc", - "sha256:cf48ec2c4bfae7837dc325c431a2932dc23a1485e71c59591c1df471ba234e0e", - "sha256:e3da499ded004d0b956ab04248b2ae17e54a67ffc81353514ac583af5959a255", - "sha256:ecb87788284af96725643eae9ab3ac746d8cc09aad140268523b019f7ac3cd98", - "sha256:f1aa621beed533f46e9c7d6fe00e7f6e4570155b61d8f020387b72ace2b42e04", - "sha256:fc4006cbc6c11fefc25f122d2eb4731d7a3d815dc74d67c54991cc3f99c90177" + "sha256:0e2e06459042ac841ed510196c350ba35a9b24a643e23db60d79b2db92af0c2b", + "sha256:1f32cadf44c2020e75e0c56c3408ed1d32c024766bd41aedef92aa3ca28eef68", + "sha256:22e306bf15e09af45ca812bc42fa59b628646fa7c26072555f278994890bc7ac", + "sha256:24016ed18db3dc9786af103ff49c03bdf408ea253f3cb9e3638f39ac9cf2d483", + "sha256:33bcc160aee2520664bc0859cfeaebc84bb7323becff3f303b8f1f2d81cb4edc", + "sha256:3afabaf7ba8e9c485a14ad8f4122feff6b2b93cc53cd4dad2fd24ae35112d5c5", + "sha256:5ec481661fb2fd88a5d6cf1f83403d388ec90f9daaa36e40e2c003de66751798", + "sha256:652e4ba553e421a6dc2a6d4868bc3b3881311702633eb3672f9f244ded8908cd", + "sha256:6a2243f8f434e487c2a010c7252150b1fdf019035130f41b77626f5655c9ca22", + "sha256:6ab165ef5d72392b4ebb85a8b0fbd321f69832a632e07a74794c0e598e7a8376", + "sha256:7891ee376770ac094da3ad40c116258a381b86c7352552788377c6eb16d784fe", + "sha256:799eb468ea6bc54b95527143a4ceaf970d5aa3613050c6cff54c85fda3fde480", + "sha256:82986bb77ad83a1719c90b9528a9dd663c9206f7c0ab69282af8223566a0c34e", + "sha256:8772130a063f3eebdf7095da00c0b9898bd1774c43b336272c3e98667d4fb8fa", + "sha256:8d14dc8953f8af7e003a485ef560bbefa5f8cc1ad994eebb5b12136049bbccc5", + "sha256:cbd1e87c71bca14792948c4ccb51ee61c3296e164019d2d484f3eaa2d360dfaf", + "sha256:ec4ba9436a51527fb6931a8839af4c36a5481f8c19e8f5e42c2f7ad3a49f5069" ], "index": "pypi", "markers": "python_version >= '3.7'", - "version": "==0.3.6" + "version": "==0.4.2" }, "sourcery": { "hashes": [