-
Notifications
You must be signed in to change notification settings - Fork 15
/
zk
executable file
·119 lines (112 loc) · 3.87 KB
/
zk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
# This is a shell script to simplify/automate certain Zettelkasten-related
# tasks, such as creating Zettels or searching the Zettelkasten
# Set some variables for use later
ZKUID="$(date +%Y%m%d%H%M%S)"
ZKPATH="${HOME}/Sync/Zettelkasten"
HELP="Usage: zk [ new | name-search | body-search | open ]"
# If the user supplied "--help", then provide help text
if [ "$1" = "--help" ]; then
echo -e "${HELP}\n"
exit 0
fi
# Set certain OS-specific variables
case "${OSTYPE}" in
darwin*)
CODE="/usr/local/bin/code"
;;
linux*)
CODE="/usr/bin/code"
;;
*)
echo -e "This script is not supported on this operating system\n"
exit 1
;;
esac
# Test to ensure the user passed at least one parameter (the subcommand)
if [ ! "$#" -ge 1 ]; then
echo -e "No valid subcommand was provided, exiting\n"
echo -e "${HELP}\n"
exit 1
fi
# Parse the command line to get the subcommand
SUBCMD="$1"
# Switch into the Zettelkasten directory for the remainder of the script
cd "${ZKPATH}"
# Take action based on commands and parameters provided by the user
case "${SUBCMD}" in
new)
# Test to ensure the user passed a name parameter
if [ "$#" -ne 2 ]; then
echo -e "An invalid name was specified for the new zettel, exiting\n"
echo -e "Usage: zk new name-of-new-zettel\n"
exit 1
fi
# Parse out the command line further
NAME="$2"
if [ "${NAME}" = "--help" ]; then
echo -e "Usage: zk new name-of-new-zettel\n"
echo -e "It is recommended to use hyphens to separate words in the name of the zettel;\nfor example, 'name-of-new-zettel'\n"
exit 1
fi
# Calculate correct file name
ZETTELNAME="${ZKUID}-${NAME}.md"
# Create file with initial contents
touch "${ZETTELNAME}"
echo "${ZKUID}-${NAME}" >> "${ZETTELNAME}"
# Pass off to the editor
$CODE "${ZKPATH}" -g "${ZETTELNAME}"
;;
name-search)
# Test to ensure the user passed only the "name-search" subcommand
if [ "$#" -ne 1 ]; then
echo -e "Only the 'name-search' subcommand should be provided\n"
echo -e "Usage: zk name-search\n"
exit 1
fi
# Use fd to list all files, pass that to fzf, and then open in editor
fd -t f . "${ZKPATH}" | fzf | xargs code "${ZKPATH}" -g
;;
body-search)
# Test to ensure the user passed only the "body-search" subcommand
if [ "$#" -ne 2 ]; then
echo -e "A search term should be provided with the 'body-search' subcommand\n"
echo -e "Usage: zk body-search search-term\n"
exit 1
fi
# Use rg to find all files, pass that to fzf, and then open in editor
TERM="$2"
rg --files-with-matches "${TERM}" | fzf | xargs code "${ZKPATH}" -g
;;
open)
# Open the Zettelkasten folder in the editor
${CODE} "${ZKPATH}"
;;
preview)
# Test to ensure the user passed only the "preview" command
if [ "$#" -ne 1 ]; then
echo -e "Only the 'preview' subcommand should be provided\n"
echo -e "Usage: zk preview\n"
exit 1
fi
# Take action based on the OS
case "${OSTYPE}" in
darwin*)
fd -t f . "${ZKPATH}" | fzf | xargs open -b com.brettterpstra.marked2
;;
linux*)
fd -t f . "${ZKPATH}" | fzf | xargs bat
;;
*)
echo -e "This script is not supported on this operating system\n"
exit 1
;;
esac
;;
*)
# Present an error about an unsupported subcommand
echo -e "No valid subcommand was provided, exiting\n"
echo -e "${HELP}\n"
exit 1
;;
esac