Compare commits

...

7 Commits

Author SHA1 Message Date
10cb9777fa add 0.11.0 to CHANGELOG 2025-06-22 02:57:31 +01:00
c48f7a49ac add Style section to README 2025-06-22 02:54:42 +01:00
b14d9b7610 ensure studio mode is disabled on test cleanup 2025-06-22 02:54:08 +01:00
864751ecc9 remove terminaltables3 dep, add rich 2025-06-22 02:53:46 +01:00
c02ffac403 replace terminaltables with rich tables.
allow rich to handle all console output.

util.check_mark is now used to pass back colourless check/cross marks if NO_COLOR is set or --style/SLOBS_STYLE was not set.
2025-06-22 02:52:27 +01:00
6bcdd8391c add --style and --no-border flags to root command 2025-06-22 02:39:28 +01:00
b0d311dad9 define styles 2025-06-22 02:38:56 +01:00
18 changed files with 543 additions and 166 deletions

View File

@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# [0.11.0] - 2025-06-22
### Added
- Various colouring styles, see [Style](https://github.com/onyx-and-iris/slobs-cli/tree/main?tab=readme-ov-file#style)
- colouring is applied to list tables as well as highlighted information in stdout/stderr output.
- table border styling may be optionally disabled with the --no-border flag.
# [0.10.0] - 2025-06-13
### Changed

View File

@ -293,6 +293,34 @@ slobs-cli scenecollection load "ExistingCollection"
slobs-cli scenecollection rename "ExistingCollection" "NewName"
```
## Style
By default styling is disabled but you may enable it with:
- --style/-s: Style used in output.
- SLOBS_STYLE
- --no-border/-b: Disable table border styling in output.
- SLOBS_STYLE_NO_BORDER
Available styles:
- red
- magenta
- purple
- blue
- cyan
- green
- yellow
- orange
- white
- grey
- navy
- black
```console
slobs-cli --style=cyan --no-border scene list
```
## Special Thanks
- [Julian-0](https://github.com/Julian-O) For writing the [PySLOBS wrapper](https://github.com/Julian-O/PySLOBS) on which this CLI depends.

118
pdm.lock generated
View File

@ -5,7 +5,7 @@
groups = ["default", "dev"]
strategy = ["inherit_metadata"]
lock_version = "4.5.0"
content_hash = "sha256:c1f6a22d9f4fca9c52692b2931ca64dada84a1e99c9013dad4be26bdd786cc6e"
content_hash = "sha256:9be0832aae27a9da3f885900d367836aa05a12e9c0459d751c319d1bd329c33c"
[[metadata.targets]]
requires_python = ">=3.11"
@ -45,13 +45,13 @@ files = [
[[package]]
name = "cachetools"
version = "6.0.0"
version = "6.1.0"
requires_python = ">=3.9"
summary = "Extensible memoizing collections and decorators"
groups = ["dev"]
files = [
{file = "cachetools-6.0.0-py3-none-any.whl", hash = "sha256:82e73ba88f7b30228b5507dce1a1f878498fc669d972aef2dde4f3a3c24f103e"},
{file = "cachetools-6.0.0.tar.gz", hash = "sha256:f225782b84438f828328fc2ad74346522f27e5b1440f4e9fd18b20ebfd1aa2cf"},
{file = "cachetools-6.1.0-py3-none-any.whl", hash = "sha256:1c7bb3cf9193deaf3508b7c5f2a79986c13ea38965c5adcff1f84519cf39163e"},
{file = "cachetools-6.1.0.tar.gz", hash = "sha256:b4c4f404392848db3ce7aac34950d17be4d864da4b8b66911008e430bc544587"},
]
[[package]]
@ -119,6 +119,31 @@ files = [
{file = "iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7"},
]
[[package]]
name = "markdown-it-py"
version = "3.0.0"
requires_python = ">=3.8"
summary = "Python port of markdown-it. Markdown parsing, done right!"
groups = ["default"]
dependencies = [
"mdurl~=0.1",
]
files = [
{file = "markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb"},
{file = "markdown_it_py-3.0.0-py3-none-any.whl", hash = "sha256:355216845c60bd96232cd8d8c40e8f9765cc86f46880e43a8fd22dc1a1a8cab1"},
]
[[package]]
name = "mdurl"
version = "0.1.2"
requires_python = ">=3.7"
summary = "Markdown URL utilities"
groups = ["default"]
files = [
{file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"},
{file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"},
]
[[package]]
name = "packaging"
version = "25.0"
@ -165,13 +190,13 @@ files = [
[[package]]
name = "pygments"
version = "2.19.1"
version = "2.19.2"
requires_python = ">=3.8"
summary = "Pygments is a syntax highlighting package written in Python."
groups = ["dev"]
groups = ["default", "dev"]
files = [
{file = "pygments-2.19.1-py3-none-any.whl", hash = "sha256:9ea1544ad55cecf4b8242fab6dd35a93bbce657034b0611ee383099054ab6d8c"},
{file = "pygments-2.19.1.tar.gz", hash = "sha256:61c16d2a8576dc0649d9f39e089b5f02bcd27fba10d8fb4dcc28173f7a45151f"},
{file = "pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b"},
{file = "pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887"},
]
[[package]]
@ -205,7 +230,7 @@ files = [
[[package]]
name = "pytest"
version = "8.4.0"
version = "8.4.1"
requires_python = ">=3.9"
summary = "pytest: simple powerful testing with Python"
groups = ["dev"]
@ -219,8 +244,8 @@ dependencies = [
"tomli>=1; python_version < \"3.11\"",
]
files = [
{file = "pytest-8.4.0-py3-none-any.whl", hash = "sha256:f40f825768ad76c0977cbacdf1fd37c6f7a468e460ea6a0636078f8972d4517e"},
{file = "pytest-8.4.0.tar.gz", hash = "sha256:14d920b48472ea0dbf68e45b96cd1ffda4705f33307dcc86c676c1b5104838a6"},
{file = "pytest-8.4.1-py3-none-any.whl", hash = "sha256:539c70ba6fcead8e78eebbf1115e8b589e7565830d7d006a8723f19ac8a0afb7"},
{file = "pytest-8.4.1.tar.gz", hash = "sha256:7c67fd69174877359ed9371ec3af8a3d2b04741818c51e5e99cc1742251fa93c"},
]
[[package]]
@ -238,31 +263,47 @@ files = [
{file = "pytest_randomly-3.16.0.tar.gz", hash = "sha256:11bf4d23a26484de7860d82f726c0629837cf4064b79157bd18ec9d41d7feb26"},
]
[[package]]
name = "rich"
version = "14.0.0"
requires_python = ">=3.8.0"
summary = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
groups = ["default"]
dependencies = [
"markdown-it-py>=2.2.0",
"pygments<3.0.0,>=2.13.0",
"typing-extensions<5.0,>=4.0.0; python_version < \"3.11\"",
]
files = [
{file = "rich-14.0.0-py3-none-any.whl", hash = "sha256:1c9491e1951aac09caffd42f448ee3d04e58923ffe14993f6e83068dc395d7e0"},
{file = "rich-14.0.0.tar.gz", hash = "sha256:82f1bc23a6a21ebca4ae0c45af9bdbc492ed20231dcb63f297d6d1021a9d5725"},
]
[[package]]
name = "ruff"
version = "0.11.13"
version = "0.12.0"
requires_python = ">=3.7"
summary = "An extremely fast Python linter and code formatter, written in Rust."
groups = ["dev"]
files = [
{file = "ruff-0.11.13-py3-none-linux_armv6l.whl", hash = "sha256:4bdfbf1240533f40042ec00c9e09a3aade6f8c10b6414cf11b519488d2635d46"},
{file = "ruff-0.11.13-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:aef9c9ed1b5ca28bb15c7eac83b8670cf3b20b478195bd49c8d756ba0a36cf48"},
{file = "ruff-0.11.13-py3-none-macosx_11_0_arm64.whl", hash = "sha256:53b15a9dfdce029c842e9a5aebc3855e9ab7771395979ff85b7c1dedb53ddc2b"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ab153241400789138d13f362c43f7edecc0edfffce2afa6a68434000ecd8f69a"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:6c51f93029d54a910d3d24f7dd0bb909e31b6cd989a5e4ac513f4eb41629f0dc"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1808b3ed53e1a777c2ef733aca9051dc9bf7c99b26ece15cb59a0320fbdbd629"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:d28ce58b5ecf0f43c1b71edffabe6ed7f245d5336b17805803312ec9bc665933"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:55e4bc3a77842da33c16d55b32c6cac1ec5fb0fbec9c8c513bdce76c4f922165"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:633bf2c6f35678c56ec73189ba6fa19ff1c5e4807a78bf60ef487b9dd272cc71"},
{file = "ruff-0.11.13-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4ffbc82d70424b275b089166310448051afdc6e914fdab90e08df66c43bb5ca9"},
{file = "ruff-0.11.13-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:4a9ddd3ec62a9a89578c85842b836e4ac832d4a2e0bfaad3b02243f930ceafcc"},
{file = "ruff-0.11.13-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:d237a496e0778d719efb05058c64d28b757c77824e04ffe8796c7436e26712b7"},
{file = "ruff-0.11.13-py3-none-musllinux_1_2_i686.whl", hash = "sha256:26816a218ca6ef02142343fd24c70f7cd8c5aa6c203bca284407adf675984432"},
{file = "ruff-0.11.13-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:51c3f95abd9331dc5b87c47ac7f376db5616041173826dfd556cfe3d4977f492"},
{file = "ruff-0.11.13-py3-none-win32.whl", hash = "sha256:96c27935418e4e8e77a26bb05962817f28b8ef3843a6c6cc49d8783b5507f250"},
{file = "ruff-0.11.13-py3-none-win_amd64.whl", hash = "sha256:29c3189895a8a6a657b7af4e97d330c8a3afd2c9c8f46c81e2fc5a31866517e3"},
{file = "ruff-0.11.13-py3-none-win_arm64.whl", hash = "sha256:b4385285e9179d608ff1d2fb9922062663c658605819a6876d8beef0c30b7f3b"},
{file = "ruff-0.11.13.tar.gz", hash = "sha256:26fa247dc68d1d4e72c179e08889a25ac0c7ba4d78aecfc835d49cbfd60bf514"},
{file = "ruff-0.12.0-py3-none-linux_armv6l.whl", hash = "sha256:5652a9ecdb308a1754d96a68827755f28d5dfb416b06f60fd9e13f26191a8848"},
{file = "ruff-0.12.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:05ed0c914fabc602fc1f3b42c53aa219e5736cb030cdd85640c32dbc73da74a6"},
{file = "ruff-0.12.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:07a7aa9b69ac3fcfda3c507916d5d1bca10821fe3797d46bad10f2c6de1edda0"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e7731c3eec50af71597243bace7ec6104616ca56dda2b99c89935fe926bdcd48"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:952d0630eae628250ab1c70a7fffb641b03e6b4a2d3f3ec6c1d19b4ab6c6c807"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c021f04ea06966b02614d442e94071781c424ab8e02ec7af2f037b4c1e01cc82"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:7d235618283718ee2fe14db07f954f9b2423700919dc688eacf3f8797a11315c"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0c0758038f81beec8cc52ca22de9685b8ae7f7cc18c013ec2050012862cc9165"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:139b3d28027987b78fc8d6cfb61165447bdf3740e650b7c480744873688808c2"},
{file = "ruff-0.12.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:68853e8517b17bba004152aebd9dd77d5213e503a5f2789395b25f26acac0da4"},
{file = "ruff-0.12.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:3a9512af224b9ac4757f7010843771da6b2b0935a9e5e76bb407caa901a1a514"},
{file = "ruff-0.12.0-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:b08df3d96db798e5beb488d4df03011874aff919a97dcc2dd8539bb2be5d6a88"},
{file = "ruff-0.12.0-py3-none-musllinux_1_2_i686.whl", hash = "sha256:6a315992297a7435a66259073681bb0d8647a826b7a6de45c6934b2ca3a9ed51"},
{file = "ruff-0.12.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:1e55e44e770e061f55a7dbc6e9aed47feea07731d809a3710feda2262d2d4d8a"},
{file = "ruff-0.12.0-py3-none-win32.whl", hash = "sha256:7162a4c816f8d1555eb195c46ae0bd819834d2a3f18f98cc63819a7b46f474fb"},
{file = "ruff-0.12.0-py3-none-win_amd64.whl", hash = "sha256:d00b7a157b8fb6d3827b49d3324da34a1e3f93492c1f97b08e222ad7e9b291e0"},
{file = "ruff-0.12.0-py3-none-win_arm64.whl", hash = "sha256:8cd24580405ad8c1cc64d61725bca091d6b6da7eb3d36f72cc605467069d7e8b"},
{file = "ruff-0.12.0.tar.gz", hash = "sha256:4d047db3662418d4a848a3fdbfaf17488b34b62f527ed6f10cb8afd78135bc5c"},
]
[[package]]
@ -276,20 +317,9 @@ files = [
{file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"},
]
[[package]]
name = "terminaltables3"
version = "4.0.0"
requires_python = ">=3.8"
summary = "Generate simple tables in terminals from a nested list of strings. Fork of terminaltables."
groups = ["default"]
files = [
{file = "terminaltables3-4.0.0-py3-none-any.whl", hash = "sha256:93b4c722f35400a7869cd630e2bbab616b129d1c47c628765c7f47baab2ca270"},
{file = "terminaltables3-4.0.0.tar.gz", hash = "sha256:4e3eefe209aa89005a0a34d1525739424569729ee29b5e64a8dd51c5ebdab77f"},
]
[[package]]
name = "tox"
version = "4.26.0"
version = "4.27.0"
requires_python = ">=3.9"
summary = "tox is a generic virtualenv management and test command line tool"
groups = ["dev"]
@ -307,8 +337,8 @@ dependencies = [
"virtualenv>=20.31",
]
files = [
{file = "tox-4.26.0-py3-none-any.whl", hash = "sha256:75f17aaf09face9b97bd41645028d9f722301e912be8b4c65a3f938024560224"},
{file = "tox-4.26.0.tar.gz", hash = "sha256:a83b3b67b0159fa58e44e646505079e35a43317a62d2ae94725e0586266faeca"},
{file = "tox-4.27.0-py3-none-any.whl", hash = "sha256:2b8a7fb986b82aa2c830c0615082a490d134e0626dbc9189986da46a313c4f20"},
{file = "tox-4.27.0.tar.gz", hash = "sha256:b97d5ecc0c0d5755bcc5348387fef793e1bfa68eb33746412f4c60881d7f5f57"},
]
[[package]]

View File

@ -2,7 +2,7 @@
name = "slobs-cli"
description = "A command line application for Streamlabs Desktop"
authors = [{ name = "onyx-and-iris", email = "code@onyxandiris.online" }]
dependencies = ["pyslobs>=2.0.5", "asyncclick>=8.1.8", "terminaltables3>=4.0.0"]
dependencies = ["pyslobs>=2.0.5", "asyncclick>=8.1.8", "rich>=14.0.0"]
requires-python = ">=3.11"
readme = "README.md"
license = { text = "MIT" }

View File

@ -1,3 +1,3 @@
"""module for package metadata."""
__version__ = '0.10.0'
__version__ = '0.11.0'

View File

@ -3,8 +3,10 @@
import asyncclick as click
from anyio import create_task_group
from pyslobs import AudioService
from terminaltables3 import AsciiTable
from rich.table import Table
from rich.text import Text
from . import console, util
from .cli import cli
from .errors import SlobsCliError
@ -25,32 +27,40 @@ async def list(ctx: click.Context, id: bool = False):
async def _run():
sources = await as_.get_sources()
if not sources:
click.echo('No audio sources found.')
console.out.print('No audio sources found.')
conn.close()
return
table_data = [
['Audio Device Name', 'ID', 'Muted']
if id
else ['Audio Device Name', 'Muted']
style = ctx.obj['style']
table = Table(
show_header=True, header_style=style.header, border_style=style.border
)
if id:
columns = [
('Audio Source Name', 'left'),
('Muted', 'center'),
('ID', 'left'),
]
else:
columns = [
('Audio Source Name', 'left'),
('Muted', 'center'),
]
for col_name, col_justify in columns:
table.add_column(Text(col_name, justify='center'), justify=col_justify)
for source in sources:
model = await source.get_model()
to_append = [click.style(model.name, fg='blue')]
to_append = [Text(model.name, style=style.cell)]
to_append.append(util.check_mark(ctx, model.muted))
if id:
to_append.append(model.source_id)
to_append.append('' if model.muted else '')
to_append.append(Text(model.source_id, style=style.cell))
table_data.append(to_append)
table.add_row(*to_append)
table = AsciiTable(table_data)
table.justify_columns = {
0: 'left',
1: 'left' if id else 'center',
2: 'center' if id else None,
}
click.echo(table.table)
console.out.print(table)
conn.close()
@ -78,7 +88,7 @@ async def mute(ctx: click.Context, source_name: str):
raise SlobsCliError(f'Audio source "{source_name}" not found.')
await source.set_muted(True)
click.echo(f'{source_name} muted successfully.')
console.out.print(f'{console.highlight(ctx, source_name)} muted successfully.')
conn.close()
try:
@ -109,7 +119,9 @@ async def unmute(ctx: click.Context, source_name: str):
raise SlobsCliError(f'Audio source "{source_name}" not found.')
await source.set_muted(False)
click.echo(f'{source_name} unmuted successfully.')
console.out.print(
f'{console.highlight(ctx, source_name)} unmuted successfully.'
)
conn.close()
try:
@ -136,10 +148,14 @@ async def toggle(ctx: click.Context, source_name: str):
if model.name.lower() == source_name.lower():
if model.muted:
await source.set_muted(False)
click.echo(f'{source_name} unmuted successfully.')
console.out.print(
f'{console.highlight(ctx, source_name)} unmuted successfully.'
)
else:
await source.set_muted(True)
click.echo(f'{source_name} muted successfully.')
console.out.print(
f'{console.highlight(ctx, source_name)} muted successfully.'
)
conn.close()
break
else: # If no source by the given name was found
@ -168,8 +184,8 @@ async def status(ctx: click.Context, source_name: str):
for source in sources:
model = await source.get_model()
if model.name.lower() == source_name.lower():
click.echo(
f'"{source_name}" is {"muted" if model.muted else "unmuted"}.'
console.out.print(
f'{console.highlight(ctx, source_name)} is {"muted" if model.muted else "unmuted"}.'
)
conn.close()
return

View File

@ -4,6 +4,7 @@ import anyio
import asyncclick as click
from pyslobs import ConnectionConfig, SlobsConnection
from . import styles
from .__about__ import __version__ as version
@ -34,11 +35,32 @@ from .__about__ import __version__ as version
required=True,
help='The token for the SLOBS server.',
)
@click.option(
'-s',
'--style',
default='disabled',
envvar='SLOBS_STYLE',
show_default=True,
show_envvar=True,
help='The style to use for output.',
)
@click.option(
'-b',
'--no-border',
is_flag=True,
default=False,
envvar='SLOBS_STYLE_NO_BORDER',
show_default=True,
show_envvar=True,
help='Disable borders in the output.',
)
@click.version_option(
version, '-v', '--version', message='%(prog)s version: %(version)s'
)
@click.pass_context
async def cli(ctx: click.Context, domain: str, port: int, token: str):
async def cli(
ctx: click.Context, domain: str, port: int, token: str, style: str, no_border: bool
):
"""Command line interface for Streamlabs Desktop."""
ctx.ensure_object(dict)
config = ConnectionConfig(
@ -47,6 +69,7 @@ async def cli(ctx: click.Context, domain: str, port: int, token: str):
token=token,
)
ctx.obj['connection'] = SlobsConnection(config)
ctx.obj['style'] = styles.request_style_obj(style, no_border)
def run():

21
src/slobs_cli/console.py Normal file
View File

@ -0,0 +1,21 @@
"""module for console output handling."""
import asyncclick as click
from rich.console import Console
out = Console()
err = Console(stderr=True, style='bold red')
def highlight(ctx: click.Context, text: str) -> str:
"""Highlight text for console output."""
if ctx.obj['style'].name == 'no_colour':
return text
return f'[{ctx.obj["style"].highlight}]{text}[/{ctx.obj["style"].highlight}]'
def warning(ctx: click.Context, text: str) -> str:
"""Format warning text for console output."""
if ctx.obj['style'].name == 'no_colour':
return text
return f'[magenta]{text}[/magenta]'

View File

@ -4,6 +4,8 @@ import json
import asyncclick as click
from . import console
class SlobsCliError(click.ClickException):
"""Base class for all Slobs CLI errors."""
@ -14,8 +16,8 @@ class SlobsCliError(click.ClickException):
self.exit_code = 1
def show(self):
"""Display the error message in red."""
click.secho(f'Error: {self.message}', fg='red', err=True)
"""Display the error message in red and write to stderr."""
console.err.print(f'Error: {self.message}')
class SlobsCliProtocolError(SlobsCliError):
@ -36,10 +38,8 @@ class SlobsCliProtocolError(SlobsCliError):
"""Display the protocol error message in red."""
match self.protocol_code:
case -32600:
click.secho(
'Oops! Looks like we hit a rate limit for this command. Please try again later.',
fg='red',
err=True,
console.err.print(
'Oops! Looks like we hit a rate limit for this command. Please try again later.'
)
case _:
# Fall back to the base error display for unknown protocol codes

View File

@ -4,6 +4,7 @@ import asyncclick as click
from anyio import create_task_group
from pyslobs import StreamingService
from . import console
from .cli import cli
from .errors import SlobsCliError
@ -29,7 +30,7 @@ async def start(ctx: click.Context):
raise SlobsCliError('Recording is already active.')
await ss.toggle_recording()
click.echo('Recording started.')
console.out.print('Recording started.')
conn.close()
@ -58,7 +59,7 @@ async def stop(ctx: click.Context):
raise SlobsCliError('Recording is already inactive.')
await ss.toggle_recording()
click.echo('Recording stopped.')
console.out.print('Recording stopped.')
conn.close()
@ -83,9 +84,9 @@ async def status(ctx: click.Context):
active = model.recording_status != 'offline'
if active:
click.echo('Recording is currently active.')
console.out.print('Recording is currently active.')
else:
click.echo('Recording is currently inactive.')
console.out.print('Recording is currently inactive.')
conn.close()
@ -107,9 +108,9 @@ async def toggle(ctx: click.Context):
await ss.toggle_recording()
if active:
click.echo('Recording stopped.')
console.out.print('Recording stopped.')
else:
click.echo('Recording started.')
console.out.print('Recording started.')
conn.close()

View File

@ -4,6 +4,7 @@ import asyncclick as click
from anyio import create_task_group
from pyslobs import StreamingService
from . import console
from .cli import cli
from .errors import SlobsCliError
@ -29,7 +30,7 @@ async def start(ctx: click.Context):
raise SlobsCliError('Replay buffer is already active.')
await ss.start_replay_buffer()
click.echo('Replay buffer started.')
console.out.print('Replay buffer started.')
conn.close()
try:
@ -57,7 +58,7 @@ async def stop(ctx: click.Context):
raise SlobsCliError('Replay buffer is already inactive.')
await ss.stop_replay_buffer()
click.echo('Replay buffer stopped.')
console.out.print('Replay buffer stopped.')
conn.close()
try:
@ -80,9 +81,9 @@ async def status(ctx: click.Context):
model = await ss.get_model()
active = model.replay_buffer_status != 'offline'
if active:
click.echo('Replay buffer is currently active.')
console.out.print('Replay buffer is currently active.')
else:
click.echo('Replay buffer is currently inactive.')
console.out.print('Replay buffer is currently inactive.')
conn.close()
async with create_task_group() as tg:
@ -99,7 +100,7 @@ async def save(ctx: click.Context):
async def _run():
await ss.save_replay()
click.echo('Replay buffer saved.')
console.out.print('Replay buffer saved.')
conn.close()
async with create_task_group() as tg:

View File

@ -3,8 +3,10 @@
import asyncclick as click
from anyio import create_task_group
from pyslobs import ProtocolError, ScenesService, TransitionsService
from terminaltables3 import AsciiTable
from rich.table import Table
from rich.text import Text
from . import console, util
from .cli import cli
from .errors import SlobsCliError, SlobsCliProtocolError
@ -25,34 +27,45 @@ async def list(ctx: click.Context, id: bool = False):
async def _run():
scenes = await ss.get_scenes()
if not scenes:
click.echo('No scenes found.')
console.out.print('No scenes found.')
conn.close()
return
active_scene = await ss.active_scene()
table_data = [
['Scene Name', 'ID', 'Active'] if id else ['Scene Name', 'Active']
]
for scene in scenes:
if scene.id == active_scene.id:
to_append = [click.style(scene.name, fg='green')]
else:
to_append = [click.style(scene.name, fg='blue')]
style = ctx.obj['style']
table = Table(
show_header=True,
header_style=style.header,
border_style=style.border,
)
if id:
to_append.append(scene.id)
if scene.id == active_scene.id:
to_append.append('')
columns = [
('Scene Name', 'left'),
('Active', 'center'),
('ID', 'left'),
]
else:
columns = [
('Scene Name', 'left'),
('Active', 'center'),
]
table_data.append(to_append)
for col_name, col_justify in columns:
table.add_column(Text(col_name, justify='center'), justify=col_justify)
table = AsciiTable(table_data)
table.justify_columns = {
0: 'left',
1: 'left' if id else 'center',
2: 'center' if id else None,
}
click.echo(table.table)
for scene in scenes:
to_append = [Text(scene.name, style=style.cell)]
to_append.append(
util.check_mark(ctx, scene.id == active_scene.id, empty_if_false=True)
)
if id:
to_append.append(Text(scene.id, style=style.cell))
table.add_row(*to_append)
console.out.print(table)
conn.close()
@ -76,9 +89,9 @@ async def current(ctx: click.Context, id: bool = False):
async def _run():
active_scene = await ss.active_scene()
click.echo(
f'Current active scene: {click.style(active_scene.name, fg="green")} '
f'{f"(ID: {active_scene.id})" if id else ""}'
console.out.print(
f'Current active scene: {console.highlight(ctx, active_scene.name)} '
f'{f"(ID: {console.highlight(ctx, active_scene.id)})" if id else ""}'
)
conn.close()
@ -118,18 +131,21 @@ async def switch(
if model.studio_mode:
await ss.make_scene_active(scene.id)
if preview:
click.echo(
f'Switched to preview scene: {click.style(scene.name, fg="blue")} '
f'{f"(ID: {scene.id})." if id else ""}'
console.out.print(
f'Switched to preview scene: {console.highlight(ctx, scene.name)} '
f'{f"(ID: {console.highlight(ctx, scene.id)})" if id else ""}'
)
else:
click.echo(
f'Switched to scene: {click.style(scene.name, fg="blue")} '
f'{f"(ID: {scene.id})." if id else ""}'
console.out.print(
f'Switched to scene: {console.highlight(ctx, scene.name)} '
f'{f"(ID: {console.highlight(ctx, scene.id)})" if id else ""}'
)
console.err.print(
console.warning(
ctx,
'Warning: You are in studio mode. The scene switch is not active yet.\n'
'use `slobs-cli studiomode force-transition` to activate the scene switch.',
)
await ts.execute_studio_mode_transition()
click.echo(
'Executed studio mode transition to make the scene active.'
)
else:
if preview:
@ -139,9 +155,9 @@ async def switch(
)
await ss.make_scene_active(scene.id)
click.echo(
f'Switched to scene: {click.style(scene.name, fg="blue")} '
f'{f"(ID: {scene.id})." if id else ""}'
console.out.print(
f'Switched to scene: {console.highlight(ctx, scene.name)} '
f'{f"(ID: {console.highlight(ctx, scene.id)})" if id else ""}'
)
conn.close()

View File

@ -3,8 +3,10 @@
import asyncclick as click
from anyio import create_task_group
from pyslobs import ISceneCollectionCreateOptions, SceneCollectionsService
from terminaltables3 import AsciiTable
from rich.table import Table
from rich.text import Text
from . import console, util
from .cli import cli
from .errors import SlobsCliError
@ -25,35 +27,46 @@ async def list(ctx: click.Context, id: bool):
async def _run():
collections = await scs.collections()
if not collections:
click.echo('No scene collections found.')
console.out.print('No scene collections found.')
conn.close()
return
active_collection = await scs.active_collection()
table_data = [
['Scene Collection Name', 'ID', 'Active']
if id
else ['Scene Collection Name', 'Active']
]
for collection in collections:
if collection.id == active_collection.id:
to_append = [click.style(collection.name, fg='green')]
else:
to_append = [click.style(collection.name, fg='blue')]
if id:
to_append.append(collection.id)
if collection.id == active_collection.id:
to_append.append('')
table_data.append(to_append)
style = ctx.obj['style']
table = Table(
show_header=True,
header_style=style.header,
border_style=style.border,
)
table = AsciiTable(table_data)
table.justify_columns = {
0: 'left',
1: 'left' if id else 'center',
2: 'center' if id else None,
}
click.echo(table.table)
if id:
columns = [
('Scene Collection Name', 'left'),
('Active', 'center'),
('ID', 'left'),
]
else:
columns = [
('Scene Collection Name', 'left'),
('Active', 'center'),
]
for col_name, col_justify in columns:
table.add_column(Text(col_name, justify='center'), justify=col_justify)
for collection in collections:
to_append = [Text(collection.name, style=style.cell)]
to_append.append(
util.check_mark(
ctx, collection.id == active_collection.id, empty_if_false=True
)
)
if id:
to_append.append(Text(collection.id, style=style.cell))
table.add_row(*to_append)
console.out.print(table)
conn.close()
@ -80,7 +93,9 @@ async def load(ctx: click.Context, scenecollection_name: str):
raise SlobsCliError(f'Scene collection "{scenecollection_name}" not found.')
await scs.load(collection.id)
click.echo(f'Scene collection "{scenecollection_name}" loaded successfully.')
console.out.print(
f'Scene collection {console.highlight(scenecollection_name)} loaded successfully.'
)
conn.close()
try:
@ -102,7 +117,9 @@ async def create(ctx: click.Context, scenecollection_name: str):
async def _run():
await scs.create(ISceneCollectionCreateOptions(scenecollection_name))
click.echo(f'Scene collection "{scenecollection_name}" created successfully.')
console.out.print(
f'Scene collection {console.highlight(scenecollection_name)} created successfully.'
)
conn.close()
async with create_task_group() as tg:
@ -128,7 +145,9 @@ async def delete(ctx: click.Context, scenecollection_name: str):
raise SlobsCliError(f'Scene collection "{scenecollection_name}" not found.')
await scs.delete(collection.id)
click.echo(f'Scene collection "{scenecollection_name}" deleted successfully.')
console.out.print(
f'Scene collection {console.highlight(scenecollection_name)} deleted successfully.'
)
conn.close()
try:
@ -159,8 +178,8 @@ async def rename(ctx: click.Context, scenecollection_name: str, new_name: str):
raise SlobsCliError(f'Scene collection "{scenecollection_name}" not found.')
await scs.rename(new_name, collection.id)
click.echo(
f'Scene collection "{scenecollection_name}" renamed to "{new_name}".'
console.out.print(
f'Scene collection {console.highlight(scenecollection_name)} renamed to {console.highlight(new_name)}.'
)
conn.close()

View File

@ -4,6 +4,7 @@ import asyncclick as click
from anyio import create_task_group
from pyslobs import StreamingService
from . import console
from .cli import cli
from .errors import SlobsCliError
@ -29,7 +30,7 @@ async def start(ctx: click.Context):
raise SlobsCliError('Stream is already active.')
await ss.toggle_streaming()
click.echo('Stream started.')
console.out.print('Stream started.')
conn.close()
try:
@ -57,7 +58,7 @@ async def stop(ctx: click.Context):
raise SlobsCliError('Stream is already inactive.')
await ss.toggle_streaming()
click.echo('Stream stopped.')
console.out.print('Stream stopped.')
conn.close()
try:
@ -81,9 +82,9 @@ async def status(ctx: click.Context):
active = model.streaming_status != 'offline'
if active:
click.echo('Stream is currently active.')
console.out.print('Stream is currently active.')
else:
click.echo('Stream is currently inactive.')
console.out.print('Stream is currently inactive.')
conn.close()
async with create_task_group() as tg:
@ -104,9 +105,9 @@ async def toggle(ctx: click.Context):
await ss.toggle_streaming()
if active:
click.echo('Stream stopped.')
console.out.print('Stream stopped.')
else:
click.echo('Stream started.')
console.out.print('Stream started.')
conn.close()

View File

@ -4,6 +4,7 @@ import asyncclick as click
from anyio import create_task_group
from pyslobs import TransitionsService
from . import console
from .cli import cli
from .errors import SlobsCliError
@ -27,7 +28,7 @@ async def enable(ctx: click.Context):
raise SlobsCliError('Studio mode is already enabled.')
await ts.enable_studio_mode()
click.echo('Studio mode enabled successfully.')
console.out.print('Studio mode enabled successfully.')
conn.close()
try:
@ -53,7 +54,7 @@ async def disable(ctx: click.Context):
raise SlobsCliError('Studio mode is already disabled.')
await ts.disable_studio_mode()
click.echo('Studio mode disabled successfully.')
console.out.print('Studio mode disabled successfully.')
conn.close()
try:
@ -75,9 +76,9 @@ async def status(ctx: click.Context):
async def _run():
model = await ts.get_model()
if model.studio_mode:
click.echo('Studio mode is currently enabled.')
console.out.print('Studio mode is currently enabled.')
else:
click.echo('Studio mode is currently disabled.')
console.out.print('Studio mode is currently disabled.')
conn.close()
async with create_task_group() as tg:
@ -96,10 +97,10 @@ async def toggle(ctx: click.Context):
model = await ts.get_model()
if model.studio_mode:
await ts.disable_studio_mode()
click.echo('Studio mode disabled successfully.')
console.out.print('Studio mode disabled successfully.')
else:
await ts.enable_studio_mode()
click.echo('Studio mode enabled successfully.')
console.out.print('Studio mode enabled successfully.')
conn.close()
async with create_task_group() as tg:
@ -121,7 +122,7 @@ async def force_transition(ctx: click.Context):
raise SlobsCliError('Studio mode is not enabled.')
await ts.execute_studio_mode_transition()
click.echo('Forced studio mode transition.')
console.out.print('Forced studio mode transition.')
conn.close()
try:

186
src/slobs_cli/styles.py Normal file
View File

@ -0,0 +1,186 @@
"""module containing style management for Slobs CLI."""
import os
from dataclasses import dataclass
_registry = {}
def register_style(cls):
"""Register a style class."""
key = cls.__name__.lower()
if key in _registry:
raise ValueError(f'Style {key} is already registered.')
_registry[key] = cls
return cls
@dataclass
class Style:
"""Base class for styles."""
name: str = 'no_colour'
border: str | None = None
header: str | None = None
cell: str | None = None
highlight: str | None = None
no_border: bool = False
def __post_init__(self):
"""Post-initialization to set default values and normalize the name."""
self.name = self.name.lower()
if self.no_border:
self.border = None
@register_style
@dataclass
class Red(Style):
"""Red style."""
name: str = 'red'
header: str = ''
border: str = 'dark_red'
cell: str = 'red'
highlight: str = 'red3'
@register_style
@dataclass
class Magenta(Style):
"""Magenta style."""
name: str = 'magenta'
header: str = ''
border: str = 'dark_magenta'
cell: str = 'magenta'
highlight: str = 'magenta3'
@register_style
@dataclass
class Purple(Style):
"""Purple style."""
name: str = 'purple'
header: str = ''
border: str = 'purple'
cell: str = 'medium_orchid'
highlight: str = 'medium_orchid'
@register_style
@dataclass
class Blue(Style):
"""Blue style."""
name: str = 'blue'
header: str = ''
border: str = 'dark_blue'
cell: str = 'blue'
highlight: str = 'blue3'
@register_style
@dataclass
class Cyan(Style):
"""Cyan style."""
name: str = 'cyan'
header: str = ''
border: str = 'dark_cyan'
cell: str = 'cyan'
highlight: str = 'cyan3'
@register_style
@dataclass
class Green(Style):
"""Green style."""
name: str = 'green'
header: str = ''
border: str = 'dark_green'
cell: str = 'green'
highlight: str = 'green3'
@register_style
@dataclass
class Yellow(Style):
"""Yellow style."""
name: str = 'yellow'
header: str = ''
border: str = 'yellow3'
cell: str = 'wheat1'
highlight: str = 'yellow3'
@register_style
@dataclass
class Orange(Style):
"""Orange style."""
name: str = 'orange'
header: str = ''
border: str = 'dark_orange'
cell: str = 'orange'
highlight: str = 'orange3'
@register_style
@dataclass
class White(Style):
"""White style."""
name: str = 'white'
header: str = ''
border: str = 'white'
cell: str = 'white'
highlight: str = 'white'
@register_style
@dataclass
class Grey(Style):
"""Grey style."""
name: str = 'grey'
header: str = ''
border: str = 'grey50'
cell: str = 'grey70'
highlight: str = 'grey90'
@register_style
@dataclass
class Navy(Style):
"""Navy style."""
name: str = 'navy'
header: str = ''
border: str = 'deep_sky_blue4'
cell: str = 'light_sky_blue3'
highlight: str = 'light_sky_blue3'
@register_style
@dataclass
class Black(Style):
"""Black style."""
name: str = 'black'
header: str = ''
border: str = 'black'
cell: str = 'grey30'
highlight: str = 'grey30'
def request_style_obj(style_name: str, no_border: bool) -> Style:
"""Request a style object by name."""
key = style_name.lower()
if key not in _registry:
os.environ['NO_COLOR'] = '1' # Disable colour output
return Style(no_border=no_border)
return _registry[key](no_border=no_border)

15
src/slobs_cli/util.py Normal file
View File

@ -0,0 +1,15 @@
"""module containing utility functions for Slobs CLI."""
import os
import asyncclick as click
def check_mark(ctx: click.Context, value: bool, empty_if_false: bool = False) -> str:
"""Return a check mark or cross mark based on the boolean value."""
if empty_if_false and not value:
return ''
if os.getenv('NO_COLOR', '') != '' or ctx.obj['style'].name == 'no_colour':
return '' if value else ''
return '' if value else ''

View File

@ -9,7 +9,13 @@ import os
import anyio
from anyio import create_task_group
from pyslobs import ConnectionConfig, ScenesService, SlobsConnection, StreamingService
from pyslobs import (
ConnectionConfig,
ScenesService,
SlobsConnection,
StreamingService,
TransitionsService,
)
async def cleanup(conn: SlobsConnection):
@ -29,6 +35,11 @@ async def cleanup(conn: SlobsConnection):
if model.recording_status != 'offline':
await ss.toggle_recording()
ts = TransitionsService(conn)
model = await ts.get_model()
if model.studio_mode:
await ts.disable_studio_mode()
conn.close()