t script
This commit is contained in:
parent
7cc90747ca
commit
66681dd143
@ -2,7 +2,7 @@ readonly TARGETS="\
|
|||||||
colors:.config/terminal-colors.d
|
colors:.config/terminal-colors.d
|
||||||
less:.lesskey .infokey
|
less:.lesskey .infokey
|
||||||
tmux:.config/tmux .tmux .config/systemd/user/tmux.service .local/bin/tmux_start_session.sh .local/bin/tmux_list_sessions.sh .local/bin/tmux_attach_session.sh .local/bin/tmux_kill_sessions.sh
|
tmux:.config/tmux .tmux .config/systemd/user/tmux.service .local/bin/tmux_start_session.sh .local/bin/tmux_list_sessions.sh .local/bin/tmux_attach_session.sh .local/bin/tmux_kill_sessions.sh
|
||||||
t:.local/bin/t
|
t:
|
||||||
zsh:.config/zsh .zshenv .inputrc %colors
|
zsh:.config/zsh .zshenv .inputrc %colors
|
||||||
alacritty:.config/alacritty
|
alacritty:.config/alacritty
|
||||||
nvim:.config/nvim .editorconfig .editrc .local/bin/vim_askpass_helper
|
nvim:.config/nvim .editorconfig .editrc .local/bin/vim_askpass_helper
|
||||||
|
|||||||
@ -1,258 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
|
|
||||||
set -o errexit
|
|
||||||
set -o nounset
|
|
||||||
|
|
||||||
|
|
||||||
readonly ENV_FILE=".t.env"
|
|
||||||
readonly EXIT_SUCCESS=0
|
|
||||||
readonly NOTES_DIR_BASE="${HOME}/.t"
|
|
||||||
readonly DEFAULT_NAMESPACE="def"
|
|
||||||
readonly PATH_SEPARATOR_REPLACE="%2F"
|
|
||||||
|
|
||||||
|
|
||||||
find_file_up_fs() (
|
|
||||||
file_to_find="${1}"
|
|
||||||
|
|
||||||
while [ ! "$(pwd)" = "/" ]
|
|
||||||
do
|
|
||||||
if [ -f "${file_to_find}" ]; then
|
|
||||||
realpath "${file_to_find}"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
cd ..
|
|
||||||
done
|
|
||||||
)
|
|
||||||
|
|
||||||
load_env_if_found() {
|
|
||||||
env_file="$(find_file_up_fs "./${ENV_FILE}")"
|
|
||||||
|
|
||||||
if [ -n "${env_file}" ]; then
|
|
||||||
. "${env_file}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
if [ -z "${t+x}" ]; then
|
|
||||||
load_env_if_found
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
readonly t="${t:-${DEFAULT_NAMESPACE}}"
|
|
||||||
readonly NOTES_DIR="${NOTES_DIR_BASE}/${t}"
|
|
||||||
|
|
||||||
TAB="$(printf -- '\t')"
|
|
||||||
readonly TAB
|
|
||||||
|
|
||||||
|
|
||||||
cmd_help() {
|
|
||||||
echo "USAGE
|
|
||||||
T script for fast notes
|
|
||||||
|
|
||||||
t - Show notes in format '[INDEX] NOTE NAME (LINES)'
|
|
||||||
t get (NOTE) - Get note content
|
|
||||||
t show - Show notes in format '[INDEX] NOTE NAME (LINES)'
|
|
||||||
t (INDEX) - Show note content
|
|
||||||
t add (X X X) - Add note with name X X X
|
|
||||||
t edit (INDEX) - Edit note with INDEX by \$EDITOR
|
|
||||||
t done (INDEX) [INDEX] ... - Delete notes with INDEXes
|
|
||||||
t namespaces - Show namespaces
|
|
||||||
t --help - Show this message
|
|
||||||
|
|
||||||
t a - alias for add
|
|
||||||
t e - alias for edit
|
|
||||||
t d - alias for done
|
|
||||||
t delete - alias for done
|
|
||||||
t ns - alias for namespaces
|
|
||||||
|
|
||||||
|
|
||||||
NAMESPACES
|
|
||||||
t namespaces # show namespaces
|
|
||||||
t=work t a fix bug 211 # add note in workspace 'work'
|
|
||||||
t=work t # show notes in workspace 'work'"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
die() {
|
|
||||||
echo "$(basename "${0}"): Error: ${1}" 1>&2
|
|
||||||
exit "${2:-${EXIT_SUCCESS}}"
|
|
||||||
}
|
|
||||||
|
|
||||||
get_notes_sorted_by_access_time() {
|
|
||||||
find "${NOTES_DIR}" -maxdepth 1 -type f -printf '%C@ %p\n' \
|
|
||||||
| sort -k1 -r \
|
|
||||||
| cut -d" " -f2- \
|
|
||||||
| tr '\n' "${IFS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
unescape_path_separators() {
|
|
||||||
sed "s/${PATH_SEPARATOR_REPLACE}/\//g"
|
|
||||||
}
|
|
||||||
|
|
||||||
escape_path_separators() {
|
|
||||||
sed "s/\//${PATH_SEPARATOR_REPLACE}/g"
|
|
||||||
}
|
|
||||||
|
|
||||||
get_notes_with_indexes() {
|
|
||||||
SAVEIFS="${IFS}"
|
|
||||||
IFS=';'
|
|
||||||
|
|
||||||
index=1
|
|
||||||
for note in $(IFS="${IFS}" get_notes_sorted_by_access_time)
|
|
||||||
do
|
|
||||||
if [ -z "${note}" ]; then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ${_TO_SHOW:-false}; then
|
|
||||||
note_lines="$(wc -l < "${note}")"
|
|
||||||
|
|
||||||
if [ "${note_lines}" -gt 0 ]; then
|
|
||||||
note_lines="$((note_lines+1))"
|
|
||||||
fi
|
|
||||||
if [ "${note_lines}" -gt 70 ]; then
|
|
||||||
note_lines="..."
|
|
||||||
elif [ "${note_lines}" -eq 0 ]; then
|
|
||||||
note_lines="-"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
note_name="$(basename -- "${note}")"
|
|
||||||
|
|
||||||
if ${_TO_SHOW:-false}; then
|
|
||||||
if [ "${#note_name}" -gt "$(($(tput cols)-12))" ]; then
|
|
||||||
note_name="$(echo "${note_name}" | colrm $(($(tput cols)-12)))..."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ${_TO_SHOW:-false}; then
|
|
||||||
note_name="$(echo "${note_name}" | unescape_path_separators)"
|
|
||||||
printf -- '[%d]\t%s\t(%s)\n' "${index}" "${note_name}" "${note_lines}"
|
|
||||||
else
|
|
||||||
printf -- '[%d]\t%s\n' "${index}" "${note_name}"
|
|
||||||
fi
|
|
||||||
index="$((index+1))"
|
|
||||||
done
|
|
||||||
|
|
||||||
IFS="${SAVEIFS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
_remove_first_and_last_element() {
|
|
||||||
cut -d"${TAB}" -f2
|
|
||||||
}
|
|
||||||
|
|
||||||
find_note_name_by_index() {
|
|
||||||
note_index="${1}"
|
|
||||||
grep "^\[${note_index}\]" | _remove_first_and_last_element
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_add_note() {
|
|
||||||
note_name="$(echo "$*" | escape_path_separators)"
|
|
||||||
note="${NOTES_DIR}/${note_name}"
|
|
||||||
touch "${note}"
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_delete_note() {
|
|
||||||
current_notes="$(get_notes_with_indexes)"
|
|
||||||
|
|
||||||
for note_index in "$@"
|
|
||||||
do
|
|
||||||
note="$(echo "${current_notes}" | find_note_name_by_index "${note_index}")"
|
|
||||||
note_to_remove="${NOTES_DIR}/${note}"
|
|
||||||
|
|
||||||
if [ ! -f "${note_to_remove}" ]; then
|
|
||||||
die "Note with index ${note_index} not found" 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm "${note_to_remove}"
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_edit_note() {
|
|
||||||
note_index="${1}"
|
|
||||||
note="$(get_notes_with_indexes | find_note_name_by_index "${note_index}")"
|
|
||||||
note_to_edit="${NOTES_DIR}/${note}"
|
|
||||||
|
|
||||||
if [ ! -f "${note_to_edit}" ]; then
|
|
||||||
die "Note with index ${note_index} not found" 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
${EDITOR} "${note_to_edit}"
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_cat_note() {
|
|
||||||
note_index="${1}"
|
|
||||||
note="$(get_notes_with_indexes | find_note_name_by_index "${note_index}")"
|
|
||||||
note_to_cat="${NOTES_DIR}/${note}"
|
|
||||||
|
|
||||||
if [ ! -f "${note_to_cat}" ]; then
|
|
||||||
die "Note with index ${note_index} not found" 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
note_name="$(basename "${note_to_cat}" | unescape_path_separators)"
|
|
||||||
printf '\033[1;34m# %s\033[0m\n\n' "${note_name}"
|
|
||||||
cat "${note_to_cat}"
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_get_note() {
|
|
||||||
note_to_cat="${NOTES_DIR}/${1}"
|
|
||||||
|
|
||||||
if [ ! -f "${note_to_cat}" ]; then
|
|
||||||
die "Note '${1}' not found" 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat "${note_to_cat}"
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
cmd_show_namespaces() {
|
|
||||||
(
|
|
||||||
for namespace in $(find "${NOTES_DIR_BASE}/" -mindepth 1 -maxdepth 1 -type d | sort -n)
|
|
||||||
do
|
|
||||||
namespace_notes_count="$(find "${namespace}" -type f | wc -l)"
|
|
||||||
printf -- '%s\t(%s)\n' "$(basename "${namespace}")" "${namespace_notes_count}"
|
|
||||||
done
|
|
||||||
) | column -t -R -1 -s "${TAB}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_show_notes() {
|
|
||||||
printf -- '\033[1;34m# %s\033[0m\n' "${t}"
|
|
||||||
_TO_SHOW=true get_notes_with_indexes | column -t -R 1,-1 -s "${TAB}"
|
|
||||||
}
|
|
||||||
|
|
||||||
remove_empty_namespaces() {
|
|
||||||
find "${NOTES_DIR_BASE}" -type d -empty -exec rm -r {} \; 2>/dev/null || true
|
|
||||||
}
|
|
||||||
trap remove_empty_namespaces EXIT
|
|
||||||
|
|
||||||
|
|
||||||
if [ ! -d "${NOTES_DIR}" ]; then
|
|
||||||
mkdir -p "${NOTES_DIR}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
if [ -z "${1+x}" ]; then
|
|
||||||
cmd_show_notes
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
case "${1}" in
|
|
||||||
show) shift; cmd_show_notes ;;
|
|
||||||
ns|namespaces) shift; cmd_show_namespaces ;;
|
|
||||||
a|add) shift; cmd_add_note "$@" ;;
|
|
||||||
d|done|delete) shift; cmd_delete_note "$@" ;;
|
|
||||||
e|edit) shift; cmd_edit_note "$@" ;;
|
|
||||||
get) shift; cmd_get_note "$@" ;;
|
|
||||||
--help) shift; cmd_help ;;
|
|
||||||
|
|
||||||
*) cmd_cat_note "$@" ;;
|
|
||||||
esac
|
|
||||||
exit "${EXIT_SUCCESS}"
|
|
||||||
3
install-hooks/t/post-install
Executable file
3
install-hooks/t/post-install
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/zsh
|
||||||
|
|
||||||
|
strip =t
|
||||||
3
install-hooks/t/pre-install
Executable file
3
install-hooks/t/pre-install
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
GOPROXY=direct go install github.com/thek4n/t/cmd/t@latest
|
||||||
Loading…
x
Reference in New Issue
Block a user