dotfiles/fish/functions/entry.fish
Freyja Wildes e68829029f [fish] entry function correctly create file
- entry function correctly creates the file when no date is input
- correctly check for the definition of EDITOR variable and only try to
  edit the file if the variable is defined
- Remove dead code
2025-06-17 02:35:05 +02:00

99 lines
3.5 KiB
Fish
Executable file

#!/bin/fish
# A script to create journal entries
# Default behavior is to create an entry for the current date. See "help" for usage and options.
# Unsafe script
set entry_date now
set entry_author $WAREHOUSE_AUTHOR
set journal_dir content/journal
function print_help
echo "Usage: entry [OPTION…]"
echo ""
echo "JOURNAL_DIR must be defined and point where the journal entry will be written. This can also be passed as an argument using -j/--journal option."
echo ""
echo " -a, --author author Change the author name in the created entry. Can be specified with WAREHOUSE_AUTHOR environment variable"
echo " -e, --edit Edit the entry file created using user defined editor. The editor is specified using the EDITOR environment variable. Does nothing if said variable is not defined"
echo " -h, --help Display this help message and exit."
echo " -n, --new [DATE] Create a new entry for the specified DATE (optional). If no date is specified, use current date. This is default behavior. DATE is expected in a format valid for the 'date' command"
end
# Open the entry file in the default editor. Requires the EDITOR variable to be defined
function edit_file
if ! set -qU EDITOR
echo No EDITOR defined
return 3
end
$EDITOR $argv[1]
end
function entry
argparse "a/author=" "e/edit" "h/help" "j/journal=" "n/new=" -- $argv
if set -ql _flag_help
print_help
return 0
end
if test -z $JOURNAL_DIR && ! set -ql _flag_journal
echo "You need to define JOURNAL_DIR environment variable. See entry -h."
return 2
end
if set -ql _flag_journal
set journal_dir $_flag_journal
else
set journal_dir $JOURNAL_DIR
end
if set -ql _flag_new
set entry_date $_flag_new
end
# Build variable related to the date. Need several info: year, mounth (number and name), day number and full date in YYYY-MM-DD format
set entry_date $(date +%Y-%m-%d -d $entry_date)
set entry_date_values $(string split -n " " $(date +'%Y %B %m %e' -d $entry_date))
set entry_year $entry_date_values[1]
set entry_mounth_name $(string replace -r '^(\w)' '\u$0' $entry_date_values[2])
set entry_mounth_number $entry_date_values[3]
set entry_day $entry_date_values[4]
# Build variables to write output
set mounth_dir $entry_mounth_number-$entry_mounth_name
set full_date $entry_day $entry_mounth_name $entry_year
set filename $entry_date.md
set output_dir $(path normalize $journal_dir/content/journal/$entry_year/$mounth_dir)
set output_path $(path normalize $output_dir/$filename)
if test -f $output_path
if set -ql _flag_edit
edit_file $output_path
return 0
else
echo "Error, $output_path: file exists"
return 1
end
end
if ! test -d "$output_dir"
echo "Creating necessary directories $output_dir"
mkdir -p "$output_dir"
if test $status -gt 0
echo "Couldn't create directory $output_dir"
return 1
end
end
echo "New template : $entry_date"
echo "Title: Journal du $argv$entry_day $entry_mounth_name $entry_year" >> $output_path
echo "Date: $entry_date" >> $output_path
echo "Author: $entry_author" >> $output_path
echo "Category: $entry_year" >> $output_path
echo "Tags:" >> $output_path
echo "" >> $output_path
echo "## Les bonnes choses" >> $output_path
if set -ql _flag_edit
edit_file $output_path
end
end