annotate .shell.d/99.mcfly.zsh @ 526:852565046ed0 default tip

zsh: fidget with screen/tmux message This should speed things up very slightly by avoiding some `grep` action in the common case of no detached screens/tmuxes.
author Augie Fackler <raf@durin42.com>
date Mon, 14 Nov 2022 11:02:35 -0500
parents ea947b2b04a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
1 #!/bin/zsh
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
2
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
3 # Ensure stdin is a tty
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
4 [[ -t 0 ]] || return
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
5
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
6 # Make sure mcfly is installed
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
7 if ! which mcfly > /dev/null ; then
492
26caf7bf8805 mcfly: fix to correctly no-op when mcfly is absent
Augie Fackler <raf@durin42.com>
parents: 491
diff changeset
8 return 0
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
9 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
10
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
11 # Avoid loading this file more than once
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
12 if [[ "$__MCFLY_LOADED" == "loaded" ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
13 return 0
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
14 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
15 __MCFLY_LOADED="loaded"
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
16
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
17 emulate -L zsh
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
18
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
19 # Ensure HISTFILE exists.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
20 export HISTFILE="${HISTFILE:-$HOME/.zsh_history}"
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
21 if [[ ! -r "${HISTFILE}" ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
22 echo "McFly: ${HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly."
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
23 return 1
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
24 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
25
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
26 # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
27 export MCFLY_SESSION_ID=$(dd if=/dev/urandom bs=256 count=1 2> /dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
28
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
29 # Find the binary
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
30 MCFLY_PATH=${MCFLY_PATH:-$(which mcfly)}
512
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
31 if [[ -z "$MCFLY_PATH" || "$MCFLY_PATH" == "mcfly not found" ]]; then
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
32 echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.bash."
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
33 return 1
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
34 fi
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
35
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
36 # Required for commented out mcfly search commands to work.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
37 setopt interactive_comments # allow comments in interactive shells (like Bash does)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
38
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
39 # McFly's temporary, per-session history file.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
40 if [[ ! -f "${MCFLY_HISTORY}" ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
41 export MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
42 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
43
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
44 # Setup a function to be used by $PROMPT_COMMAND.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
45 function mcfly_prompt_command {
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
46 local exit_code=$? # Record exit status of previous command.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
47
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
48 # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
49 if [[ ! -f "${MCFLY_HISTORY}" ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
50 export MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
51 tail -n100 "${HISTFILE}" >| ${MCFLY_HISTORY}
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
52 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
53
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
54 # Write history to $MCFLY_HISTORY.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
55 fc -W "${MCFLY_HISTORY}"
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
56
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
57 # Run mcfly with the saved code. It fill find the text of the last command in $MCFLY_HISTORY and save it to the database.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
58 [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Run mcfly add --exit ${exit_code}"
512
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
59 $MCFLY_PATH --history_format zsh add --exit ${exit_code}
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
60 return ${exit_code} # Restore the original exit code by returning it.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
61 }
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
62 precmd_functions+=(mcfly_prompt_command)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
63
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
64 # Cleanup $MCFLY_HISTORY tmp files on exit.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
65 exit_logger() {
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
66 [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Exiting and removing $MCFLY_HISTORY"
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
67 rm -f $MCFLY_HISTORY
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
68 }
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
69 zshexit_functions+=(exit_logger)
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
70
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
71 # If this is an interactive shell, take ownership of ctrl-r.
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
72 if [[ $- =~ .*i.* ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
73 mcfly-history-widget() {
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
74 () {
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
75 echoti rmkx
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
76 exec </dev/tty
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
77 local mcfly_output=$(mktemp -t mcfly.output.XXXXXXXX)
512
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
78 $MCFLY_PATH --history_format zsh search -o "${mcfly_output}" "${LBUFFER}"
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
79 echoti smkx
512
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
80
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
81 # Interpret commandline/run requests from McFly
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
82 while read -r key val; do
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
83 if [[ "$key" = "mode" ]]; then local mode="$val"; fi
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
84 if [[ "$key" = "commandline" ]]; then local commandline="$val"; fi
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
85 done < "${mcfly_output}"
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
86 rm -f $mcfly_output
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
87
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
88 if [[ -n $commandline ]]; then
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
89 RBUFFER=""
512
ea947b2b04a0 zsh: update mcfly support to 1d1d5892b0c8
Augie Fackler <raf@durin42.com>
parents: 503
diff changeset
90 LBUFFER="${commandline}"
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
91 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
92 if [[ "${mode}" == "run" ]]; then
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
93 zle accept-line
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
94 fi
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
95 zle redisplay
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
96 }
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
97 }
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
98 zle -N mcfly-history-widget
503
55aa151f3567 zsh: move mcfly to F1 from ^T
Augie Fackler <raf@durin42.com>
parents: 492
diff changeset
99 bindkey '^[OP' mcfly-history-widget
485
e4d4cd0d120e zsh: start using mcfly for ^R history search
Augie Fackler <raf@durin42.com>
parents:
diff changeset
100 fi