mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2025-01-07 09:13:43 +01:00
WIP: implement reverse search
This commit is contained in:
parent
e292b4ce74
commit
a2c4c99af4
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module hs9001
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/peterh/liner v1.2.1
|
github.com/peterh/liner v1.2.1
|
||||||
github.com/tj/go-naturaldate v1.3.0
|
github.com/tj/go-naturaldate v1.3.0
|
||||||
modernc.org/sqlite v1.10.0
|
modernc.org/sqlite v1.10.0
|
||||||
)
|
)
|
||||||
|
77
history.go
Normal file
77
history.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tj/go-naturaldate"
|
||||||
|
)
|
||||||
|
|
||||||
|
type history struct {
|
||||||
|
conn *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *history) GetHistoryByPrefix(prefix string) (ph []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(h.conn, prefix+"%", "%", 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")
|
||||||
|
}
|
||||||
|
ph = append(ph, entry.cmd)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
|
||||||
|
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(h.conn, "%"+pattern+"%", "%", 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")
|
||||||
|
}
|
||||||
|
//mt.Printf("\nAppending: [%s] %s -- %d\n", pattern, entry.cmd, strings.Index(entry.cmd, pattern))
|
||||||
|
ph = append(ph, entry.cmd)
|
||||||
|
pos = append(pos, strings.Index(strings.ToLower(entry.cmd), strings.ToLower(pattern)))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *history) ReadHistory(r io.Reader) (num int, err error) {
|
||||||
|
panic("not implemented")
|
||||||
|
}
|
||||||
|
func (h *history) WriteHistory(w io.Writer) (num int, err error) {
|
||||||
|
panic("not implemented")
|
||||||
|
}
|
||||||
|
func (h *history) AppendHistory(item string) {
|
||||||
|
panic("not implemented")
|
||||||
|
}
|
||||||
|
func (h *history) ClearHistory() {
|
||||||
|
panic("not implemented")
|
||||||
|
}
|
||||||
|
func (h *history) RLock() {
|
||||||
|
//noop
|
||||||
|
}
|
||||||
|
func (h *history) RUnlock() {
|
||||||
|
//noop
|
||||||
|
}
|
11
main.go
11
main.go
@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -271,6 +272,7 @@ func main() {
|
|||||||
line := liner.NewLiner()
|
line := liner.NewLiner()
|
||||||
defer line.Close()
|
defer line.Close()
|
||||||
line.SetCtrlCAborts(true)
|
line.SetCtrlCAborts(true)
|
||||||
|
line.SetHistoryProvider(&history{conn: conn})
|
||||||
line.SetCompleter(func(line string) (c []string) {
|
line.SetCompleter(func(line string) (c []string) {
|
||||||
beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now())
|
beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -291,14 +293,19 @@ func main() {
|
|||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
if name, err := line.Prompt("What is your command? "); err == nil {
|
rdlineline := os.Getenv("READLINE_LINE")
|
||||||
log.Print("Got: ", name)
|
rdlinepos := os.Getenv("READLINE_POS")
|
||||||
|
rdlineposint, _ := strconv.Atoi(rdlinepos)
|
||||||
|
|
||||||
|
if name, err := line.PromptWithSuggestionReverse("", rdlineline, rdlineposint); err == nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "bash-enable":
|
case "bash-enable":
|
||||||
fmt.Printf(`
|
fmt.Printf(`
|
||||||
if [ -n "$PS1" ] ; then
|
if [ -n "$PS1" ] ; then
|
||||||
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
||||||
|
bind -x '"\C-r": " READLINE_LINE=$(hs9001 bash-ctrlr 3>&1 1>&2 2>&3) READLINE_POINT=0"'
|
||||||
fi
|
fi
|
||||||
`)
|
`)
|
||||||
case "bash-disable":
|
case "bash-disable":
|
||||||
|
Loading…
Reference in New Issue
Block a user