last.sh will exit 1 when no file found; handle it in next.sh. meaning um next works in empty dirs

This commit is contained in:
bth 2024-01-22 10:21:53 -08:00
commit cf6ff3349d
6 changed files with 47 additions and 19 deletions

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Get the last file name of any numbered series of text files.
# file format:
@ -6,6 +8,14 @@
# in plain terms:
# [leading numbers with consistent width].[optional string descriptor and . separator][md|txt]
# TODO: signal no file found, so that next knows to create one.
# Grep exits 1, but tail exits 0.
ls | egrep '^[[:digit:]].*\.md|txt' | tail -n 1
UMALLFILES=`ls | egrep '^[[:digit:]].*\.(md|txt)'`
LASTSTATUS=$?
# NOTE: if we don't stop and signal here, tail will exit 0
if [ "$LASTSTATUS" -ne 0 ]; then
echo "no files found" >&2
exit 1
fi
# NOTE: if var isn't quoted echo will change newlines to spaces
echo "$UMALLFILES" | tail -n 1

View file

@ -23,8 +23,11 @@
#
# See https://git.bth.moi/bth/um#next
# NOTE the optional $1 arg passed to awk with -v:
NEXTFILE=`$UMBASEPATH/last.sh | awk -f $UMBASEPATH/next.awk -v arg=$1`
# NOTE: handle the exit 1 case of last.sh
LASTFILE=`$UMBASEPATH/last.sh || echo $UMDEFAULTINIT`
# NOTE: the optional $1 arg passed to awk with -v:
NEXTFILE=`echo $LASTFILE | awk -f $UMBASEPATH/next.awk -v arg=$1`
# tag is optional second arg to this script
TAGINSERT='(previous-line) (insert "+ %s\\n") (next-line)'
@ -40,10 +43,12 @@ fi
UMELISP="(progn (find-file \"$NEXTFILE\") (um-journal-header) $UMTAG (message \"creating $NEXTFILE\"))"
# for testing and piping
if [ $UMNEXTECHO = true ]; then
if [ $UMNEXTECHO ]; then
echo $UMELISP
else
# HACK: emacs won't accept piped input as a file name, so we use --eval to open
# the file via find-file.
emacsclient --eval "$UMELISP"
fi
exit 0

View file

View file

@ -10,6 +10,7 @@ setup() {
# FIXME: why isn't this working?
# PATH="$DIR/../:$PATH"
# commands depend on being in the journal/ path.
export ORIGPWD=`pwd`
cd $DIR/mockjournal
# so that we can run next.sh without dealing with emacsclient.
export UMNEXTECHO=true
@ -17,7 +18,8 @@ setup() {
teardown() {
export UMNEXTECHO=false
cd -
cd $ORIGPWD
git restore $DIR/mockjournal
}
@test "um usage" {
@ -67,6 +69,13 @@ um_next_awk() {
assert_output '099.poo.md'
}
@test "um last empty" {
cd foo
run um last
assert_failure
assert_output 'no files found'
}
@test "um cat" {
run um cat 001.md 002.blah.md
assert_output '# 001

View file

@ -2,6 +2,11 @@
# export so that we can use it in subscripts.
export UMBASEPATH=`realpath $0 | xargs dirname`
# TODO: load a config file?
export UMDEFAULTINIT='000.md'
# TODO: decide if getopts would be better. positional args supposed to be bad.
# https://stackoverflow.com/a/28466267
# catch first arguments with $1
case "$1" in
@ -22,6 +27,6 @@ case "$1" in
$UMBASEPATH/tag.sh $2
;;
*)
echo "usage: um (next|last|cat|rename|tag)"
echo "usage: um (next|last|cat|rename|tag)"; exit 1
;;
esac