|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/wavetermdev/waveterm/pkg/waveobj" |
| 12 | + "github.com/wavetermdev/waveterm/pkg/wps" |
| 13 | + "github.com/wavetermdev/waveterm/pkg/wshrpc" |
| 14 | + "github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient" |
| 15 | +) |
| 16 | + |
| 17 | +var tabIndicatorCmd = &cobra.Command{ |
| 18 | + Use: "tabindicator [icon]", |
| 19 | + Short: "set or clear a tab indicator", |
| 20 | + Args: cobra.MaximumNArgs(1), |
| 21 | + RunE: tabIndicatorRun, |
| 22 | + PreRunE: preRunSetupRpcClient, |
| 23 | +} |
| 24 | + |
| 25 | +var ( |
| 26 | + tabIndicatorTabId string |
| 27 | + tabIndicatorColor string |
| 28 | + tabIndicatorPriority float64 |
| 29 | + tabIndicatorClear bool |
| 30 | + tabIndicatorPersistent bool |
| 31 | + tabIndicatorBeep bool |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + rootCmd.AddCommand(tabIndicatorCmd) |
| 36 | + tabIndicatorCmd.Flags().StringVar(&tabIndicatorTabId, "tabid", "", "tab id (defaults to WAVETERM_TABID)") |
| 37 | + tabIndicatorCmd.Flags().StringVar(&tabIndicatorColor, "color", "", "indicator color") |
| 38 | + tabIndicatorCmd.Flags().Float64Var(&tabIndicatorPriority, "priority", 0, "indicator priority") |
| 39 | + tabIndicatorCmd.Flags().BoolVar(&tabIndicatorClear, "clear", false, "clear the indicator") |
| 40 | + tabIndicatorCmd.Flags().BoolVar(&tabIndicatorPersistent, "persistent", false, "make indicator persistent (don't clear on focus)") |
| 41 | + tabIndicatorCmd.Flags().BoolVar(&tabIndicatorBeep, "beep", false, "play system bell sound") |
| 42 | +} |
| 43 | + |
| 44 | +func tabIndicatorRun(cmd *cobra.Command, args []string) (rtnErr error) { |
| 45 | + defer func() { |
| 46 | + sendActivity("tabindicator", rtnErr == nil) |
| 47 | + }() |
| 48 | + |
| 49 | + tabId := tabIndicatorTabId |
| 50 | + if tabId == "" { |
| 51 | + tabId = os.Getenv("WAVETERM_TABID") |
| 52 | + } |
| 53 | + if tabId == "" { |
| 54 | + return fmt.Errorf("no tab id specified (use --tabid or set WAVETERM_TABID)") |
| 55 | + } |
| 56 | + |
| 57 | + var indicator *wshrpc.TabIndicator |
| 58 | + if !tabIndicatorClear { |
| 59 | + icon := "bell" |
| 60 | + if len(args) > 0 { |
| 61 | + icon = args[0] |
| 62 | + } |
| 63 | + indicator = &wshrpc.TabIndicator{ |
| 64 | + Icon: icon, |
| 65 | + Color: tabIndicatorColor, |
| 66 | + Priority: tabIndicatorPriority, |
| 67 | + ClearOnFocus: !tabIndicatorPersistent, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + eventData := wshrpc.TabIndicatorEventData{ |
| 72 | + TabId: tabId, |
| 73 | + Indicator: indicator, |
| 74 | + } |
| 75 | + |
| 76 | + event := wps.WaveEvent{ |
| 77 | + Event: wps.Event_TabIndicator, |
| 78 | + Scopes: []string{waveobj.MakeORef(waveobj.OType_Tab, tabId).String()}, |
| 79 | + Data: eventData, |
| 80 | + } |
| 81 | + |
| 82 | + err := wshclient.EventPublishCommand(RpcClient, event, &wshrpc.RpcOpts{NoResponse: true}) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("publishing tab indicator event: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if tabIndicatorBeep { |
| 88 | + err = wshclient.ElectronSystemBellCommand(RpcClient, &wshrpc.RpcOpts{Route: "electron"}) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("playing system bell: %v", err) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if tabIndicatorClear { |
| 95 | + fmt.Printf("tab indicator cleared\n") |
| 96 | + } else { |
| 97 | + fmt.Printf("tab indicator set\n") |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments