鏡像自
				https://github.com/quitesimpleorg/hs9001.git
				已同步 2025-11-04 03:39:30 +01:00 
			
		
		
		
	WIP: implement reverse search
此提交包含在:
		
							
								
								
									
										77
									
								
								history.go
									
									
									
									
									
										一般檔案
									
								
							
							
						
						
									
										77
									
								
								history.go
									
									
									
									
									
										一般檔案
									
								
							@@ -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"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@@ -271,6 +272,7 @@ func main() {
 | 
			
		||||
		line := liner.NewLiner()
 | 
			
		||||
		defer line.Close()
 | 
			
		||||
		line.SetCtrlCAborts(true)
 | 
			
		||||
		line.SetHistoryProvider(&history{conn: conn})
 | 
			
		||||
		line.SetCompleter(func(line string) (c []string) {
 | 
			
		||||
			beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
@@ -291,14 +293,19 @@ func main() {
 | 
			
		||||
			return
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
		if name, err := line.Prompt("What is your command? "); err == nil {
 | 
			
		||||
			log.Print("Got: ", name)
 | 
			
		||||
		rdlineline := os.Getenv("READLINE_LINE")
 | 
			
		||||
		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":
 | 
			
		||||
		fmt.Printf(`
 | 
			
		||||
			if [ -n "$PS1" ] ; then
 | 
			
		||||
				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
 | 
			
		||||
		`)
 | 
			
		||||
	case "bash-disable":
 | 
			
		||||
 
 | 
			
		||||
		新增問題並參考
	
	封鎖使用者