summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2024-01-10 11:02:57 -0700
committertjp <tjp@ctrl-c.club>2024-01-10 11:02:57 -0700
commita69742c81c7d46a71adce5d39da28a5b839d5716 (patch)
tree138cd9418163db4b66b262fc47c209d40fa7b579
parenta44a063c1a60ca11066d21d49aca95cfed7d3499 (diff)
cli goodies
-h to print "help cli" output -c CMDS to run commands in quiet mode and then exit execute with URL as positional argument to go there and start the interactive session
-rw-r--r--help.go21
-rw-r--r--main.go56
2 files changed, 64 insertions, 13 deletions
diff --git a/help.go b/help.go
index 3d5cd88..ba45cf0 100644
--- a/help.go
+++ b/help.go
@@ -26,8 +26,9 @@ var helpTopics = map[string]string{
"topics": `
help topics
-----------
-commands: Basics of x-1 commands, and a full listing of them. Each
- command also has its own help topic.
+commands: Basics of x-1 commands, and a full listing of them. Each command
+ also has its own help topic.
+cli: Flags and options available when invoking x-1 on the command line.
urls: The forms of URLs which can be entered into x-1 commands.
mark: Information on the bookmarks and the "mark" meta-command.
tour: Information about tours and the "tour" meta-command.
@@ -62,6 +63,22 @@ Typing just any URL is interpreted as a "go" command to that URL. See
Consult "help COMMAND" for more information on any single command.
`[1:],
+ "cli": `
+x-1 [-c COMMANDS] [URL]
+-----------------------
+With no arguments or flags, x-1 will just display the prompt and begin
+executing your commands. Use the "help" command to begin exploring this
+interactive mode.
+
+With the -c flag, it will execute the provided commands (multiple may be
+provided by separating them with a semi-colon ';') and then exit. In
+this mode it also forces quiet mode, in which it doesn't automatically
+print loaded pages.
+
+With a URL argument, it will begin an interactive prompt session by
+loading the requested url.
+`[1:],
+
"urls": `
x-1 urls
--------
diff --git a/main.go b/main.go
index 0c1e7da..7ad9b4a 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "flag"
"fmt"
"io"
"log"
@@ -10,6 +11,9 @@ import (
"github.com/chzyer/readline"
)
+var cmdMode = flag.String("c", "", "")
+var helpMode = flag.Bool("h", false, "")
+
func main() {
conf, err := getConfig()
if err != nil {
@@ -22,11 +26,6 @@ func main() {
state := NewBrowserState(conf)
- rl, err := readline.New(Prompt)
- if err != nil {
- log.Fatal(err)
- }
-
marks, err := getMarks()
if err != nil {
log.Fatal(err)
@@ -45,11 +44,40 @@ func main() {
}
state.Identities = idents
+ flag.Parse()
+
+ if *helpMode {
+ if err := Help(state, "cli"); err != nil {
+ writeError(err.Error())
+ }
+ return
+ }
+
+ if *cmdMode != "" {
+ conf.Quiet = true
+ state.Quiet = true
+ if err := handleCmdLine(state, conf, *cmdMode); err != nil {
+ writeError(err.Error())
+ }
+ return
+ }
+
+ rl, err := readline.New(Prompt)
+ if err != nil {
+ log.Fatal(err)
+ }
+
if conf.VimKeys {
rl.SetVimMode(true)
}
state.Readline = rl
+ if urls := flag.Args(); len(urls) > 0 {
+ if err := Go(state, urls[0], conf); err != nil {
+ writeError(err.Error())
+ }
+ }
+
for {
rl.SetPrompt(Prompt)
line, err := rl.Readline()
@@ -60,15 +88,21 @@ func main() {
log.Fatal(err)
}
- for _, cmd := range strings.Split(line, ";") {
- if c, err := ParseCommand(strings.TrimSpace(cmd)); err != nil {
- writeError(err.Error())
- } else if err := RunCommand(conf, c, state); err != nil {
- writeError(err.Error())
- }
+ if err := handleCmdLine(state, conf, line); err != nil {
+ writeError(err.Error())
}
+ }
+}
+func handleCmdLine(state *BrowserState, conf *Config, line string) error {
+ for _, cmd := range strings.Split(line, ";") {
+ if c, err := ParseCommand(strings.TrimSpace(cmd)); err != nil {
+ return err
+ } else if err := RunCommand(conf, c, state); err != nil {
+ return err
+ }
}
+ return nil
}
const Prompt = promptStyle + "X-1" + ansiClear + "> "