Skip to content

Commit

Permalink
add experimental history workaround (disabled by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
matan-h committed Apr 15, 2024
1 parent c6aeaaa commit bbff37f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions startup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# this file should be in "/sdcard/.adb/startup.sh"
# shellcheck shell=ksh
ENABLE_EXPERIMENTAL_HISTORY=false # Set to true to enable the experimental history workaround

# basic:
# shellcheck disable=SC2164
Expand Down Expand Up @@ -69,4 +70,25 @@ if [ -x "$(command -v 'ip')" ]; then
ifconfig | awk '/inet /{print $2}' | grep -v 127.0.0.1
fi
}
if [ "$ENABLE_EXPERIMENTAL_HISTORY" = true ]; then
# experimental history workaround (its disabled by default in mksh for android. see android.stackexchange:152061 mirabilos answer)
HISTFILE="$HOME/.adb/.history"
# "print -s": print to the history file, which doesn't exist. This allows the shell history features.
if [ -f "$HISTFILE" ] ; then
while read -r p; do print -s "$p"; done < "$HISTFILE"
fi

add_to_history(){
# fc -ln -1: get the last command from history without numbers.
last_command=$(fc -ln -1 2> /dev/null)||return

last_line=$(tail -n 1 "$HISTFILE" 2>/dev/null || true)
if [ "$last_command" != "$last_line" ]; then
# Append the last command to the file
echo "$last_command" >> "$HISTFILE"
else
return
fi
}
PS1="$PS1\$(add_to_history)";
fi

0 comments on commit bbff37f

Please sign in to comment.