From 26ababc93ee0aa91046647e5f2325e6310d9a70d Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 5 Jun 2021 17:41:58 +0200 Subject: [PATCH] Begin liner integration --- go.mod | 1 + go.sum | 5 ++++- main.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index af93227..931bcd0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module hs9001 go 1.16 require ( + github.com/peterh/liner v1.2.1 github.com/tj/go-naturaldate v1.3.0 modernc.org/sqlite v1.10.0 ) diff --git a/go.sum b/go.sum index 649744e..4499ffa 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,12 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNU github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= 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/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/peterh/liner v1.2.1 h1:O4BlKaq/LWu6VRWmol4ByWfzx6MfXc5Op5HETyIy5yg= +github.com/peterh/liner v1.2.1/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= @@ -50,7 +54,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index a726e4d..bd8a42d 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "strings" "time" + "github.com/peterh/liner" "github.com/tj/go-naturaldate" _ "modernc.org/sqlite" ) @@ -266,6 +267,34 @@ func main() { migrateDatabase(conn, fetchDBVersion(conn)) switch cmd { + case "bash-ctrlr": + line := liner.NewLiner() + defer line.Close() + line.SetCtrlCAborts(true) + line.SetCompleter(func(line string) (c []string) { + beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now()) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error()) + } + endTimeStamp, err := naturaldate.Parse("now", time.Now()) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error()) + } + results := search(conn, "%"+line+"%", "%", beginTimestamp, endTimeStamp, -9001) + for e := results.Front(); e != nil; e = e.Next() { + entry, ok := e.Value.(*HistoryEntry) + if !ok { + log.Panic("Failed to retrieve entries") + } + c = append(c, entry.cmd) + } + return + }) + + if name, err := line.Prompt("What is your command? "); err == nil { + log.Print("Got: ", name) + } + case "bash-enable": fmt.Printf(` if [ -n "$PS1" ] ; then