diff --git a/main.go b/main.go index e0e94b2..ee7a7eb 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,7 @@ type CLI struct { Sceneitem SceneItemCmd `help:"Manage scene items." cmd:"" aliases:"si" group:"Scene Item"` Group GroupCmd `help:"Manage groups." cmd:"" aliases:"g" group:"Group"` Input InputCmd `help:"Manage inputs." cmd:"" aliases:"i" group:"Input"` + Text TextCmd `help:"Manage text inputs." cmd:"" aliases:"t" group:"Text Input"` Record RecordCmd `help:"Manage recording." cmd:"" aliases:"rec" group:"Recording"` Stream StreamCmd `help:"Manage streaming." cmd:"" aliases:"st" group:"Streaming"` Scenecollection SceneCollectionCmd `help:"Manage scene collections." cmd:"" aliases:"scn" group:"Scene Collection"` diff --git a/text.go b/text.go new file mode 100644 index 0000000..64a9ba5 --- /dev/null +++ b/text.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/andreykaipov/goobs/api/requests/inputs" +) + +// TextCmd provides commands for managing text inputs in OBS. +type TextCmd struct { + Current TextCurrentCmd `cmd:"current" help:"Display current text for a text input." aliases:"c"` + Update TextUpdateCmd `cmd:"update" help:"Update the text of a text input." aliases:"u"` +} + +// TextCurrentCmd provides a command to display the current text of a text input. +type TextCurrentCmd struct { + InputName string `arg:"" help:"Name of the text source."` +} + +// Run executes the command to display the current text of a text input. +func (cmd *TextCurrentCmd) Run(ctx *context) error { + resp, err := ctx.Client.Inputs.GetInputSettings( + inputs.NewGetInputSettingsParams().WithInputName(cmd.InputName), + ) + if err != nil { + return fmt.Errorf("failed to get input settings: %w", err) + } + + // Check if the input is a text input + kind := resp.InputKind + if !strings.HasPrefix(kind, "text_") { + return fmt.Errorf("input %s is of %s", cmd.InputName, kind) + } + + fmt.Fprintf( + ctx.Out, + "Current text for source %s: %s\n", + ctx.Style.Highlight(cmd.InputName), + resp.InputSettings["text"], + ) + return nil +} + +// TextUpdateCmd provides a command to update the text of a text input. +type TextUpdateCmd struct { + InputName string `arg:"" help:"Name of the text source."` + NewText string `arg:"" help:"New text to set for the source." default:""` +} + +// Run executes the command to update the text of a text input. +func (cmd *TextUpdateCmd) Run(ctx *context) error { + resp, err := ctx.Client.Inputs.GetInputSettings( + inputs.NewGetInputSettingsParams().WithInputName(cmd.InputName), + ) + if err != nil { + return fmt.Errorf("failed to get input settings: %w", err) + } + + // Check if the input is a text input + kind := resp.InputKind + if !strings.HasPrefix(kind, "text_") { + return fmt.Errorf("input %s is of %s", cmd.InputName, kind) + } + + if _, err := ctx.Client.Inputs.SetInputSettings(&inputs.SetInputSettingsParams{ + InputName: &cmd.InputName, + InputSettings: map[string]any{"text": &cmd.NewText}, + }); err != nil { + return fmt.Errorf("failed to update text for source %s: %w", cmd.InputName, err) + } + + if cmd.NewText == "" { + cmd.NewText = "(empty)" + } + fmt.Fprintf(ctx.Out, "Updated text for source %s to: %s\n", ctx.Style.Highlight(cmd.InputName), cmd.NewText) + return nil +}