add ScreenshotCmd

This commit is contained in:
onyx-and-iris 2025-06-04 12:57:17 +01:00
parent ee47832cd6
commit 29fe6fedfb
3 changed files with 49 additions and 0 deletions

View File

@ -47,6 +47,7 @@ type CLI struct {
Hotkey HotkeyCmd `help:"Manage hotkeys." cmd:"" aliases:"hk"` Hotkey HotkeyCmd `help:"Manage hotkeys." cmd:"" aliases:"hk"`
Filter FilterCmd `help:"Manage filters." cmd:"" aliases:"f"` Filter FilterCmd `help:"Manage filters." cmd:"" aliases:"f"`
Projector ProjectorCmd `help:"Manage projectors." cmd:"" aliases:"prj"` Projector ProjectorCmd `help:"Manage projectors." cmd:"" aliases:"prj"`
Screenshot ScreenshotCmd `help:"Take screenshots." cmd:"" aliases:"ss"`
} }
type context struct { type context struct {

41
screenshot.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"path/filepath"
"github.com/andreykaipov/goobs/api/requests/sources"
)
// ScreenshotCmd provides commands to manage screenshots in OBS Studio.
type ScreenshotCmd struct {
Save ScreenshotSaveCmd `cmd:"" help:"Take a screenshot and save it to a file." aliases:"sv"`
}
// ScreenshotSaveCmd represents the command to save a screenshot of a source in OBS.
type ScreenshotSaveCmd struct {
SourceName string `arg:"" help:"Name of the source to take a screenshot of."`
FilePath string `arg:"" help:"Path to the file where the screenshot will be saved."`
Width float64 ` help:"Width of the screenshot in pixels." flag:"" default:"1920"`
Height float64 ` help:"Height of the screenshot in pixels." flag:"" default:"1080"`
Quality float64 ` help:"Quality of the screenshot (1-100)." flag:"" default:"-1"`
}
// Run executes the command to take a screenshot and save it to a file.
func (cmd *ScreenshotSaveCmd) Run(ctx *context) error {
_, err := ctx.Client.Sources.SaveSourceScreenshot(
sources.NewSaveSourceScreenshotParams().
WithSourceName(cmd.SourceName).
WithImageFormat(trimPrefix(filepath.Ext(cmd.FilePath), ".")).
WithImageFilePath(cmd.FilePath).
WithImageWidth(cmd.Width).
WithImageHeight(cmd.Height).
WithImageCompressionQuality(cmd.Quality),
)
if err != nil {
return fmt.Errorf("failed to take screenshot: %w", err)
}
fmt.Fprintf(ctx.Out, "Screenshot saved to %s.\n", cmd.FilePath)
return nil
}

View File

@ -20,3 +20,10 @@ func getEnabledMark(enabled bool) string {
} }
return "\u274c" // red cross mark return "\u274c" // red cross mark
} }
func trimPrefix(s, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}