Skip to content

Commit

Permalink
v2 changes (PR #1)
Browse files Browse the repository at this point in the history
Breaking changes: YYYY/MM/DD.md as default <name> and list notes displays the full path relative to $JOT_DIR without the .md extension.
  • Loading branch information
Pinjasaur committed Mar 24, 2020
2 parents 330848d + b2a2fb7 commit f7c388d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,4 +1,4 @@
language: shell

script:
- bash -c 'shellcheck jot'
- shellcheck jot && bats test.bats
15 changes: 12 additions & 3 deletions README.md
Expand Up @@ -13,7 +13,7 @@ Add the [`jot` script](/jot) to your `$PATH` or create a function similar to:

```bash
jot() {
bash /path/to/jot "${@}"
/path/to/jot "${@}"
}
```

Expand All @@ -25,19 +25,28 @@ You'll also want to make sure `$EDITOR` is set to your preferred editor

## Usage

Run `jot <name>` to start writing. You can list notes with `jot --list` and show
the usage info with `jot --help`:

```
jot [<arguments>]
Options:
-h --help Display help.
-h --help Show this.
-l --list List notes.
<name> Create a note named <name>.md.
If you want it in a path, use slashes (e.g. foo/bar/baz).
If no <name>, the default is YYYY/MM/DD.md.
You can also use slashes e.g. foo/bar to create foo/bar.md.
```

## Related

- [jt][jt] - Creates timestamped notes (simplified version of jot)

## License

[MIT][license] &copy; Paul Esch-Laurent.

[jt]: https://github.com/Pinjasaur/jt
[license]: https://pinjasaur.mit-license.org/2017
[globstar]: https://www.linuxjournal.com/content/globstar-new-bash-globbing-option
18 changes: 11 additions & 7 deletions jot
Expand Up @@ -20,7 +20,8 @@ Options:
-h --help Show this.
-l --list List notes.
<name> Create a note named <name>.md.
If you want it in a path, use slashes (e.g. foo/bar/baz).
If no <name>, the default is YYYY/MM/DD.md.
You can also use slashes e.g. foo/bar to create foo/bar.md.
HEREDOC
}

Expand All @@ -29,17 +30,20 @@ list_notes() {
# Get all Markdown files in $JOT_DIR
shopt -s globstar nullglob
for note in "${JOT_DIR}"/**/*.md; do
echo -e "$(basename "${note}")"
# Strip the $JOT_DIR/ prefix
note_path="${note#$JOT_DIR/}"
# ... and the .md suffix
echo -e "${note_path%.md}"
done
}

# Make a new Markdown note
make_note() {
# Check for note name
if [[ -z "${*:-}" ]]; then
note_dir="."
# If not set, create one from today's date
note_name="note-$(date +%Y-%m-%d)"
# If not, create one from today's date
note_dir="$(date +%Y/%m)"
note_name="$(date +%d)"
else
note_dir="$(dirname "${*}")"
# Replace spaces with hyphens
Expand All @@ -57,7 +61,7 @@ make_note() {
$EDITOR "${JOT_DIR}/${note_dir}/${note_name}.md"
}

# Drives the program
# Drive it
main() {
# Check if $JOT_DIR is set
if [[ -z "${JOT_DIR:-}" ]]; then
Expand All @@ -82,5 +86,5 @@ main() {
fi
}

# Call the program
# Call it
main "${@:-}"
69 changes: 69 additions & 0 deletions test.bats
@@ -0,0 +1,69 @@
#!/usr/bin/env bats

# Local directory relative to PWD
export JOT_DIR=./.tmp

# Don't open interactively—touching is fine
export EDITOR=touch

# Stub out to use local `jot`
jot() {
./jot "${@}"
}

setup() {
mkdir -p "${JOT_DIR}"
}

teardown() {
rm -rf "${JOT_DIR}"
}

@test "jot --help prints usage & exits cleanly" {
run jot --help
[[ "${lines[0]}" == "Usage:" ]]
[[ "${status}" == 0 ]]
}

@test "jot creates \$JOT_DIR if not exist" {
rm -rf "${JOT_DIR}"
[[ ! -d "${JOT_DIR}" ]]
run jot --help
[[ -d "${JOT_DIR}" ]]
[[ "${status}" == 0 ]]
}

@test "jot no args makes a note from today" {
today="$(date +%Y/%m/%d)"
run jot
[[ -f "${JOT_DIR}/${today}.md" ]]
[[ "${status}" == 0 ]]
}

@test "jot --list with single note works" {
run jot --list
[[ "${output}" == "${today}" ]]
[[ "${status}" == 0 ]]
}

@test "jot with simple note name works" {
testnote1="testnote"
run jot "${testnote1}"
[[ -f "${JOT_DIR}/${testnote1}.md" ]]
[[ "${status}" == 0 ]]
}

@test "jot with more complex note name works" {
testnote2="directory/containing/some/testnote"
run jot "${testnote2}"
[[ -f "${JOT_DIR}/${testnote2}.md" ]]
[[ "${status}" == 0 ]]
}

@test "jot --list with multiple notes works" {
run jot --list
[[ "${lines[0]}" == "${today}" ]]
[[ "${lines[1]}" == "${testnote1}" ]]
[[ "${lines[2]}" == "${testnote2}" ]]
[[ "${status}" == 0 ]]
}

0 comments on commit f7c388d

Please sign in to comment.