From 8671b7b71a4b44db9144eaeb603ec94212cb94b3 Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Sat, 12 Nov 2022 14:12:36 +0800 Subject: Refactor --- Makefile | 6 +----- wd | 63 +++++++++++++++++++++++++++++++++++++++++++++----------------- wd-edit | 3 --- wd-journal | 52 --------------------------------------------------- 4 files changed, 47 insertions(+), 77 deletions(-) delete mode 100755 wd-edit delete mode 100755 wd-journal diff --git a/Makefile b/Makefile index 1a866ee..91420d5 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,6 @@ all: install: @install -vm0755 \ "$(PROG)" \ - "$(PROG)-journal" \ - "$(PROG)-edit" \ "$(DESTDIR)$(PREFIX)/bin" @echo @echo "$(PROG) is installed succesfully" @@ -20,8 +18,6 @@ install: uninstall: @rm -vrf \ - "$(DESTDIR)$(PREFIX)/bin/$(PROG)" \ - "$(DESTDIR)$(PREFIX)/bin/$(PROG)-journal" \ - "$(DESTDIR)$(PREFIX)/bin/$(PROG)-edit" + "$(DESTDIR)$(PREFIX)/bin/$(PROG)" .PHONY: all install uninstall diff --git a/wd b/wd index d6b7c6b..7e62181 100755 --- a/wd +++ b/wd @@ -3,7 +3,15 @@ VERSION="0.0.2" PROGRAM="$(basename $0)" -cmd_wd_usage () { +[ -f "${WDRC:-"$HOME/.wdrc"}" ] && source "${WDRC:-"$HOME/.wdrc"}" + +WD_NOTES_DIR=${WD_NOTES_DIR:-"$HOME/notes"} +WD_JOURNALS_DIR=${WD_JOURNALS_DIR:-"$WD_NOTES_DIR/journals"} + +year=$(date +%Y) +week_number=$(date +%W) + +cmd_usage () { cat <<-_EOF Usage: @@ -14,32 +22,53 @@ _EOF exit 0 } -cmd_wd_version () { +cmd_version () { echo $VERSION exit 0 } -setup_env () { +cmd_edit () { + cd "$WD_NOTES_DIR" && $EDITOR "$WD_NOTES_DIR/index.md" - local wdrc=${WDRC:-"$HOME/.wdrc"} - if [ ! -f "$wdrc" ]; then - touch $wdrc - fi - source $wdrc + exit 0 +} - export WD_NOTES_DIR=${WD_NOTES_DIR:-"$HOME/notes"} - export WD_JOURNALS_DIR=${WD_JOURNALS_DIR:-"$WD_NOTES_DIR/journals"} +cmd_journal () { + local weekly_journal_title="# Week $week_number $year" + local weekly_journal_subtitle="## $(date '+%Y-%m-%d %A')" -} + local monthly_journal_title="# $(date '+%B %Y')" + local monthly_journal_subtitle="## [Week $week_number $year](week$week_number.md)" + + local weekly_journal="$WD_JOURNALS_DIR/$year/week$week_number.md" + local monthly_journal="$WD_JOURNALS_DIR/$year/$(date '+%Y-%m').md" -setup_env + local file_to_edit="$weekly_journal" + local file_title="$weekly_journal_title" + local file_subtitle="$weekly_journal_subtitle" + + case "$1" in + --monthly|-m) shift; + file_to_edit="$monthly_journal" + file_title="$monthly_journal_title" + file_subtitle="$monthly_journal_subtitle" + ;; + esac + + [ ! -f "$file_to_edit" ] && echo "$file_title" > "$file_to_edit" + grep -qF "$file_subtitle" "$file_to_edit" || echo "$file_subtitle" >> "$file_to_edit" + + cd "$WD_NOTES_DIR" && $EDITOR "$file_to_edit" + + exit 0 +} case "$1" in - help|--help|-h) shift; cmd_wd_usage "$@";; - version|--version|-v) shift; cmd_wd_version "$@";; - journal|j) shift; "$PROGRAM-journal" "$@";; - edit|e) shift; "$PROGRAM-edit" "$@";; - *) cmd_wd_usage "$@";; + help|--help|-h) shift; cmd_usage "$@";; + version|--version|-v) shift; cmd_version "$@";; + journal|j) shift; cmd_journal "$@";; + edit|e) shift; cmd_edit "$@";; + *) cmd_usage "$@";; esac exit 0 diff --git a/wd-edit b/wd-edit deleted file mode 100755 index 90faab5..0000000 --- a/wd-edit +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -$EDITOR "$WD_NOTES_DIR/index.md" diff --git a/wd-journal b/wd-journal deleted file mode 100755 index 28edd46..0000000 --- a/wd-journal +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env bash - -year=$(date +%Y) -week_number=$(date +%W) - -weekly_journal_title="# Week $week_number $year" -weekly_journal_subtitle="## $(date '+%Y-%m-%d %A')" - -monthly_journal_title="# $(date '+%B %Y')" -monthly_journal_subtitle="## [Week $week_number $year](week$week_number.md)" - -weekly_journal="$WD_JOURNALS_DIR/$year/week$week_number.md" -monthly_journal="$WD_JOURNALS_DIR/$year/$(date '+%Y-%m').md" - -# Open weekly journal by default. -# TODO: maybe I can add a environment variable on it. -file_to_edit="$weekly_journal" - -init_journal_with_title () { - local filepath=$1 - local journal_title=$2 - - [ ! -f "$filepath" ] && echo "$journal_title" > "$filepath" -} - -append_line_if_not_in_file () { - local filepath=$1 - local line=$2 - - if ! grep -qF "$line" "$filepath"; then - echo "$line" >> "$filepath" - fi -} - -init_weekly_journal () { - init_journal_with_title "$weekly_journal" "$weekly_journal_title" - - append_line_if_not_in_file "$weekly_journal" "$weekly_journal_subtitle" -} - -init_monthly_journal () { - init_journal_with_title "$monthly_journal" "$monthly_journal_title" - - append_line_if_not_in_file "$monthly_journal" "$monthly_journal_subtitle" -} - -case "$1" in - --monthly|-m) shift; init_monthly_journal; file_to_edit="$monthly_journal";; - --weekly|-w) shift; init_weekly_journal; file_to_edit="$weekly_journal";; -esac - -$EDITOR "$file_to_edit" -- cgit v1.2.3