print group list as rich table

scene_name arg is now optional

upd README

patch bump
This commit is contained in:
onyx-and-iris 2025-05-23 21:55:52 +01:00
parent 37dbbdf4e2
commit abeb5285d8
3 changed files with 57 additions and 21 deletions

View File

@ -205,7 +205,10 @@ obsws-cli scenecollection create test-collection
#### Group #### Group
- list: List groups in a scene. - list: List groups in a scene.
*optional*
- args: <scene_name> - args: <scene_name>
- defaults to current scene
```console ```console
obsws-cli group list START obsws-cli group list START

View File

@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online> # SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
__version__ = "0.12.0" __version__ = "0.12.1"

View File

@ -1,12 +1,16 @@
"""module containing commands for manipulating groups in scenes.""" """module containing commands for manipulating groups in scenes."""
import typer import typer
from rich.console import Console
from rich.table import Table
from . import validate from . import validate
from .alias import AliasGroup from .alias import AliasGroup
from .protocols import DataclassProtocol from .protocols import DataclassProtocol
app = typer.Typer(cls=AliasGroup) app = typer.Typer(cls=AliasGroup)
out_console = Console()
err_console = Console(stderr=True)
@app.callback() @app.callback()
@ -15,17 +19,46 @@ def main():
@app.command('list | ls') @app.command('list | ls')
def list(ctx: typer.Context, scene_name: str): def list(
ctx: typer.Context,
scene_name: str = typer.Argument(
None, help='Scene name (optional, defaults to current scene)'
),
):
"""List groups in a scene.""" """List groups in a scene."""
if not scene_name:
scene_name = ctx.obj.get_current_program_scene().scene_name
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.", err=True) err_console.print(f"Scene '{scene_name}' not found.")
raise typer.Exit(1) raise typer.Exit(1)
resp = ctx.obj.get_scene_item_list(scene_name) resp = ctx.obj.get_scene_item_list(scene_name)
groups = ( groups = [
item.get('sourceName') for item in resp.scene_items if item.get('isGroup') (item.get('sceneItemId'), item.get('sourceName'), item.get('sceneItemEnabled'))
) for item in resp.scene_items
typer.echo('\n'.join(groups)) if item.get('isGroup')
]
if not groups:
err_console.print(f"No groups found in scene '{scene_name}'.")
raise typer.Exit(1)
table = Table(title=f'Groups in Scene: {scene_name}')
for column in ('ID', 'Name', 'Enabled'):
table.add_column(
column, justify='left' if column == 'Name' else 'center', style='cyan'
)
for item_id, group_name, is_enabled in groups:
table.add_row(
str(item_id),
group_name,
':heavy_check_mark:' if is_enabled else ':x:',
)
out_console.print(table)
def _get_group(group_name: str, resp: DataclassProtocol) -> dict | None: def _get_group(group_name: str, resp: DataclassProtocol) -> dict | None:
@ -45,12 +78,12 @@ def _get_group(group_name: str, resp: DataclassProtocol) -> dict | None:
def show(ctx: typer.Context, scene_name: str, group_name: str): def show(ctx: typer.Context, scene_name: str, group_name: str):
"""Show a group in a scene.""" """Show a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.", err=True) err_console.print(f"Scene '{scene_name}' not found.")
raise typer.Exit(1) raise typer.Exit(1)
resp = ctx.obj.get_scene_item_list(scene_name) resp = ctx.obj.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None: if (group := _get_group(group_name, resp)) is None:
typer.echo(f"Group '{group_name}' not found in scene {scene_name}.", err=True) err_console.print(f"Group '{group_name}' not found in scene {scene_name}.")
raise typer.Exit(1) raise typer.Exit(1)
ctx.obj.set_scene_item_enabled( ctx.obj.set_scene_item_enabled(
@ -59,19 +92,19 @@ def show(ctx: typer.Context, scene_name: str, group_name: str):
enabled=True, enabled=True,
) )
typer.echo(f"Group '{group_name}' is now visible.") out_console.print(f"Group '{group_name}' is now visible.")
@app.command('hide | h') @app.command('hide | h')
def hide(ctx: typer.Context, scene_name: str, group_name: str): def hide(ctx: typer.Context, scene_name: str, group_name: str):
"""Hide a group in a scene.""" """Hide a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.", err=True) err_console.print(f"Scene '{scene_name}' not found.")
raise typer.Exit(1) raise typer.Exit(1)
resp = ctx.obj.get_scene_item_list(scene_name) resp = ctx.obj.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None: if (group := _get_group(group_name, resp)) is None:
typer.echo(f"Group '{group_name}' not found in scene {scene_name}.", err=True) err_console.print(f"Group '{group_name}' not found in scene {scene_name}.")
raise typer.Exit(1) raise typer.Exit(1)
ctx.obj.set_scene_item_enabled( ctx.obj.set_scene_item_enabled(
@ -80,19 +113,19 @@ def hide(ctx: typer.Context, scene_name: str, group_name: str):
enabled=False, enabled=False,
) )
typer.echo(f"Group '{group_name}' is now hidden.") out_console.print(f"Group '{group_name}' is now hidden.")
@app.command('toggle | tg') @app.command('toggle | tg')
def toggle(ctx: typer.Context, scene_name: str, group_name: str): def toggle(ctx: typer.Context, scene_name: str, group_name: str):
"""Toggle a group in a scene.""" """Toggle a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.", err=True) err_console.print(f"Scene '{scene_name}' not found.")
raise typer.Exit(1) raise typer.Exit(1)
resp = ctx.obj.get_scene_item_list(scene_name) resp = ctx.obj.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None: if (group := _get_group(group_name, resp)) is None:
typer.echo(f"Group '{group_name}' not found in scene {scene_name}.", err=True) err_console.print(f"Group '{group_name}' not found in scene {scene_name}.")
raise typer.Exit(1) raise typer.Exit(1)
new_state = not group.get('sceneItemEnabled') new_state = not group.get('sceneItemEnabled')
@ -103,21 +136,21 @@ def toggle(ctx: typer.Context, scene_name: str, group_name: str):
) )
if new_state: if new_state:
typer.echo(f"Group '{group_name}' is now visible.") out_console.print(f"Group '{group_name}' is now visible.")
else: else:
typer.echo(f"Group '{group_name}' is now hidden.") out_console.print(f"Group '{group_name}' is now hidden.")
@app.command('status | ss') @app.command('status | ss')
def status(ctx: typer.Context, scene_name: str, group_name: str): def status(ctx: typer.Context, scene_name: str, group_name: str):
"""Get the status of a group in a scene.""" """Get the status of a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.", err=True) err_console.print(f"Scene '{scene_name}' not found.")
raise typer.Exit(1) raise typer.Exit(1)
resp = ctx.obj.get_scene_item_list(scene_name) resp = ctx.obj.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None: if (group := _get_group(group_name, resp)) is None:
typer.echo(f"Group '{group_name}' not found in scene {scene_name}.", err=True) err_console.print(f"Group '{group_name}' not found in scene {scene_name}.")
raise typer.Exit(1) raise typer.Exit(1)
enabled = ctx.obj.get_scene_item_enabled( enabled = ctx.obj.get_scene_item_enabled(
@ -126,6 +159,6 @@ def status(ctx: typer.Context, scene_name: str, group_name: str):
) )
if enabled.scene_item_enabled: if enabled.scene_item_enabled:
typer.echo(f"Group '{group_name}' is now visible.") out_console.print(f"Group '{group_name}' is now visible.")
else: else:
typer.echo(f"Group '{group_name}' is now hidden.") out_console.print(f"Group '{group_name}' is now hidden.")