view/edit, fix wsh editor (this is the one for EDITOR var)

This commit is contained in:
sawka 2024-09-03 10:44:16 -07:00
parent 9d344b899d
commit 0084f8eb97
2 changed files with 32 additions and 19 deletions

View File

@ -16,33 +16,21 @@ import (
var editMagnified bool var editMagnified bool
var editCmd = &cobra.Command{ var editorCmd = &cobra.Command{
Use: "edit", Use: "editor",
Short: "edit a file", Short: "edit a file (blocks until editor is closed)",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: editRun, Run: editorRun,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
} }
func init() { func init() {
editCmd.Flags().BoolVarP(&editMagnified, "magnified", "m", false, "open view in magnified mode") editCmd.Flags().BoolVarP(&editMagnified, "magnified", "m", false, "open view in magnified mode")
rootCmd.AddCommand(editCmd) rootCmd.AddCommand(editorCmd)
} }
func editRun(cmd *cobra.Command, args []string) { func editorRun(cmd *cobra.Command, args []string) {
fileArg := args[0] fileArg := args[0]
wshCmd := wshrpc.CommandCreateBlockData{
BlockDef: &waveobj.BlockDef{
Meta: map[string]any{
waveobj.MetaKey_View: "preview",
waveobj.MetaKey_File: fileArg,
},
},
Magnified: editMagnified,
}
if RpcContext.Conn != "" {
wshCmd.BlockDef.Meta[waveobj.MetaKey_Connection] = RpcContext.Conn
}
absFile, err := filepath.Abs(fileArg) absFile, err := filepath.Abs(fileArg)
if err != nil { if err != nil {
WriteStderr("[error] getting absolute path: %v\n", err) WriteStderr("[error] getting absolute path: %v\n", err)
@ -57,6 +45,19 @@ func editRun(cmd *cobra.Command, args []string) {
WriteStderr("[error] getting file info: %v\n", err) WriteStderr("[error] getting file info: %v\n", err)
return return
} }
wshCmd := wshrpc.CommandCreateBlockData{
BlockDef: &waveobj.BlockDef{
Meta: map[string]any{
waveobj.MetaKey_View: "preview",
waveobj.MetaKey_File: absFile,
waveobj.MetaKey_Edit: true,
},
},
Magnified: editMagnified,
}
if RpcContext.Conn != "" {
wshCmd.BlockDef.Meta[waveobj.MetaKey_Connection] = RpcContext.Conn
}
blockRef, err := wshclient.CreateBlockCommand(RpcClient, wshCmd, &wshrpc.RpcOpts{Timeout: 2000}) blockRef, err := wshclient.CreateBlockCommand(RpcClient, wshCmd, &wshrpc.RpcOpts{Timeout: 2000})
if err != nil { if err != nil {
WriteStderr("[error] running view command: %v\r\n", err) WriteStderr("[error] running view command: %v\r\n", err)

View File

@ -18,7 +18,15 @@ var viewMagnified bool
var viewCmd = &cobra.Command{ var viewCmd = &cobra.Command{
Use: "view", Use: "view",
Short: "preview a file or directory", Short: "preview/edit a file or directory",
Args: cobra.ExactArgs(1),
Run: viewRun,
PreRunE: preRunSetupRpcClient,
}
var editCmd = &cobra.Command{
Use: "edit",
Short: "preview/edit a file or directory",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: viewRun, Run: viewRun,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
@ -27,6 +35,7 @@ var viewCmd = &cobra.Command{
func init() { func init() {
viewCmd.Flags().BoolVarP(&viewMagnified, "magnified", "m", false, "open view in magnified mode") viewCmd.Flags().BoolVarP(&viewMagnified, "magnified", "m", false, "open view in magnified mode")
rootCmd.AddCommand(viewCmd) rootCmd.AddCommand(viewCmd)
rootCmd.AddCommand(editCmd)
} }
func viewRun(cmd *cobra.Command, args []string) { func viewRun(cmd *cobra.Command, args []string) {
@ -67,6 +76,9 @@ func viewRun(cmd *cobra.Command, args []string) {
}, },
Magnified: viewMagnified, Magnified: viewMagnified,
} }
if cmd.Use == "edit" {
wshCmd.BlockDef.Meta[waveobj.MetaKey_Edit] = true
}
if conn != "" { if conn != "" {
wshCmd.BlockDef.Meta[waveobj.MetaKey_Connection] = conn wshCmd.BlockDef.Meta[waveobj.MetaKey_Connection] = conn
} }