mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
Add a Notification Subcommand to Wsh (#985)
This allows users to use the `wsh notify` command to make a popup notification, even from a remote connection.
This commit is contained in:
parent
d2b2491211
commit
c5527dd87b
41
cmd/wsh/cmd/wshcmd-notify.go
Normal file
41
cmd/wsh/cmd/wshcmd-notify.go
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2024, Command Line Inc.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
||||
"github.com/wavetermdev/waveterm/pkg/wshutil"
|
||||
)
|
||||
|
||||
var notifyTitle string
|
||||
var notifySilent bool
|
||||
|
||||
var setNotifyCmd = &cobra.Command{
|
||||
Use: "notify <message> [-t <title>] [-s]",
|
||||
Short: "create a notification",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: notifyRun,
|
||||
PreRunE: preRunSetupRpcClient,
|
||||
}
|
||||
|
||||
func init() {
|
||||
setNotifyCmd.Flags().StringVarP(¬ifyTitle, "title", "t", "Wsh Notify", "the notification title")
|
||||
setNotifyCmd.Flags().BoolVarP(¬ifySilent, "silent", "s", false, "whether or not the notification sound is silenced")
|
||||
rootCmd.AddCommand(setNotifyCmd)
|
||||
}
|
||||
|
||||
func notifyRun(cmd *cobra.Command, args []string) {
|
||||
message := args[0]
|
||||
notificationOptions := &wshrpc.WaveNotificationOptions{
|
||||
Title: notifyTitle,
|
||||
Body: message,
|
||||
Silent: notifySilent,
|
||||
}
|
||||
_, err := RpcClient.SendRpcRequest(wshrpc.Command_Notify, notificationOptions, &wshrpc.RpcOpts{Timeout: 2000, Route: wshutil.ElectronRoute})
|
||||
if err != nil {
|
||||
WriteStderr("[error] sending notification: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
@ -28,6 +28,14 @@ export class ElectronWshClientType extends WshClient {
|
||||
const rtn = await webGetSelector(wc, data.selector, data.opts);
|
||||
return rtn;
|
||||
}
|
||||
|
||||
async handle_notify(rh: RpcResponseHelper, notificationOptions: WaveNotificationOptions) {
|
||||
new electron.Notification({
|
||||
title: notificationOptions.title,
|
||||
body: notificationOptions.body,
|
||||
silent: notificationOptions.silent,
|
||||
}).show();
|
||||
}
|
||||
}
|
||||
|
||||
export let ElectronWshClient: ElectronWshClientType;
|
||||
|
@ -132,6 +132,11 @@ class RpcApiType {
|
||||
return client.wshRpcCall("message", data, opts);
|
||||
}
|
||||
|
||||
// command "notify" [call]
|
||||
NotifyCommand(client: WshClient, data: WaveNotificationOptions, opts?: RpcOpts): Promise<void> {
|
||||
return client.wshRpcCall("notify", data, opts);
|
||||
}
|
||||
|
||||
// command "remotefiledelete" [call]
|
||||
RemoteFileDeleteCommand(client: WshClient, data: string, opts?: RpcOpts): Promise<void> {
|
||||
return client.wshRpcCall("remotefiledelete", data, opts);
|
||||
|
7
frontend/types/gotypes.d.ts
vendored
7
frontend/types/gotypes.d.ts
vendored
@ -632,6 +632,13 @@ declare global {
|
||||
meta: {[key: string]: any};
|
||||
};
|
||||
|
||||
// wshrpc.WaveNotificationOptions
|
||||
type WaveNotificationOptions = {
|
||||
title?: string;
|
||||
body?: string;
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
// waveobj.WaveObj
|
||||
type WaveObj = {
|
||||
otype: string;
|
||||
|
@ -163,6 +163,12 @@ func MessageCommand(w *wshutil.WshRpc, data wshrpc.CommandMessageData, opts *wsh
|
||||
return err
|
||||
}
|
||||
|
||||
// command "notify", wshserver.NotifyCommand
|
||||
func NotifyCommand(w *wshutil.WshRpc, data wshrpc.WaveNotificationOptions, opts *wshrpc.RpcOpts) error {
|
||||
_, err := sendRpcRequestCallHelper[any](w, "notify", data, opts)
|
||||
return err
|
||||
}
|
||||
|
||||
// command "remotefiledelete", wshserver.RemoteFileDeleteCommand
|
||||
func RemoteFileDeleteCommand(w *wshutil.WshRpc, data string, opts *wshrpc.RpcOpts) error {
|
||||
_, err := sendRpcRequestCallHelper[any](w, "remotefiledelete", data, opts)
|
||||
|
@ -68,6 +68,7 @@ const (
|
||||
Command_ConnList = "connlist"
|
||||
|
||||
Command_WebSelector = "webselector"
|
||||
Command_Notify = "notify"
|
||||
)
|
||||
|
||||
type RespOrErrorUnion[T any] struct {
|
||||
@ -126,6 +127,7 @@ type WshRpcInterface interface {
|
||||
RemoteStreamCpuDataCommand(ctx context.Context) chan RespOrErrorUnion[TimeSeriesData]
|
||||
|
||||
WebSelectorCommand(ctx context.Context, data CommandWebSelectorData) ([]string, error)
|
||||
NotifyCommand(ctx context.Context, notificationOptions WaveNotificationOptions) error
|
||||
}
|
||||
|
||||
// for frontend
|
||||
@ -374,3 +376,9 @@ type BlockInfoData struct {
|
||||
WindowId string `json:"windowid"`
|
||||
Meta waveobj.MetaMapType `json:"meta"`
|
||||
}
|
||||
|
||||
type WaveNotificationOptions struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Body string `json:"body,omitempty"`
|
||||
Silent bool `json:"silent,omitempty"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user