blob: 399daceddae6b9306f8d5afa3f26a637cfa610a8 (
plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
#!/usr/bin/env nu
# +---------------+
# | Configuration |
# +---------------+
# Default config options.
mut config = {
notebook_dir: "~/Sync/notebook"
journal_dir: "~/Sync/notebook/journal"
trash_dir: "~/Sync/notebook/.trash"
}
# Read config options from environment variables.
if ((try {$env.spiral_notebook_dir}) != null) {
$config.notebook_dir = $env.spiral_notebook_dir
}
if ((try {$env.spiral_journal_dir}) != null) {
$config.journal_dir = $env.spiral_journal_dir
}
if ((try {$env.spiral_trash_dir}) != null) {
$config.trash_dir = $env.spiral_trash_dir
}
# Make config immutable.
let config = $config
# +-------------+
# | Subcommands |
# +-------------+
# A commandline utility written in nushell to manage your markdown notebook!
def "main" [] {}
# Create a new note.
def "main new" [
title?: string # The title of the new note.
--no-frontmatter (-n) # Don't add a frontmatter to the note.
] {
mut title = $title
if ($title == null) {
$title = get_string_input "Enter a title: "
}
if $no_frontmatter {
create_note --no-frontmatter $title
} else {
create_note $title
}
}
# Open a pre-existing note.
def "main open" [
--by-title (-t) # Filter notes by their title (disables preview)
--by-contents (-c) # Search through the contents of the notebook.
title?: string # The title of the note to open.
] {
if ($by_title and $by_contents) {
error make {msg: "Only one flag can be used at a time."}
}
if ($title != null) {
if ((title_to_path $title) | path exists) {
return (title_to_path $title)
} else {
if (confirm $"The note ($title) does not exist. Would you like to create it? ") {
create_note $title
}
}
} else if $by_title {
edit $"(get_choice_from_note_titles --dir $config.notebook_dir)"
} else if $by_contents {
edit (get_choice_from_dir_file_contents --dir $config.notebook_dir)
} else {
edit $"(get_choice_from_dir --dir $config.notebook_dir)"
}
}
# Open a note based on the tag.
def "main tags" [
tag?: string # The tag to select a note from.
] {
# This section of code creates nested records with tags and notes and stores them in $tag_record.
let note_table = list_notes --dir $config.notebook_dir
let tag_list = $note_table
| get tags
| flatten
| where (($it != null) and ($it != ""))
mut tag_record = {}
print $tag_list
for tag in $tag_list {
mut notes = {}
for note_path in ($note_table | get path) {
let note_tags = ($note_table | where path == $note_path | get tags | flatten)
let note_title = ($note_table | where path == $note_path | get title | to text | str trim)
if ($note_tags != []) {
for note_tag in $note_tags {
if ($note_tag == $tag) {
$notes = $notes | insert $note_title { $note_path }
}
}
}
}
let notes = $notes
# $tag_record = $tag_record | insert $tag { $notes }
}
# Actual logic of the subcommand
if ($tag == null) {
let user_tag_choice = get_choice_from_list --list $tag_list
if ($user_tag_choice != "") {
let note_record = $tag_record | get $user_tag_choice
let user_note_title_choice = get_choice_from_list --list ($note_record | columns)
if ($user_note_title_choice != "") {
edit ($note_record | get $user_note_title_choice)
}
}
} else {
if ($tag_list | any {|item| $item == $tag}) {
let note_record = $tag_record | get $tag
let user_note_title_choice = get_choice_from_list --list ($note_record | columns)
if ($user_note_title_choice != "") {
edit ($note_record | get $user_note_title_choice)
}
} else {
print $"The tag '($tag)' does not exist."
}
}
}
# Get a table of notes and their properties.
def "main list" [] {
return (list_notes --dir $config.notebook_dir)
}
# Send a note to the trash.
def "main remove" [
--by-title (-t) # Filter notes by their title (disables preview)
--by-contents (-c) # Search through the contents of the notebook.
title?: string # The title of the note to send to the trash.
] {
if ($by_title and $by_contents) {
error make {msg: "Only one flag can be used at a time."}
}
if ($title != null) {
if ((title_to_path $title) | path exists) {
return (title_to_path $title)
} else {
if (confirm $"The note ($title) does not exist. Would you like to create it? ") {
create_note $title
}
}
} else if $by_title {
mv (select_note --by-title --dir $config.notebook_dir)
} else if $by_contents {
^$env.editor (select_note --by-contents --dir $config.notebook_dir)
} else {
^$env.editor (select_note --dir $config.notebook_dir)
}
}
# Explains how to configural Spiral.
def "main config" [] {
print "The default options:"
print $config
print "These can be modified by editing the environment variables corresponding to each option. For example, to change the notebook directory, set $env.spiral_notebook_dir='~/custom-dir/notebook'."
}
# +------------------+
# | Action Functions |
# +------------------+
def create_note [--no-frontmatter, title: string] {
let note_path = title_to_path $title
if ($note_path | path exists) {
if (confirm "This note already exists. Would you like to open it?") {
edit $note_path
} else {exit}
} else {
if $no_frontmatter {
generate_note_text --no-frontmatter --title $title | save $note_path
} else {
generate_note_text --title $title | save $note_path
}
edit $"($note_path):100:100"
}
}
def create_journal_entry [
] {
}
def edit [
file_path: string
] {
if (($file_path | path type) == file) {
^$env.editor ($file_path | path expand)
}
}
# +---------------------+
# | Interface Functions |
# +---------------------+
def get_string_input [prompt: string] {
print $prompt
return (input)
}
def get_confirmation [prompt: string] {
print $"($prompt) \(Y/n)"
let confirmation = (input | str downcase)
if ($confirmation == "n" ) {
return false
} else {return true}
}
def get_choice_from_list [ --list: list ] {
return (($list | to text) | tv)
} # -> string
def get_choice_from_dir [ --dir: string ] {
return $"($config.notebook_dir | path expand)/(tv files ($dir | path expand))"
} # -> full path
def get_choice_from_dir_file_contents [ --dir: string ] {
return $"($config.notebook_dir | path expand)/(tv text ($dir | path expand))"
} # -> full path: line number
def get_choice_from_note_titles [ --dir: string ] {
let note_table = list_notes --dir $config.notebook_dir
let user_choice = get_choice_from_list --list ($note_table | get title)
let user_choice_path = (
$note_table
| where title == $user_choice
| get path
| to text
| str trim
)
if (($user_choice_path | path type) == file) {
return $user_choice_path
}
} # -> full path
# +-------------------------------+
# | String Manipulation Functions |
# +-------------------------------+
def title_to_path [title: string] {
return (
$"($config.notebook_dir)/($title | str kebab-case).md"
| path expand
)
} # -> full path
def strip_line_number [string: string] {
return (
$string
| split row ":"
| select 0
| to text
| str trim
)
} # -> string
def generate_note_text [
--no-frontmatter
--title: string
] {
if $no_frontmatter {
return $"# ($title)\n"
} else {
let frontmatter_info = {
title: $title
date: (date now)
lang: "en-US"
tags: ['note']
}
return $"---\n($frontmatter_info | to yaml)---\n\n# ($title)"
}
} # -> string
# +-----------------------------+
# | File Manipulation Functions |
# +-----------------------------+
def list_notes [--dir: string] {
return ((
ls ($dir | path expand)
| where type == file
| get name
) | par-each {
|note_path|
let frontmatter = parse_frontmatter $note_path
if ($frontmatter != null) {
return {
title: (try {$frontmatter.title})
path: $note_path
tags: (try {$frontmatter.tags})
date: (try {$frontmatter.date})
lang: (try {$frontmatter.lang})
}
}
})
} # -> table
def parse_frontmatter [path: string] {
let frontmatter = try {
open ($path | path expand)
| split row "---"
| get 1
| from yaml
}
if ($frontmatter == null) {
let title = try {
open ($path | path expand)
| split row "#"
| get 1
| split row "\n"
| get 0
| str trim
}
return {title: $title}
}
return $frontmatter
} # -> record
|