66 lines
1.6 kB
1
use math
2
use os
3
use file
4
use str
5
6
set-env ATUIN_SESSION (atuin uuid)
7
unset-env ATUIN_HISTORY_ID
8
9
set edit:after-readline = [$@edit:after-readline {|line|
10
try {
11
if (not-eq (str:trim-space $line) '') {
12
set-env ATUIN_HISTORY_ID (atuin history start -- $line)
13
}
14
} catch e {
15
unset-env ATUIN_HISTORY_ID
16
}
17
}]
18
19
set edit:after-command = [$@edit:after-command {|m|
20
if (has-env ATUIN_HISTORY_ID) {
21
var exit-status = 0
22
if (not-eq $m[error] $nil) {
23
if (has-key $m[error][reason] exit-status) {
24
set exit-status = $m[error][reason][exit-status]
25
} else {
26
set exit-status = 127
27
}
28
}
29
var duration = (exact-num (math:round (* $m[duration] 1000000000)))
30
31
var history-end = {
32
tmp E:ATUIN_LOG = 'error'
33
atuin history end --exit $exit-status --duration $duration -- $E:ATUIN_HISTORY_ID >$os:dev-null 2>&1
34
unset-env ATUIN_HISTORY_ID
35
}
36
37
# TODO: Add option to not background for use with `$notify-bg-job-success`?
38
$history-end &
39
}
40
}]
41
42
fn search {|@argv|
43
var accept-prefix = '__atuin_accept__:'
44
45
var p = (file:pipe)
46
# TODO: Will need an elvish flag in Atuin binary
47
with [E:ATUIN_LOG = 'error'] [E:ATUIN_SHELL_BASH = t] [E:ATUIN_QUERY = $edit:current-command] {
48
atuin search $@argv -i >$os:dev-tty 2>$p; edit:redraw &full=$true
49
}
50
file:close $p[w]
51
var command = (str:trim-space (slurp < $p))
52
file:close $p[r]
53
54
if (not-eq $command '') {
55
if (str:has-prefix $command $accept-prefix) {
56
edit:replace-input (str:trim-prefix $command $accept-prefix)
57
edit:return-line
58
} else {
59
edit:replace-input $command
60
}
61
}
62
}
63
64
fn search-up {
65
search --shell-up-key-binding
66
}
67