Last active
August 10, 2025 18:28
-
-
Save catb0t/68a9532740cb3b00c3799eb36ac74c54 to your computer and use it in GitHub Desktop.
Launch-editor - Launch your favourite editor in a saved directory, or type a new directory to be saved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires Python3 and `tput` from ncurses-bin | |
launch-editor () { | |
EDITOR="pulsar" | |
history_file="$HOME/.launch-editor_history" | |
have_history=0 | |
number=1 | |
tput setaf 4 | |
tput bold | |
echo -en "\n- Launch $EDITOR " | |
tput smul | |
echo -e "where?\n" | |
tput sgr0 | |
# file exists regardless of type | |
if [ -e "$history_file" ] ; then | |
have_history=1 | |
removing=0 | |
while IFS= read -r line; do | |
tput smul | |
tput setaf 2 | |
tput bold | |
if [ -d "$line" ] ; then | |
echo -n "$number" | |
tput rmul | |
tput setaf 6 | |
echo -e "\t$line" | |
else | |
echo -e "\e[9m\e[1;30m$number\e[0m\t\e[9m\e[1;30m$line\e[0m (will be removed)" | |
to_remove[removing]="$line" | |
(( removing+=1 )) | |
fi | |
tput sgr0 | |
dir_array[number]="$line" | |
(( number+=1 )) | |
done < "$history_file" | |
else | |
true | |
fi | |
if [ $have_history -eq 1 ] ; then | |
prompt="Type or select \e[1;32m\e[4mdirectory #\e[0m\e[1;35m: " | |
else | |
prompt="Type directory: " | |
fi | |
good_dir=0 | |
while [ $good_dir -ne 1 ] ; do | |
tput bold | |
tput setaf 5 | |
echo -en "\n$prompt" | |
tput setaf 3 | |
read -re ip | |
tput sgr0 | |
selected_dir="" | |
case "$ip" in | |
'') continue ;; | |
*[!0-9]*) selected_dir="$ip" ;; | |
*) selected_dir=${dir_array[$ip]} ;; | |
esac | |
selected_dir="${selected_dir/#\~/$HOME}" | |
selected_dir=$(realpath "$selected_dir") | |
if [ -d "$selected_dir" ]; then | |
# echo "$selected_dir" | |
good_dir=1 | |
else | |
echo -e "Error: not a directory: $ip\n" | |
fi | |
done | |
awk -v line="$selected_dir" 'FNR==NR && line==$0{f=1; exit} | |
END{if (!f) print line >> FILENAME}' "$history_file" | |
if (( ${#to_remove[@]} )) | |
then | |
join "$(echo -e "\x02")" "${to_remove[@]}" | | |
python3 -c "from pathlib import Path; from codecs import escape_decode | |
hist = Path('$history_file').read_text().rstrip().split('\n') | |
# print(repr(hist)) | |
i_remove = input() | |
remove = escape_decode(bytes(i_remove, 'utf-8'))[0].decode('utf-8').split('\x02') | |
# print(repr(remove)) | |
if not remove: | |
print('\n'.join(hist)) | |
for hist_line in hist: | |
# print('hist line is', hist_line) | |
if hist_line not in remove: | |
print(hist_line)" > "$history_file.fix" | |
command rm "$history_file" | |
command mv "$history_file.fix" "$history_file" | |
fi | |
tput bold | |
tput setaf 4 | |
echo -ne "\nFinal: " | |
tput setaf 3 | |
echo "$selected_dir" | |
tput sgr0 | |
cd "$selected_dir" && "$EDITOR" . | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment