鏡像自
https://github.com/quitesimpleorg/hs9001.git
已同步 2025-07-07 23:14:56 +02:00
比較提交
2 次程式碼提交
v0.4
...
3a6a1b2aa9
作者 | SHA1 | 日期 | |
---|---|---|---|
3a6a1b2aa9 | |||
1a6c75cea9 |
53
README.md
53
README.md
@ -2,15 +2,33 @@
|
||||
hs9001 (history search 9001) is an easy, quite simple bash history enhancement. It simply writes all
|
||||
your bash commands into an sqlite database. You can then search this database.
|
||||
|
||||
It improves over bash's built-in history mechanism as it'll aggregate the shell history of all open shells,
|
||||
timestamp them and also record additional information such as the directory a command was executed in.
|
||||
|
||||
## Usage / Examples
|
||||
### Search
|
||||
|
||||
```
|
||||
hs [search terms]
|
||||
```
|
||||
You can further filter with options like `-cwd`, `-after` and so on...
|
||||
For a full list, see `-help`.
|
||||
|
||||
```
|
||||
hs -cwd .
|
||||
```
|
||||
Lists all commands ever entered in this directory
|
||||
|
||||
```
|
||||
hs -after yesterday -cwd . git
|
||||
```
|
||||
Lists all git commands in the current directory which have been entered today.
|
||||
|
||||
Also, it (by default) replaces bash's built-in CTRL-R mechanism, so hs9001's database will be used instead
|
||||
of bash's limited history files.
|
||||
|
||||
## Install
|
||||
|
||||
### From source
|
||||
```
|
||||
go build
|
||||
#move hs9001 to a PATH location
|
||||
```
|
||||
|
||||
### Debian / Ubuntu
|
||||
Latest release can be installed using apt
|
||||
```
|
||||
@ -28,31 +46,26 @@ apk update
|
||||
apk add hs9001
|
||||
```
|
||||
|
||||
### From source
|
||||
```
|
||||
go build
|
||||
#move hs9001 to a PATH location
|
||||
```
|
||||
|
||||
### Setup / Config
|
||||
## Setup / Config
|
||||
|
||||
Add this to .bashrc
|
||||
|
||||
```
|
||||
eval "$(hs9001 bash-enable)"
|
||||
```
|
||||
|
||||
This will also create a `hs`alias so you have to type less in everyday usage.
|
||||
|
||||
By default, every system user gets his own database. You can override this by setting the environment variable
|
||||
for all users that should write to your unified database.
|
||||
|
||||
```
|
||||
export HS9001_DB_PATH="/home/db/history.sqlite"
|
||||
```
|
||||
|
||||
## Usage
|
||||
### Search
|
||||
|
||||
```
|
||||
hs9001 search [search terms]
|
||||
```
|
||||
|
||||
It is recommended to create an alias for search to make life easier, e. g.:
|
||||
|
||||
```
|
||||
alias searchh='hs9001 search'
|
||||
```
|
||||
|
||||
|
22
history.go
22
history.go
@ -12,16 +12,15 @@ type history struct {
|
||||
}
|
||||
|
||||
func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
||||
/* Hack for performance reasons */
|
||||
if len(prefix) < 2 {
|
||||
return
|
||||
}
|
||||
opts := searchopts{}
|
||||
opts.order = "DESC"
|
||||
o := "DESC"
|
||||
opts.order = &o
|
||||
lim := 100
|
||||
opts.limit = &lim
|
||||
cmdqry := prefix + "%"
|
||||
opts.command = &cmdqry
|
||||
results := search(h.conn, opts)
|
||||
for e := results.Front(); e != nil; e = e.Next() {
|
||||
for e := results.Back(); e != nil; e = e.Prev() {
|
||||
entry, ok := e.Value.(*HistoryEntry)
|
||||
if !ok {
|
||||
log.Panic("Failed to retrieve entries")
|
||||
@ -31,16 +30,15 @@ func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
||||
return
|
||||
}
|
||||
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
|
||||
/* Hack for performance reasons */
|
||||
if len(pattern) < 2 {
|
||||
return
|
||||
}
|
||||
opts := searchopts{}
|
||||
opts.order = "DESC"
|
||||
o := "DESC"
|
||||
opts.order = &o
|
||||
lim := 100
|
||||
opts.limit = &lim
|
||||
cmdqry := "%" + pattern + "%"
|
||||
opts.command = &cmdqry
|
||||
results := search(h.conn, opts)
|
||||
for e := results.Front(); e != nil; e = e.Next() {
|
||||
for e := results.Back(); e != nil; e = e.Prev() {
|
||||
entry, ok := e.Value.(*HistoryEntry)
|
||||
if !ok {
|
||||
log.Panic("Failed to retrieve entries")
|
||||
|
20
main.go
20
main.go
@ -157,7 +157,8 @@ type searchopts struct {
|
||||
after *time.Time
|
||||
before *time.Time
|
||||
retval *int
|
||||
order string
|
||||
order *string
|
||||
limit *int
|
||||
}
|
||||
|
||||
func search(conn *sql.DB, opts searchopts) list.List {
|
||||
@ -188,7 +189,18 @@ func search(conn *sql.DB, opts searchopts) list.List {
|
||||
args = append(args, opts.retval)
|
||||
}
|
||||
sb.WriteString("ORDER BY timestamp ")
|
||||
sb.WriteString("ASC ")
|
||||
if opts.order != nil {
|
||||
sb.WriteString(*opts.order)
|
||||
sb.WriteRune(' ')
|
||||
} else {
|
||||
sb.WriteString("ASC ")
|
||||
}
|
||||
|
||||
if opts.limit != nil {
|
||||
sb.WriteString("LIMIT ")
|
||||
sb.WriteString(strconv.Itoa(*opts.limit))
|
||||
sb.WriteRune(' ')
|
||||
}
|
||||
|
||||
queryStmt := sb.String()
|
||||
|
||||
@ -309,6 +321,7 @@ func main() {
|
||||
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
|
||||
alias hs='hs9001 search'
|
||||
`)
|
||||
case "bash-disable":
|
||||
fmt.Printf("unset PROMPT_COMMAND\n")
|
||||
@ -352,7 +365,8 @@ func main() {
|
||||
q := strings.Join(args, " ")
|
||||
|
||||
opts := searchopts{}
|
||||
opts.order = "ASC"
|
||||
o := "ASC"
|
||||
opts.order = &o
|
||||
if q != "" {
|
||||
cmd := "%" + q + "%"
|
||||
opts.command = &cmd
|
||||
|
新增問題並參考
封鎖使用者