From ceaaaaf695afab718d98a92a4c3a3b7569261a25 Mon Sep 17 00:00:00 2001 From: tjp Date: Thu, 4 Jan 2024 12:47:52 -0700 Subject: back and forward by N --- command.go | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'command.go') diff --git a/command.go b/command.go index ea7ee7c..c3396b2 100644 --- a/command.go +++ b/command.go @@ -43,11 +43,35 @@ func ParseCommand(line string) (*Command, error) { } case 'b': if strings.HasPrefix("back", cmd) { - return &Command{Name: "back"}, nil + fields := strings.Fields(rest) + switch len(fields) { + case 0: + return &Command{Name: "back"}, nil + case 1: + if _, err := strconv.Atoi(fields[0]); err != nil { + return nil, ErrInvalidArgs + } + return &Command{Name: "back", Args: fields}, nil + default: + return nil, ErrInvalidArgs + + } } case 'f': if strings.HasPrefix("forward", cmd) { - return &Command{Name: "forward"}, nil + fields := strings.Fields(rest) + switch len(fields) { + case 0: + return &Command{Name: "forward"}, nil + case 1: + if _, err := strconv.Atoi(fields[0]); err != nil { + return nil, ErrInvalidArgs + } + return &Command{Name: "forward", Args: fields}, nil + default: + return nil, ErrInvalidArgs + + } } case 'n': if strings.HasPrefix("next", cmd) { @@ -270,9 +294,17 @@ func RunCommand(conf *Config, cmd *Command, state *BrowserState) error { case "reload": return Reload(state, conf) case "back": - return Back(state) + num := 1 + if len(cmd.Args) == 1 { + num, _ = strconv.Atoi(cmd.Args[0]) + } + return Back(state, num) case "forward": - return Forward(state) + num := 1 + if len(cmd.Args) == 1 { + num, _ = strconv.Atoi(cmd.Args[0]) + } + return Forward(state, num) case "next": return Next(state, conf) case "previous": -- cgit v1.2.3