Skip to content

Commit 61de86c

Browse files
authored
Merge pull request #188 from trimclain/yasbc-silent-flag
feat(cli): add `--silent` option
2 parents 4d9664b + 136b935 commit 61de86c

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

docs/CLI.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ The YASB CLI is a command line interface that allows you to interact with the YA
1717

1818
## Options
1919
- `--help` - Show the help message for the command.
20+
- `--silent` - Disable print messages for `start`, `stop` and `reload`
2021
- `--version` - Show the YASB version.
2122

23+
> **Note:**
24+
> You can use the `--silent` option with the `start`, `stop` and `reload` commands to prevent non-error messages from being displayed.
25+
2226
## Autostart
2327

2428
To enable autostart for the status bar on system boot, use the following command:

src/core/utils/cli.py

+46-37
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
just_fix_windows_console()
2323

2424
YASB_VERSION = BUILD_VERSION
25-
YASB_CLI_VERSION = "1.0.8"
25+
YASB_CLI_VERSION = "1.0.9"
2626

2727
OS_STARTUP_FOLDER = os.path.join(os.environ['APPDATA'], r'Microsoft\Windows\Start Menu\Programs\Startup')
2828
INSTALLATION_PATH = os.path.abspath(os.path.join(__file__, "../../.."))
@@ -36,7 +36,7 @@ def is_process_running(process_name):
3636
if proc.info['name'] == process_name:
3737
return True
3838
return False
39-
39+
4040
class Format:
4141
end = '\033[0m'
4242
underline = '\033[4m'
@@ -119,7 +119,7 @@ def _enable_startup():
119119
except Exception as e:
120120
logging.error(f"Failed to create startup shortcut: {e}")
121121
print(f"Failed to create startup shortcut: {e}")
122-
122+
123123
def _disable_startup():
124124
shortcut_path = os.path.join(OS_STARTUP_FOLDER, SHORTCUT_FILENAME)
125125
if os.path.exists(shortcut_path):
@@ -130,55 +130,64 @@ def _disable_startup():
130130
except Exception as e:
131131
logging.error(f"Failed to remove startup shortcut: {e}")
132132
print(f"Failed to remove startup shortcut: {e}")
133-
134-
133+
134+
135135
def parse_arguments():
136136
parser = CustomArgumentParser(description="The command-line interface for YASB Reborn.", add_help=False)
137137
subparsers = parser.add_subparsers(dest='command', help='Commands')
138138

139-
subparsers.add_parser('start', help='Start the application')
140-
subparsers.add_parser('stop', help='Stop the application')
141-
subparsers.add_parser('reload', help='Reload the application')
139+
start_parser = subparsers.add_parser('start', help='Start the application')
140+
start_parser.add_argument('-s', '--silent', action='store_true', help='Silence print messages')
141+
142+
stop_parser = subparsers.add_parser('stop', help='Stop the application')
143+
stop_parser.add_argument('-s', '--silent', action='store_true', help='Silence print messages')
144+
145+
reload_parser = subparsers.add_parser('reload', help='Reload the application')
146+
reload_parser.add_argument('-s', '--silent', action='store_true', help='Silence print messages')
147+
142148
subparsers.add_parser('update', help='Update the application')
143-
149+
144150
enable_autostart_parser = subparsers.add_parser('enable-autostart', help='Enable autostart on system boot')
145151
enable_autostart_parser.add_argument('--task', action='store_true', help='Enable autostart as a scheduled task')
146-
152+
147153
disable_autostart_parser = subparsers.add_parser('disable-autostart', help='Disable autostart on system boot')
148154
disable_autostart_parser.add_argument('--task', action='store_true', help='Disable autostart as a scheduled task')
149-
155+
150156
subparsers.add_parser('help', help='Show help message')
151157
subparsers.add_parser('log', help='Tail yasb process logs (cancel with Ctrl-C)')
152158
parser.add_argument('-v', '--version', action='store_true', help="Show program's version number and exit.")
153159
parser.add_argument('-h', '--help', action='store_true', help='Show help message')
154160
args = parser.parse_args()
155-
161+
156162
if args.command == 'start':
157-
print(f"Start YASB Reborn v{YASB_VERSION} in background.")
158-
print("\n# Community")
159-
print("* Join the Discord https://discord.gg/qkeunvBFgX - Chat, ask questions, share your desktops and more...")
160-
print("* GitHub discussions https://github.com/amnweb/yasb/discussions - Ask questions, share your ideas and more...")
161-
print("\n# Documentation")
162-
print("* Read the docs https://github.com/amnweb/yasb/wiki - how to configure and use YASB")
163+
if not args.silent:
164+
print(f"Start YASB Reborn v{YASB_VERSION} in background.")
165+
print("\n# Community")
166+
print("* Join the Discord https://discord.gg/qkeunvBFgX - Chat, ask questions, share your desktops and more...")
167+
print("* GitHub discussions https://github.com/amnweb/yasb/discussions - Ask questions, share your ideas and more...")
168+
print("\n# Documentation")
169+
print("* Read the docs https://github.com/amnweb/yasb/wiki - how to configure and use YASB")
163170
subprocess.Popen(["yasb.exe"])
164171
sys.exit(0)
165-
172+
166173
elif args.command == 'stop':
167-
print("Stop YASB...")
174+
if not args.silent:
175+
print("Stop YASB...")
168176
CLIHandler.stop_or_reload_application()
169177
sys.exit(0)
170-
178+
171179
elif args.command == 'reload':
172180
if is_process_running("yasb.exe"):
173-
print("Reload YASB...")
181+
if not args.silent:
182+
print("Reload YASB...")
174183
CLIHandler.stop_or_reload_application(reload=True)
175184
else:
176185
print("YASB is not running. Reload aborted.")
177186
sys.exit(0)
178187
elif args.command == 'update':
179188
CLIUpdateHandler.update_yasb(YASB_VERSION)
180189
sys.exit(0)
181-
190+
182191
elif args.command == 'enable-autostart':
183192
if args.task:
184193
if not CLITaskHandler.is_admin():
@@ -188,7 +197,7 @@ def parse_arguments():
188197
else:
189198
CLIHandler._enable_startup()
190199
sys.exit(0)
191-
200+
192201
elif args.command == 'disable-autostart':
193202
if args.task:
194203
if not CLITaskHandler.is_admin():
@@ -197,8 +206,8 @@ def parse_arguments():
197206
CLITaskHandler.delete_task()
198207
else:
199208
CLIHandler._disable_startup()
200-
sys.exit(0)
201-
209+
sys.exit(0)
210+
202211
elif args.command == 'log':
203212
config_home = os.getenv('YASB_CONFIG_HOME') if os.getenv('YASB_CONFIG_HOME') else os.path.join(os.path.expanduser("~"), ".config", "yasb")
204213
log_file = os.path.join(config_home, "yasb.log")
@@ -218,7 +227,7 @@ def parse_arguments():
218227
except KeyboardInterrupt:
219228
pass
220229
sys.exit(0)
221-
230+
222231
elif args.command == 'help' or args.help:
223232
print("The command-line interface for YASB Reborn.")
224233
print('\n' + Format.underline + 'Usage' + Format.end + ': yasbc <COMMAND>')
@@ -235,7 +244,7 @@ def parse_arguments():
235244
print("-v, --version Print version")
236245
print("-h, --help Print this message")
237246
sys.exit(0)
238-
247+
239248
elif args.version:
240249
version_message = f"YASB Reborn v{YASB_VERSION}\nYASB-CLI v{YASB_CLI_VERSION}"
241250
print(version_message)
@@ -246,7 +255,7 @@ def parse_arguments():
246255

