131 lines
3.6 KiB
Bash
Executable File
131 lines
3.6 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
# Copyright (c) 2024 Raphaël Jakse
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
# copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
set -e
|
|
|
|
CONFIG_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/Ljournalrc"
|
|
|
|
if [ ! -f "${CONFIG_FILE}" ]; then
|
|
mkdir -p "$(dirname -- "$CONFIG_FILE")"
|
|
if [ -n "$XDG_DOCUMENTS_DIR" ]; then
|
|
printf 'JOURNAL_FILE="${XDG_DOCUMENTS_DIR:-$HOME}/journal.txt"\n' >> "${CONFIG_FILE}"
|
|
elif [ -d "$HOME/Documents" ]; then
|
|
printf 'JOURNAL_FILE="$HOME/Documents/journal.txt"\n' >> "${CONFIG_FILE}"
|
|
else
|
|
printf 'JOURNAL_FILE="$HOME/journal.txt"\n' >> "${CONFIG_FILE}"
|
|
fi
|
|
printf 'LESS='"'"'-~ -e +G'"'"'\n' >> "${CONFIG_FILE}"
|
|
fi
|
|
|
|
L=$(basename $0)
|
|
|
|
function usage() {
|
|
cat <<HERE
|
|
Usage:
|
|
$L - show the content of the journal
|
|
$L message - add message to the journal, with a date
|
|
$L + message - add message to the previous entry in the journal
|
|
$L - - add stdin to the journal
|
|
$L + - - add stdin to the journal, without a date
|
|
$L e - edit the journal manually
|
|
$L h, $L -h - show this help
|
|
|
|
Config file is in ${CONFIG_FILE}
|
|
HERE
|
|
}
|
|
|
|
if [ "$1" = "h" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
|
|
usage
|
|
exit
|
|
fi
|
|
|
|
. "${CONFIG_FILE}"
|
|
|
|
|
|
if [ "$1" = "e" ]; then
|
|
if [ -z "$EDITOR" ]; then
|
|
for prog in 'nano' 'jed' 'jove' 'vim' 'nvim' 'emacs' 'vi'; do
|
|
if command -v $prog >/dev/null; then
|
|
EDITOR=$prog
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
if [ -z "$EDITOR" ]; then
|
|
>&2 printf 'No editor found. Please set $EDITOR.\n'
|
|
exit 1
|
|
fi
|
|
exec "$EDITOR" "${JOURNAL_FILE}"
|
|
fi
|
|
|
|
|
|
# Don't add a new date line
|
|
if [ "$1" = "+" ]; then
|
|
append="+"
|
|
shift
|
|
fi
|
|
|
|
if [ "$1" = "-" ]; then
|
|
stdin="-"
|
|
shift
|
|
fi
|
|
|
|
|
|
if [ -z "$stdin" ]; then
|
|
msg="$@"
|
|
else
|
|
msg=""
|
|
while read line; do
|
|
msg=$(printf "%s\n%s" "$msg" "$line")
|
|
done
|
|
fi
|
|
|
|
msg="$(printf %s "$msg" | sed 's|^|\t|g')"
|
|
|
|
|
|
if [ -z "$msg" ] && [ -z "$append" ]; then
|
|
if [ ! -f ${JOURNAL_FILE} ]; then
|
|
usage
|
|
exit 0
|
|
else
|
|
if [ -z "$PAGER" ]; then
|
|
for prog in 'less' 'most' 'pg' 'more' 'lv' 'pager'; do
|
|
if command -v $prog >/dev/null; then
|
|
PAGER=$prog
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
if [ -z "$PAGER" ]; then
|
|
>&2 printf 'No pager found. Please set $PAGER.\n'
|
|
exit 1
|
|
fi
|
|
exec "${PAGER}" -- "${JOURNAL_FILE}"
|
|
fi
|
|
fi
|
|
|
|
if [ -z "$append" ]; then
|
|
printf "\n%s:\n\n" "$(date -R)" >> ${JOURNAL_FILE}
|
|
fi
|
|
|
|
printf "%s\n" "$msg" >> ${JOURNAL_FILE}
|