PE-117, PE-116, PE-149

This commit is contained in:
sawka 2023-09-16 11:15:09 -07:00
parent 54c2087ad6
commit de04a700d1
2 changed files with 103 additions and 1 deletions

View File

@ -219,6 +219,10 @@ func init() {
// CodeEditCommand is overloaded to do codeedit and codeview
registerCmdFn("codeedit", CodeEditCommand)
registerCmdFn("codeview", CodeEditCommand)
registerCmdFn("imageview", ImageViewCommand)
registerCmdFn("mdview", MarkdownViewCommand)
registerCmdFn("markdownview", MarkdownViewCommand)
}
func getValidCommands() []string {
@ -3152,6 +3156,80 @@ func CodeEditCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst
return update, nil
}
func ImageViewCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
if len(pk.Args) == 0 {
return nil, fmt.Errorf("%s requires an argument (file name)", GetCmdStr(pk))
}
// TODO more error checking on filename format?
if pk.Args[0] == "" {
return nil, fmt.Errorf("%s argument cannot be empty", GetCmdStr(pk))
}
ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_RemoteConnected)
if err != nil {
return nil, err
}
outputStr := fmt.Sprintf("%s %q", GetCmdStr(pk), pk.Args[0])
cmd, err := makeStaticCmd(ctx, GetCmdStr(pk), ids, pk.GetRawStr(), []byte(outputStr))
if err != nil {
// TODO tricky error since the command was a success, but we can't show the output
return nil, err
}
update, err := addLineForCmd(ctx, "/"+GetCmdStr(pk), false, ids, cmd, "image")
if err != nil {
// TODO tricky error since the command was a success, but we can't show the output
return nil, err
}
// set the line state
// TODO turn these strings into constants
lineState := make(map[string]any)
lineState["prompt:source"] = "file"
lineState["prompt:file"] = pk.Args[0]
err = sstore.UpdateLineState(ctx, ids.ScreenId, update.Line.LineId, lineState)
if err != nil {
return nil, fmt.Errorf("%s error updating line state: %v", GetCmdStr(pk), err)
}
update.Line.LineState = lineState
update.Interactive = pk.Interactive
return update, nil
}
func MarkdownViewCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
if len(pk.Args) == 0 {
return nil, fmt.Errorf("%s requires an argument (file name)", GetCmdStr(pk))
}
// TODO more error checking on filename format?
if pk.Args[0] == "" {
return nil, fmt.Errorf("%s argument cannot be empty", GetCmdStr(pk))
}
ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_RemoteConnected)
if err != nil {
return nil, err
}
outputStr := fmt.Sprintf("%s %q", GetCmdStr(pk), pk.Args[0])
cmd, err := makeStaticCmd(ctx, GetCmdStr(pk), ids, pk.GetRawStr(), []byte(outputStr))
if err != nil {
// TODO tricky error since the command was a success, but we can't show the output
return nil, err
}
update, err := addLineForCmd(ctx, "/"+GetCmdStr(pk), false, ids, cmd, "markdown")
if err != nil {
// TODO tricky error since the command was a success, but we can't show the output
return nil, err
}
// set the line state
// TODO turn these strings into constants
lineState := make(map[string]any)
lineState["prompt:source"] = "file"
lineState["prompt:file"] = pk.Args[0]
err = sstore.UpdateLineState(ctx, ids.ScreenId, update.Line.LineId, lineState)
if err != nil {
return nil, fmt.Errorf("%s error updating line state: %v", GetCmdStr(pk), err)
}
update.Line.LineState = lineState
update.Interactive = pk.Interactive
return update, nil
}
func EditTestCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
if len(pk.Args) == 0 {
return nil, fmt.Errorf("/edit:test requires an argument (file name)")

View File

@ -28,6 +28,9 @@ var BareMetaCmds = []BareMetaCmdDecl{
BareMetaCmdDecl{"reset", "reset"},
BareMetaCmdDecl{"codeedit", "codeedit"},
BareMetaCmdDecl{"codeview", "codeview"},
BareMetaCmdDecl{"imageview", "imageview"},
BareMetaCmdDecl{"markdownview", "markdownview"},
BareMetaCmdDecl{"mdview", "markdownview"},
}
const (
@ -241,6 +244,27 @@ func EvalBracketArgs(origCmdStr string) (map[string]string, string, error) {
return rtn, restStr, nil
}
func unescapeBackSlashes(s string) string {
if strings.Index(s, "\\") == -1 {
return s
}
var newStr []rune
var lastSlash bool
for _, r := range s {
if lastSlash {
lastSlash = false
newStr = append(newStr, r)
continue
}
if r == '\\' {
lastSlash = true
continue
}
newStr = append(newStr, r)
}
return string(newStr)
}
func EvalMetaCommand(ctx context.Context, origPk *scpacket.FeCommandPacketType) (*scpacket.FeCommandPacketType, error) {
if len(origPk.Args) == 0 {
return nil, fmt.Errorf("empty command (no fields)")
@ -299,7 +323,7 @@ func EvalMetaCommand(ctx context.Context, origPk *scpacket.FeCommandPacketType)
rtnPk.Kwargs[varName] = varVal
continue
}
rtnPk.Args = append(rtnPk.Args, literalVal)
rtnPk.Args = append(rtnPk.Args, unescapeBackSlashes(literalVal))
}
if resolveBool(rtnPk.Kwargs["dump"], false) {
DumpPacket(rtnPk)