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:
parent
b4a7b6a1b1
commit
cf6ff3349d
6 changed files with 47 additions and 19 deletions
21
README.md
21
README.md
|
|
@ -80,25 +80,24 @@ Note the optional setup of `embark`, which allows us to visit files just by invo
|
||||||
|
|
||||||
The "command line interface" is just a small set of convenient scripts: since these are just text files, we could just as easily create everything manually.
|
The "command line interface" is just a small set of convenient scripts: since these are just text files, we could just as easily create everything manually.
|
||||||
|
|
||||||
1. To get started, create an empty directory to serve as content origin. It doesn't matter where or what it's called, since the shell scripts only assume a sequentially numbered collection of files.
|
To get started, create an empty directory to serve as content origin. It doesn't matter where or what it's called, since the shell scripts only assume a sequentially numbered collection of files.
|
||||||
|
|
||||||
2. Decide on a zero-width for your series and seed the first file:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
touch 01.md
|
|
||||||
```
|
|
||||||
|
|
||||||
The rest of the commands will now work with this width.
|
|
||||||
|
|
||||||
## next
|
## next
|
||||||
|
|
||||||
Create a new file, open it with emacsclient, and run `um-journal-header`:
|
To create a new file:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
um next
|
um next
|
||||||
```
|
```
|
||||||
|
|
||||||
Yields:
|
This will create the file, open it with emacsclient, and run `um-journal-header` in that file:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
# 01.md
|
||||||
|
: 2024.01.14
|
||||||
|
```
|
||||||
|
|
||||||
|
If you save this file and run `um next` again you get:
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
# 02.md
|
# 02.md
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
# Get the last file name of any numbered series of text files.
|
# Get the last file name of any numbered series of text files.
|
||||||
|
|
||||||
# file format:
|
# file format:
|
||||||
|
|
@ -6,6 +8,14 @@
|
||||||
# in plain terms:
|
# in plain terms:
|
||||||
# [leading numbers with consistent width].[optional string descriptor and . separator][md|txt]
|
# [leading numbers with consistent width].[optional string descriptor and . separator][md|txt]
|
||||||
|
|
||||||
# TODO: signal no file found, so that next knows to create one.
|
UMALLFILES=`ls | egrep '^[[:digit:]].*\.(md|txt)'`
|
||||||
# Grep exits 1, but tail exits 0.
|
LASTSTATUS=$?
|
||||||
ls | egrep '^[[:digit:]].*\.md|txt' | tail -n 1
|
|
||||||
|
# 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
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,11 @@
|
||||||
#
|
#
|
||||||
# See https://git.bth.moi/bth/um#next
|
# See https://git.bth.moi/bth/um#next
|
||||||
|
|
||||||
# NOTE the optional $1 arg passed to awk with -v:
|
# NOTE: handle the exit 1 case of last.sh
|
||||||
NEXTFILE=`$UMBASEPATH/last.sh | awk -f $UMBASEPATH/next.awk -v arg=$1`
|
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
|
# tag is optional second arg to this script
|
||||||
TAGINSERT='(previous-line) (insert "+ %s\\n") (next-line)'
|
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\"))"
|
UMELISP="(progn (find-file \"$NEXTFILE\") (um-journal-header) $UMTAG (message \"creating $NEXTFILE\"))"
|
||||||
|
|
||||||
# for testing and piping
|
# for testing and piping
|
||||||
if [ $UMNEXTECHO = true ]; then
|
if [ $UMNEXTECHO ]; then
|
||||||
echo $UMELISP
|
echo $UMELISP
|
||||||
else
|
else
|
||||||
# HACK: emacs won't accept piped input as a file name, so we use --eval to open
|
# HACK: emacs won't accept piped input as a file name, so we use --eval to open
|
||||||
# the file via find-file.
|
# the file via find-file.
|
||||||
emacsclient --eval "$UMELISP"
|
emacsclient --eval "$UMELISP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|
|
||||||
0
shell/test/mockjournal/foo/bar.cc
Normal file
0
shell/test/mockjournal/foo/bar.cc
Normal file
|
|
@ -10,6 +10,7 @@ setup() {
|
||||||
# FIXME: why isn't this working?
|
# FIXME: why isn't this working?
|
||||||
# PATH="$DIR/../:$PATH"
|
# PATH="$DIR/../:$PATH"
|
||||||
# commands depend on being in the journal/ path.
|
# commands depend on being in the journal/ path.
|
||||||
|
export ORIGPWD=`pwd`
|
||||||
cd $DIR/mockjournal
|
cd $DIR/mockjournal
|
||||||
# so that we can run next.sh without dealing with emacsclient.
|
# so that we can run next.sh without dealing with emacsclient.
|
||||||
export UMNEXTECHO=true
|
export UMNEXTECHO=true
|
||||||
|
|
@ -17,7 +18,8 @@ setup() {
|
||||||
|
|
||||||
teardown() {
|
teardown() {
|
||||||
export UMNEXTECHO=false
|
export UMNEXTECHO=false
|
||||||
cd -
|
cd $ORIGPWD
|
||||||
|
git restore $DIR/mockjournal
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "um usage" {
|
@test "um usage" {
|
||||||
|
|
@ -67,6 +69,13 @@ um_next_awk() {
|
||||||
assert_output '099.poo.md'
|
assert_output '099.poo.md'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "um last empty" {
|
||||||
|
cd foo
|
||||||
|
run um last
|
||||||
|
assert_failure
|
||||||
|
assert_output 'no files found'
|
||||||
|
}
|
||||||
|
|
||||||
@test "um cat" {
|
@test "um cat" {
|
||||||
run um cat 001.md 002.blah.md
|
run um cat 001.md 002.blah.md
|
||||||
assert_output '# 001
|
assert_output '# 001
|
||||||
|
|
|
||||||
7
shell/um
7
shell/um
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
# export so that we can use it in subscripts.
|
# export so that we can use it in subscripts.
|
||||||
export UMBASEPATH=`realpath $0 | xargs dirname`
|
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
|
# catch first arguments with $1
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
|
@ -22,6 +27,6 @@ case "$1" in
|
||||||
$UMBASEPATH/tag.sh $2
|
$UMBASEPATH/tag.sh $2
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "usage: um (next|last|cat|rename|tag)"
|
echo "usage: um (next|last|cat|rename|tag)"; exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue