pxh 0.9.6

pxh is a fast, cross-shell history mining tool with interactive fuzzy search, secret scanning, and bidirectional sync across machines. It indexes bash and zsh history in SQLite with rich metadata for powerful recall.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env bats

setup() {
  PROMPT_COMMAND=''        # in case the invoking shell has set this
  history -s fake command  # preexec requires there be some history
  set -o nounset           # in case the user has this set
  __bp_delay_install="true"
  source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
}

# Evaluates all the elements of PROMPT_COMMAND
eval_PROMPT_COMMAND() {
  local prompt_command
  for prompt_command in "${PROMPT_COMMAND[@]}"; do
    eval "$prompt_command"
  done
}

# Joins the elements of PROMPT_COMMAND with $'\n'
join_PROMPT_COMMAND() {
  local IFS=$'\n'
  echo "${PROMPT_COMMAND[*]}"
}

bp_install() {
  __bp_install_after_session_init
  eval_PROMPT_COMMAND
}

test_echo() {
  echo "test echo"
}

test_preexec_echo() {
  printf "%s\n" "$1"
}

# Helper functions necessary because Bats' run doesn't preserve $?
return_exit_code() {
  return $1
}

set_exit_code_and_run_precmd() {
  return_exit_code ${1:-0}
  __bp_precmd_invoke_cmd
}


@test "sourcing bash-preexec should exit with 1 if we're not using bash" {
  unset BASH_VERSION
  run source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
  [ $status -eq 1 ]
  [ -z "$output" ]
}

@test "sourcing bash-preexec should exit with 1 if we're using an older version of bash" {
  if type -p bash-3.0 &>/dev/null; then
    run bash-3.0 -c "source \"${BATS_TEST_DIRNAME}/../bash-preexec.sh\""
    [ "$status" -eq 1 ]
    [ -z "$output" ]
  else
    skip
  fi
}

@test "__bp_install should exit if it's already installed" {
  bp_install

  run '__bp_install'
  [ $status -eq 1 ]
  [ -z "$output" ]
}

@test "__bp_install should remove trap logic and itself from PROMPT_COMMAND" {
  __bp_install_after_session_init

  # Assert that before running, the command contains the install string, and
  # afterwards it does not
  [[ "$PROMPT_COMMAND" == *"$__bp_install_string"* ]] || return 1

  eval_PROMPT_COMMAND

  [[ "$PROMPT_COMMAND" != *"$__bp_install_string"* ]] || return 1
}

@test "__bp_install should preserve an existing DEBUG trap" {
  trap_invoked_count=0
  foo() { (( trap_invoked_count += 1 )); }

  # note setting this causes BATS to mis-report the failure line when this test fails
  trap foo DEBUG
  [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'foo'" ]

  bp_install
  trap_count_snapshot=$trap_invoked_count

  [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ]
  [[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1

  __bp_interactive_mode # triggers the DEBUG trap

  # ensure the trap count is still being incremented after the trap's been overwritten
  (( trap_count_snapshot < trap_invoked_count ))
}

@test "__bp_install should preserve an existing DEBUG trap containing quotes" {
  trap_invoked_count=0
  foo() { (( trap_invoked_count += 1 )); }

  # note setting this causes BATS to mis-report the failure line when this test fails
  trap "foo && echo 'hello' >/dev/null" debug
  [ "$(trap -p DEBUG | cut -d' ' -f3-7)" == "'foo && echo '\''hello'\'' >/dev/null'" ]

  bp_install
  trap_count_snapshot=$trap_invoked_count

  [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ]
  [[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1

  __bp_interactive_mode # triggers the DEBUG trap

  # ensure the trap count is still being incremented after the trap's been overwritten
  (( trap_count_snapshot < trap_invoked_count ))
}

@test "__bp_sanitize_string should remove semicolons and trim space" {

    __bp_sanitize_string output "   true1;  "$'\n'
    [ "$output" == "true1" ]

    __bp_sanitize_string output " ; true2;  "
    [ "$output" == "true2" ]

    __bp_sanitize_string output $'\n'" ; true3;  "
    [ "$output" == "true3" ]

}

@test "Appending to PROMPT_COMMAND should work after bp_install" {
    bp_install

    PROMPT_COMMAND="$PROMPT_COMMAND; true"
    eval_PROMPT_COMMAND
}

@test "Appending or prepending to PROMPT_COMMAND should work after bp_install_after_session_init" {
    __bp_install_after_session_init
    nl=$'\n'
    PROMPT_COMMAND="$PROMPT_COMMAND; true"
    PROMPT_COMMAND="$PROMPT_COMMAND $nl true"
    PROMPT_COMMAND="$PROMPT_COMMAND; true"
    PROMPT_COMMAND="true; $PROMPT_COMMAND"
    PROMPT_COMMAND="true; $PROMPT_COMMAND"
    PROMPT_COMMAND="true; $PROMPT_COMMAND"
    PROMPT_COMMAND="true $nl $PROMPT_COMMAND"
    eval_PROMPT_COMMAND
}

# Case where a user is appending or prepending to PROMPT_COMMAND.
# This can happen after 'source bash-preexec.sh' e.g.
# source bash-preexec.sh; PROMPT_COMMAND="$PROMPT_COMMAND; other_prompt_command_hook"
@test "Adding to PROMPT_COMMAND before and after initiating install" {
    PROMPT_COMMAND="echo before"
    PROMPT_COMMAND="$PROMPT_COMMAND; echo before2"
    __bp_install_after_session_init
    PROMPT_COMMAND="$PROMPT_COMMAND"$'\n echo after'
    PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;"

    eval_PROMPT_COMMAND

    expected_result=$'__bp_precmd_invoke_cmd\necho after2; echo before; echo before2\n echo after\n__bp_interactive_mode'
    [ "$(join_PROMPT_COMMAND)" == "$expected_result" ]
}

@test "Adding to PROMPT_COMMAND after with semicolon" {
    PROMPT_COMMAND="echo before"
    __bp_install_after_session_init
    PROMPT_COMMAND="$PROMPT_COMMAND; echo after"

    eval_PROMPT_COMMAND

    expected_result=$'__bp_precmd_invoke_cmd\necho before\n echo after\n__bp_interactive_mode'
    [ "$(join_PROMPT_COMMAND)" == "$expected_result" ]
}

@test "during install PROMPT_COMMAND and precmd functions should be executed each once" {
    PROMPT_COMMAND="echo before"
    PROMPT_COMMAND="$PROMPT_COMMAND; echo before2"
    __bp_install_after_session_init
    PROMPT_COMMAND="$PROMPT_COMMAND; echo after"
    PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;"

    precmd() { echo "inside precmd"; }
    run eval_PROMPT_COMMAND
    [ "${lines[0]}" == "after2" ]
    [ "${lines[1]}" == "before" ]
    [ "${lines[2]}" == "before2" ]
    [ "${lines[3]}" == "inside precmd" ]
    [ "${lines[4]}" == "after" ]
    [ "${#lines[@]}" == '5' ]
}

@test "No functions defined for preexec should simply return" {
    __bp_interactive_mode

    run '__bp_preexec_invoke_exec' 'true'
    [ $status -eq 0 ]
    [ -z "$output" ]
}

@test "precmd should execute a function once" {
    precmd_functions+=(test_echo)
    run set_exit_code_and_run_precmd
    [ $status -eq 0 ]
    [ "$output" == "test echo" ]
}

@test "precmd should set \$? to be the previous exit code" {
    echo_exit_code() {
      echo "$?"
    }

    precmd_functions+=(echo_exit_code)
    run set_exit_code_and_run_precmd 251
    [ $status -eq 251 ]
    [ "$output" == "251" ]
}

@test "precmd should set \$BP_PIPESTATUS to the previous \$PIPESTATUS" {
  echo_pipestatus() {
    echo "${BP_PIPESTATUS[*]}"
  }
  # Helper function is necessary because Bats' run doesn't preserve $PIPESTATUS
  set_pipestatus_and_run_precmd() {
    false | true
    __bp_precmd_invoke_cmd
  }

  precmd_functions+=(echo_pipestatus)
  run 'set_pipestatus_and_run_precmd'
  [ $status -eq 0 ]
  [ "$output" == "1 0" ]
}

@test "precmd should set \$_ to be the previous last arg" {
    echo_last_arg() {
      echo "$_"
    }
    precmd_functions+=(echo_last_arg)

    bats_trap=$(trap -p DEBUG)
    trap DEBUG # remove the Bats stack-trace trap so $_ doesn't get overwritten
    : "last-arg"
    __bp_preexec_invoke_exec "$_"
    eval "$bats_trap" # Restore trap
    run set_exit_code_and_run_precmd
    [ $status -eq 0 ]
    [ "$output" == "last-arg" ]
}

@test "preexec should execute a function with the last command in our history" {
    preexec_functions+=(test_preexec_echo)
    __bp_interactive_mode
    git_command="git commit -a -m 'committing some stuff'"
    history -s $git_command

    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == "$git_command" ]
}

@test "preexec should execute multiple functions in the order added to their arrays" {
    fun_1() { echo "$1 one"; }
    fun_2() { echo "$1 two"; }
    preexec_functions+=(fun_1)
    preexec_functions+=(fun_2)
    __bp_interactive_mode

    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "${#lines[@]}" == '2' ]
    [ "${lines[0]}" == "fake command one" ]
    [ "${lines[1]}" == "fake command two" ]
}

@test "preecmd should execute multiple functions in the order added to their arrays" {
    fun_1() { echo "one"; }
    fun_2() { echo "two"; }
    precmd_functions+=(fun_1)
    precmd_functions+=(fun_2)

    run set_exit_code_and_run_precmd
    [ $status -eq 0 ]
    [ "${#lines[@]}" == '2' ]
    [ "${lines[0]}" == "one" ]
    [ "${lines[1]}" == "two" ]
}

@test "preexec should execute a function with IFS defined to local scope" {
    IFS=_
    name_with_underscores_1() { parts=(1_2); echo $parts; }
    preexec_functions+=(name_with_underscores_1)

    __bp_interactive_mode
    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == "1 2" ]
}

@test "precmd should execute a function with IFS defined to local scope" {
    IFS=_
    name_with_underscores_2() { parts=(2_2); echo $parts; }
    precmd_functions+=(name_with_underscores_2)
    run set_exit_code_and_run_precmd
    [ $status -eq 0 ]
    [ "$output" == "2 2" ]
}

@test "preexec should set \$? to be the exit code of preexec_functions" {
    return_nonzero() {
      return 1
    }
    preexec_functions+=(return_nonzero)

    __bp_interactive_mode

    run '__bp_preexec_invoke_exec'
    [ $status -eq 1 ]
}

@test "in_prompt_command should detect if a command is part of PROMPT_COMMAND" {

    PROMPT_COMMAND=$'precmd_invoke_cmd\n something; echo yo\n __bp_interactive_mode'
    run '__bp_in_prompt_command' "something"
    [ $status -eq 0 ]

    run '__bp_in_prompt_command' "something_else"
    [ $status -eq 1 ]

    # Should trim commands and arguments here.
    PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;"
    run '__bp_in_prompt_command' " precmd_invoke_cmd "
    [ $status -eq 0 ]

    PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;"
    run '__bp_in_prompt_command' " not_found"
    [ $status -eq 1 ]

}

@test "__bp_adjust_histcontrol should remove ignorespace and ignoreboth" {

    # Should remove ignorespace
    HISTCONTROL="ignorespace:ignoredups:*"
    __bp_adjust_histcontrol
    [ "$HISTCONTROL" == ":ignoredups:*" ]

    # Should remove ignoreboth and replace it with ignoredups
    HISTCONTROL="ignoreboth"
    __bp_adjust_histcontrol
    [ "$HISTCONTROL" == "ignoredups:" ]

    # Handle a few inputs
    HISTCONTROL="ignoreboth:ignorespace:some_thing_else"
    __bp_adjust_histcontrol
    echo "$HISTCONTROL"
    [ "$HISTCONTROL" == "ignoredups:::some_thing_else" ]

}

@test "preexec should respect HISTTIMEFORMAT" {
    preexec_functions+=(test_preexec_echo)
    __bp_interactive_mode
    git_command="git commit -a -m 'committing some stuff'"
    HISTTIMEFORMAT='%F %T '
    history -s $git_command

    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == "$git_command" ]
}

@test "preexec should not strip whitespace from commands" {
    preexec_functions+=(test_preexec_echo)
    __bp_interactive_mode
    history -s " this command has whitespace "

    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == " this command has whitespace " ]
}

@test "preexec should preserve multi-line strings in commands" {
    preexec_functions+=(test_preexec_echo)
    __bp_interactive_mode
    history -s "this 'command contains
a multiline string'"
    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == "this 'command contains
a multiline string'" ]
}

@test "preexec should work on options to 'echo' commands" {
    preexec_functions+=(test_preexec_echo)
    __bp_interactive_mode
    history -s -- '-n'
    run '__bp_preexec_invoke_exec'
    [ $status -eq 0 ]
    [ "$output" == '-n' ]
}