1
0

cli: add --page flag to start on a specific page

This commit is contained in:
Maksim Totmin 2026-06-22 14:44:16 +07:00
parent cdbfec8bb7
commit feae481302
2 changed files with 32 additions and 9 deletions

View File

@ -15,6 +15,7 @@ type RunOptions struct {
HTTPAddr string HTTPAddr string
HTTPEnabled bool HTTPEnabled bool
NoDeck bool NoDeck bool
StartPage string
} }
func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error { func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
@ -63,6 +64,16 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
primaryPM := pageMgrs[0] primaryPM := pageMgrs[0]
primaryDeck := decks[0] primaryDeck := decks[0]
startPage := opts.StartPage
if startPage == "" {
startPage = cfg.DefaultPage
}
if !pageExists(cfg.Pages, startPage) {
slog.Warn("start page not found in pages, falling back to default",
"requested", startPage, "default", cfg.DefaultPage)
startPage = cfg.DefaultPage
}
defer func() { defer func() {
for _, d := range decks { for _, d := range decks {
d.Close() d.Close()
@ -78,8 +89,8 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
web.SetExtraPageManagers(pageMgrs[1:]) web.SetExtraPageManagers(pageMgrs[1:])
for _, pm := range pageMgrs { for _, pm := range pageMgrs {
if err := pm.ActivatePage(cfg.DefaultPage); err != nil { if err := pm.ActivatePage(startPage); err != nil {
slog.Warn("activate default page", "error", err) slog.Warn("activate start page", "error", err)
} }
pm.startPeriodicKeys() pm.startPeriodicKeys()
} }
@ -98,7 +109,7 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
windowCh, _ = detector.Start(ctx) windowCh, _ = detector.Start(ctx)
} }
asm := NewAutoSwitchManager(cfg.AutoSwitch, cfg.DefaultPage) asm := NewAutoSwitchManager(cfg.AutoSwitch, startPage)
ssCtrl := NewScreensaver(&cfg.Screensaver) ssCtrl := NewScreensaver(&cfg.Screensaver)
@ -149,7 +160,7 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
asm.Pause() asm.Pause()
primaryDeck.Close() primaryDeck.Close()
newDeck := reconnectDeck(ctx, cfg, &primaryPM) newDeck := reconnectDeck(ctx, cfg, &primaryPM, startPage)
if newDeck == nil { if newDeck == nil {
return ctx.Err() return ctx.Err()
} }
@ -162,7 +173,7 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
web.SetPageManager(primaryPM) web.SetPageManager(primaryPM)
newDeck.SetBrightness(deviceBrightness(cfg, newDeck.Serial())) newDeck.SetBrightness(deviceBrightness(cfg, newDeck.Serial()))
asm.NotifyManualPage(cfg.DefaultPage) asm.NotifyManualPage(startPage)
asm.Resume() asm.Resume()
continue continue
} }
@ -288,7 +299,7 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
asm.Pause() asm.Pause()
primaryDeck.Close() primaryDeck.Close()
newDeck := reconnectDeck(ctx, cfg, &primaryPM) newDeck := reconnectDeck(ctx, cfg, &primaryPM, startPage)
if newDeck == nil { if newDeck == nil {
return ctx.Err() return ctx.Err()
} }
@ -301,7 +312,7 @@ func Run(ctx context.Context, cfg *config.Config, opts RunOptions) error {
web.SetPageManager(primaryPM) web.SetPageManager(primaryPM)
newDeck.SetBrightness(deviceBrightness(cfg, newDeck.Serial())) newDeck.SetBrightness(deviceBrightness(cfg, newDeck.Serial()))
asm.NotifyManualPage(cfg.DefaultPage) asm.NotifyManualPage(startPage)
asm.Resume() asm.Resume()
} }
@ -353,7 +364,7 @@ func deviceBrightness(cfg *config.Config, serial string) int {
return 75 return 75
} }
func reconnectDeck(ctx context.Context, cfg *config.Config, pm **PageManager) *Deck { func reconnectDeck(ctx context.Context, cfg *config.Config, pm **PageManager, startPage string) *Deck {
for { for {
newDeck, err := OpenDeck("") newDeck, err := OpenDeck("")
if err == nil { if err == nil {
@ -361,7 +372,7 @@ func reconnectDeck(ctx context.Context, cfg *config.Config, pm **PageManager) *D
(*pm).defaultFont = cfg.Font (*pm).defaultFont = cfg.Font
(*pm).showLabelBackground = cfg.ShowLabelBackground (*pm).showLabelBackground = cfg.ShowLabelBackground
(*pm).LoadPages(cfg.Pages) (*pm).LoadPages(cfg.Pages)
(*pm).ActivatePage(cfg.DefaultPage) (*pm).ActivatePage(startPage)
(*pm).startPeriodicKeys() (*pm).startPeriodicKeys()
return newDeck return newDeck
} }
@ -372,3 +383,13 @@ func reconnectDeck(ctx context.Context, cfg *config.Config, pm **PageManager) *D
} }
} }
} }
// pageExists reports whether pages contains a page with the given name.
func pageExists(pages []config.PageConfig, name string) bool {
for _, p := range pages {
if p.Name == name {
return true
}
}
return false
}

View File

@ -22,6 +22,7 @@ func main() {
fs := flag.NewFlagSet("streamdeck-lets-go", flag.ExitOnError) fs := flag.NewFlagSet("streamdeck-lets-go", flag.ExitOnError)
configPath := fs.String("config", "", "path to config.json (default: ~/.config/streamdeck-lets-go/config.json)") configPath := fs.String("config", "", "path to config.json (default: ~/.config/streamdeck-lets-go/config.json)")
httpAddr := fs.String("addr", ":9090", "web UI listen address") httpAddr := fs.String("addr", ":9090", "web UI listen address")
startPage := fs.String("page", "", "page to activate on startup (default: config's default_page)")
noDeck := fs.Bool("no-deck", false, "run without Stream Deck hardware (config editing only)") noDeck := fs.Bool("no-deck", false, "run without Stream Deck hardware (config editing only)")
verbose := fs.Bool("v", false, "verbose output") verbose := fs.Bool("v", false, "verbose output")
@ -71,6 +72,7 @@ func main() {
HTTPAddr: *httpAddr, HTTPAddr: *httpAddr,
HTTPEnabled: httpEnabled, HTTPEnabled: httpEnabled,
NoDeck: *noDeck, NoDeck: *noDeck,
StartPage: *startPage,
}); err != nil { }); err != nil {
slog.Error("run", "error", err) slog.Error("run", "error", err)
os.Exit(1) os.Exit(1)