247256

248257
class CLITaskHandler:
249-
258+
250259
def is_admin():
251260
try:
252261
return ctypes.windll.shell32.IsUserAnAdmin()
@@ -259,7 +268,7 @@ def get_current_user_sid():
259268
user, domain, type = win32security.LookupAccountName(None, username)
260269
sid = win32security.ConvertSidToStringSid(user)
261270
return sid
262-
271+
263272
def create_task():
264273
scheduler = win32com.client.Dispatch('Schedule.Service')
265274
scheduler.Connect()
@@ -308,7 +317,7 @@ def create_task():
308317
print(f"Task YASB Reborn created successfully.")
309318
except Exception as e:
310319
print(f"Failed to create task YASB Reborn. Error: {e}")
311-
320+
312321
def delete_task():
313322
scheduler = win32com.client.Dispatch('Schedule.Service')
314323
scheduler.Connect()
@@ -318,7 +327,7 @@ def delete_task():
318327
print(f"Task YASB Reborn deleted successfully.")
319328
except Exception:
320329
print(f"Failed to delete task YASB or task does not exist.")
321-
330+
322331
class CLIUpdateHandler():
323332

324333
def get_installed_product_code():
@@ -335,7 +344,7 @@ def get_installed_product_code():
335344
return product_code.value
336345
index += 1
337346
return None
338-
347+
339348
def update_yasb(YASB_VERSION):
340349
# Fetch the latest tag from the GitHub API
341350
api_url = f"https://api.github.com/repos/amnweb/yasb/releases/latest"
@@ -376,7 +385,7 @@ def update_yasb(YASB_VERSION):
376385
except KeyboardInterrupt:
377386
print("\nDownload interrupted by user.")
378387
sys.exit(0)
379-
388+
380389
# Verify the downloaded file size
381390
downloaded_size = os.path.getsize(msi_path)
382391
if downloaded_size != total_length:
@@ -394,7 +403,7 @@ def update_yasb(YASB_VERSION):
394403
# uninstall_command = f'msiexec /x {product_code} /passive'
395404
# else:
396405
# uninstall_command = ""
397-
406+
398407
# Construct the install command as a string
399408
install_command = f'msiexec /i "{os.path.abspath(msi_path)}" /passive /norestart'
400409
run_after_command = f'"{EXE_PATH}"'
@@ -403,7 +412,7 @@ def update_yasb(YASB_VERSION):
403412
# Finally run update and restart the application
404413
subprocess.Popen(combined_command, shell=True)
405414
sys.exit(0)
406-
415+
407416
if __name__ == "__main__":
408417
CLIHandler.parse_arguments()
409-
sys.exit(0)
418+
sys.exit(0)

0 commit comments

Comments
 (0)