switch to using liner

This commit is contained in:
Hedy Li 2021-07-29 14:39:32 +08:00
parent 7e06170bbd
commit b9f595bd09
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
4 changed files with 56 additions and 27 deletions

View File

@ -2,13 +2,16 @@ package main
import (
"fmt"
"github.com/lmorg/readline"
"github.com/manifoldco/ansiwrap"
"golang.org/x/term"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"git.sr.ht/~adnano/go-xdg"
"github.com/manifoldco/ansiwrap"
ln "github.com/peterh/liner"
"golang.org/x/term"
)
type Page struct {
@ -23,8 +26,10 @@ type Client struct {
inputLinks []int // contains index to links in `links` that needs spartan input
history []*url.URL
conf *Config
inputReader *readline.Instance
mainReader *readline.Instance
mainReader *ln.State
inputReader *ln.State
promptHistory string
inputHistory string
}
func NewClient() *Client {
@ -38,9 +43,16 @@ func NewClient() *Client {
// c.history = make([]*url.URL, 100)
c.links = make([]string, 100)
c.conf = conf
c.mainReader = readline.NewInstance()
c.mainReader.SetPrompt(promptColor(c.conf.Prompt) + " ")
c.inputReader = readline.NewInstance()
c.mainReader = ln.NewLiner()
c.mainReader.SetCtrlCAborts(true)
c.inputReader = ln.NewLiner()
c.inputReader.SetCtrlCAborts(true)
c.promptHistory = filepath.Join(xdg.DataHome(), "gelim", "prompt_history.txt")
c.inputHistory = filepath.Join(xdg.DataHome(), "gelim", "input_history.txt")
// Create cache/data/runtime dirs
os.MkdirAll(filepath.Dir(c.promptHistory), 700)
os.MkdirAll(filepath.Dir(c.inputHistory), 700)
return &c
}
@ -156,16 +168,17 @@ func (c *Client) ParseGeminiPage(page *Page) string {
// Input handles Input status codes
func (c *Client) Input(u string, sensitive bool) (ok bool) {
c.inputReader.SetPrompt("INPUT> ")
var query string
var err error
// c.inputReader.SetMultiLineMode(true)
if sensitive {
c.inputReader.PasswordMask = '*'
oldHistory := c.inputReader.History
c.inputReader.History = new(readline.NullHistory)
defer func() { c.inputReader.PasswordMask = 0; c.inputReader.History = oldHistory }()
// TODO: no history
query, err = c.inputReader.PasswordPrompt("INPUT (sensitive)> ")
} else {
query, err = c.inputReader.Prompt("INPUT> ")
}
query, err := c.inputReader.Readline()
if err != nil {
if err == readline.CtrlC {
if err == ln.ErrPromptAborted {
fmt.Println(ErrorColor("\ninput cancelled"))
return false
}
@ -224,12 +237,12 @@ func (c *Client) HandleSpartanParsedURL(parsed *url.URL) bool {
case 2:
mediaType, params, err := ParseMeta(res.meta)
if err != nil {
fmt.Println(ErrorColor("Unable to parse header meta\"", res.meta, "\":", err))
fmt.Println(ErrorColor("Unable to parse header meta\"%s\": %s", res.meta, err))
return false
}
bodyBytes, err := ioutil.ReadAll(res.bodyReader)
if err != nil {
fmt.Println(ErrorColor("Unable to read body.", err))
fmt.Println(ErrorColor("Unable to read body. %s", err))
}
page.bodyBytes = bodyBytes
page.mediaType = mediaType
@ -271,12 +284,12 @@ func (c *Client) HandleGeminiParsedURL(parsed *url.URL) bool {
case 2:
mediaType, params, err := ParseMeta(res.meta)
if err != nil {
fmt.Println(ErrorColor("Unable to parse header meta\"", res.meta, "\":", err))
fmt.Println(ErrorColor("Unable to parse header meta\"%s\": %s", res.meta, err))
return false
}
bodyBytes, err := ioutil.ReadAll(res.bodyReader)
if err != nil {
fmt.Println(ErrorColor("Unable to read body.", err))
fmt.Println(ErrorColor("Unable to read body. %s", err))
}
page.bodyBytes = bodyBytes
page.mediaType = mediaType

View File

@ -11,7 +11,7 @@ import (
"strings"
"github.com/fatih/color"
"github.com/lmorg/readline"
ln "github.com/peterh/liner"
flag "github.com/spf13/pflag"
)
@ -138,17 +138,30 @@ func main() {
// and now here comes the line-mode prompts and stuff
rl := c.mainReader
if f, err := os.Open(c.promptHistory); err == nil {
rl.ReadHistory(f)
f.Close()
}
defer func() {
if f, err := os.Open(c.promptHistory); err != nil {
fmt.Println(ErrorColor("Error writing to history: %s", err.Error()))
} else {
rl.WriteHistory(f)
}
rl.Close()
}()
for {
line, err := rl.Readline()
line, err := rl.Prompt(c.conf.Prompt + " ")
if err != nil {
if err == readline.CtrlC {
os.Exit(0)
if err == ln.ErrPromptAborted {
os.Exit(1)
}
fmt.Println(ErrorColor("\nerror reading line input"))
fmt.Println(ErrorColor(err.Error()))
continue
os.Exit(1) // Exiting because it will cause an infinite loop of error if used 'continue' here
}
rl.AppendHistory(line)
line = strings.TrimSpace(line)
if line == "" {
continue

3
go.mod
View File

@ -6,8 +6,9 @@ require (
git.sr.ht/~adnano/go-xdg v0.1.0
github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.10.0
github.com/lmorg/readline v0.0.0-20210316231630-be4b7d79fc3a
github.com/manifoldco/ansiwrap v1.1.0
github.com/peterh/liner v1.2.1
github.com/spf13/pflag v1.0.5
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa // indirect
golang.org/x/term v0.0.0-20210429154555-c04ba851c2a4
)

6
go.sum
View File

@ -4,14 +4,16 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/lmorg/readline v0.0.0-20210316231630-be4b7d79fc3a h1:xiaNGfMrhqdK89RvLnc6WAe4Pe/HKaluZmipsYF3E0o=
github.com/lmorg/readline v0.0.0-20210316231630-be4b7d79fc3a/go.mod h1:DidBcghSSS00hj06CkehMGDhZ/25vZDdydkeTVUy6lY=
github.com/manifoldco/ansiwrap v1.1.0 h1:NS3ZV8cCh+dG/sM4e4wM0WSMFWHhSuVFNnwtx6qrqyU=
github.com/manifoldco/ansiwrap v1.1.0/go.mod h1:2yA5wi1rF9kAuD6RbwON60QdT46zrKhIS+KHbNwwy8U=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/peterh/liner v1.2.1 h1:O4BlKaq/LWu6VRWmol4ByWfzx6MfXc5Op5HETyIy5yg=
github.com/peterh/liner v1.2.1/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=