Repository navigation
Expand file tree
/
Copy pathnb
More file actions
executable file
·11401 lines (10080 loc) · 297 KB
/
Copy pathnb
File metadata and controls
executable file
·11401 lines (10080 loc) · 297 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
###############################################################################
# __ _
# \ \ _ __ | |__
# \ \ | '_ \| '_ \
# / / | | | | |_) |
# /_/ |_| |_|_.__/
#
# [nb] Command line note-taking, bookmarking, archiving with plain-text data
# storage, encryption, filtering and search, Git-backed versioning and syncing,
# Pandoc-backed conversion, global and local notebooks, customizable color
# themes, plugins, and more in a single portable, user-friendly script.
#
# ❯ https://github.com/xwmx/nb
# ❯ https://xwmx.github.io/nb
#
# Based on Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2015-present William Melody ┯ hi@williammelody.com
# ┕ https://www.williammelody.com
#
# Overview
# ========
#
# - Configuration and Setup
# - Helper Functions (grouped, alphabetical order)
# - Help
# - Subcommand Functions (alphabetical order)
# - Plugins
# - Option Parsing
# - _main() / Dispatch
#
# Target Bash Versions: >= 3.2.57
#
# AGPLv3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###############################################################################
###############################################################################
# Strict Mode
#
# More Information:
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
###############################################################################
set -o nounset
set -o errexit
set -o pipefail
set -o noglob
IFS=$'\n\t'
###############################################################################
# Environment
###############################################################################
# $_VERSION
#
# The most recent program version.
_VERSION="5.5.1"
# $_ME
#
# This program's basename.
_ME="$(basename "${0}")"
# $_MY_DIR
#
# The directory containing $_ME.
_MY_DIR="$(cd "$(dirname "${0}")"; pwd)"
# $_MY_PATH
#
# This program's full path.
_MY_PATH="${_MY_DIR}/${_ME}"
# $_CURRENT_WORKING_DIR
#
# The current working directory in which the program was invoked.
_CURRENT_WORKING_DIR="${PWD}"
# $_REPO
#
# The <user>/<repo> identifier for this project's git repository.
_REPO="xwmx/nb"
# $_REPO_MAIN_BRANCH
#
# The name of the main branch in this project's git repository.
_REPO_MAIN_BRANCH="master"
# $_REPO_RAW_URL
#
# The base URL for raw files.
_REPO_RAW_URL="https://raw.githubusercontent.com/${_REPO}/${_REPO_MAIN_BRANCH}"
# $_LANG & $_LC_ALL
#
# The original values of $LANG and $LC_ALL.
_LANG="${LANG:-}" _LC_ALL="${LC_ALL:-}"
###############################################################################
# Utilities
###############################################################################
# _command_exists()
#
# Usage:
# _command_exists <name>
#
# Exit / Error Status:
# 0 (success, true) If a command <name> is defined in the current environment.
# 1 (error, false) If not.
#
# Information on why `hash` is used here:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "${1}" 2>/dev/null
}
# _contains()
#
# Usage:
# _contains <query> <list-item>...
#
# Exit / Error Status:
# 0 (success, true) If the item is included in the list.
# 1 (error, false) If not.
#
# Examples:
# _contains "${_query}" "${_list[@]}"
_contains() {
local _query="${1:-}"
shift
if [[ -z "${_query}" ]] ||
[[ -z "${*:-}" ]]
then
return 1
fi
for __element in "${@}"
do
[[ "${__element}" == "${_query}" ]] && return 0
done
return 1
}
# _sed_i()
#
# Usage:
# _sed_id <sed argument>...
#
# Description:
# `sed -i` takes an extension on macOS, but that extension can cause errors
# in GNU `sed`. Detect which `sed` is available and call it with the
# appropriate arguments.
#
# More info:
# https://stackoverflow.com/q/43171648
# https://stackoverflow.com/a/16746032
_sed_i() {
if sed --help >/dev/null 2>&1
then # GNU
sed -i "${@}"
else # BSD
sed -i '' "${@}"
fi
}
###############################################################################
# Debug
###############################################################################
# _debug()
#
# Usage:
# _debug <command> <options>...
#
# Description:
# Execute a command and print to standard error. The command is expected to
# print a message and should typically be either `echo`, `printf`, or `cat`.
#
# Example:
# _debug printf "Debug info. Variable: %s\\n" "$0"
__DEBUG_COUNTER=0
__DEBUG_START_TIME=
_debug() {
# Usage: __debug_get_timestamp
__debug_get_timestamp() {
if hash "gdate" 2>/dev/null
then
gdate +%s%3N
elif date --version >/dev/null 2>&1
then
date +%s%3N
else
return 1
fi
}
if ((${_USE_DEBUG:-0}))
then
__DEBUG_COUNTER=$((__DEBUG_COUNTER+1))
printf "🐛 %s " "${__DEBUG_COUNTER}"
"${@}"
if [[ -n "${__DEBUG_START_TIME:-}" ]]
then
printf "⏱ %s\\n" "$(($(__debug_get_timestamp)-__DEBUG_START_TIME))"
elif __DEBUG_START_TIME="$(__debug_get_timestamp)"
then
printf "⏱ 0\\n"
fi
printf "―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n"
fi 1>&2
}
###############################################################################
# Error Messages
###############################################################################
# _exit_1()
#
# Usage:
# _exit_1 <command>
#
# Description:
# Exit with status 1 after executing the specified command with output
# redirected to standard error. The command is expected to print a message
# and should typically be either `echo`, `printf`, or `cat`.
_exit_1() {
{
[[ "${1:-}" == "_help" ]] || printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
exit 1
}
# _warn()
#
# Usage:
# _warn <command>
#
# Description:
# Print the specified command with output redirected to standard error.
# The command is expected to print a message and should typically be either
# `echo`, `printf`, or `cat`.
_warn() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
}
###############################################################################
# Option Helpers
###############################################################################
# _option_get_value()
#
# Usage:
# _option_get_value <option> <value>
#
# Description:
# Given a flag (e.g., -e | --example) return the value or exit 1 if value
# is blank or appears to be another option.
_option_get_value() {
local __arg="${1:-}"
local __val="${2:-}"
if [[ -n "${__val:-}" ]] && [[ ! "${__val:-}" =~ ^- ]]
then
printf "%s\\n" "${__val}"
else
_exit_1 printf \
"%s requires a valid argument.\\n" \
"$(_color_primary "${__arg}")"
fi
}
# _option_value_is_present()
#
# Usage:
# _option_value_is_present <value>
#
# Exit / Error Status:
# 0 (success, true) The argument is present and does not match as an option
# flag.
# 1 (error, false) The argument is blank or matches as an option flag.
_option_value_is_present() {
[[ -n "${1:-}" ]] && [[ ! "${1:-}" =~ ^- ]]
}
###############################################################################
# Configuration
###############################################################################
# Configuration File ####################################### Configuration File
# $NBRC_PATH
#
# Default: `$HOME/.nbrc`
#
# The location of the .nbrc configuration file.
export NBRC_PATH="${NBRC_PATH:-"${HOME}/.nbrc"}"
# Handle symlinked NBRC_PATH.
if [[ -L "${NBRC_PATH}" ]]
then
NBRC_PATH="$(readlink "${NBRC_PATH}")"
fi
# Source rc file.
if [[ -e "${NBRC_PATH}" ]]
then
source "${NBRC_PATH}"
fi
# Base Configuration ####################################### Base Configuration
# $NB_DIR
#
# Default: `$HOME/.nb`
#
# The location of the directory that contains the notebooks.
export NB_DIR="${NB_DIR:-"${HOME}/.nb"}"
# Handle symlinked NB_DIR.
if [[ -L "${NB_DIR:-}" ]]
then
NB_DIR="$(readlink "${NB_DIR}")"
fi
# Validate that NB_DIR exists and is writable.
if [[ -z "${NB_DIR:?}" ]] ||
[[ "${NB_DIR}" == "/" ]] ||
{
[[ -e "${NB_DIR}" ]] &&
[[ ! -w "${NB_DIR}" ]]
}
then
_exit_1 cat <<HEREDOC
NB_DIR is not valid:
${NB_DIR}
Remove any NB_DIR settings in .nbrc to reset to default:
${NBRC_PATH}
NB_DIR settings prompt:
${_ME} settings nb_dir
HEREDOC
fi
# $NB_AUTO_SYNC
#
# Default: '1'
#
# When set to '1', each `_git_checkpoint()` call will automativally run
# `$_ME sync`. To disable this behavior, set the value to '0'.
export NB_AUTO_SYNC="${NB_AUTO_SYNC:-1}"
# `$NB_DEFAULT_EXTENSION`
#
# Default: 'md'
#
# Example Values: 'md' 'org'
export NB_DEFAULT_EXTENSION="${NB_DEFAULT_EXTENSION:-md}"
# $NB_ENCRYPTION_TOOL
#
# Default: 'openssl'
#
# Supported Values: 'gpg' 'openssl'
export NB_ENCRYPTION_TOOL="${NB_ENCRYPTION_TOOL:-openssl}"
# $NB_FOOTER
#
# Default: '1'
#
# Supported Values: '0' '1'
export NB_FOOTER="${NB_FOOTER:-1}"
# $NB_HEADER
#
# Default: '2'
#
# Supported Values: '0' '1' '2' '3'
export NB_HEADER="${NB_HEADER:-2}"
# $NB_LIMIT
#
# Default: '20'
#
# Supported Values: any positive number
export NB_LIMIT="${NB_LIMIT:-20}"
# $NB_SYNTAX_THEME
#
# Default: 'base16'
#
# Supported Values: Theme names listed with `bat --list-themes`
export NB_SYNTAX_THEME="${NB_SYNTAX_THEME:-base16}"
# $EDITOR ############################################################# $EDITOR
# Set default $EDITOR if one has not been set.
export EDITOR="${EDITOR:-}"
__set_editor() {
local _editors=(
code
subl
micro
mate
macdown
nano
pico
vim
vi
emacs
)
if [[ -z "${EDITOR:-}" ]]
then
if [[ -n "${VISUAL:-}" ]]
then
EDITOR="${VISUAL}"
else
for __editor in "${_editors[@]}"
do
if hash "${__editor}" 2>/dev/null
then
EDITOR="${__editor}"
break
fi
done
fi
if [[ -z "${EDITOR:-}" ]]
then
cat <<HEREDOC
Command line text editor not found. Set the \$EDITOR variable in your
environment or in the \`${_ME}\` configuration file located at:
${NBRC_PATH}
More information about setting \$EDITOR:
https://askubuntu.com/q/432524
\`${_ME}\` uses text editors with command line support, such as:
- Visual Studio Code
https://code.visualstudio.com
- Sublime Text
https://www.sublimetext.com
- Atom
https://atom.io
- MacDown
https://macdown.uranusjr.com
- Vim
https://en.wikipedia.org/wiki/Vim_(text_editor)
- Emacs
https://en.wikipedia.org/wiki/Emacs
- TextMate
https://macromates.com
- nano
https://en.wikipedia.org/wiki/GNU_nano
- or many of these:
https://en.wikipedia.org/wiki/List_of_text_editors
HEREDOC
exit 1
fi
fi
}; __set_editor
# Color ################################################################# Color
# $_COLOR_ENABLED
#
# Default: '1'
#
# Supported Values: '0' '1'
export _COLOR_ENABLED="${_COLOR_ENABLED:-1}"
# $NB_COLOR_THEME
#
# Default: 'nb'
#
# The color theme.
export NB_COLOR_THEME="${NB_COLOR_THEME:-nb}"
case "${NB_COLOR_THEME}" in
nb)
: # Colors assigned as default values below.
;;
blacklight)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-39}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-56}"
;;
console)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-40}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-28}"
;;
desert)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-179}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-95}"
;;
electro)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-200}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-26}"
;;
forest)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-29}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-94}"
;;
monochrome)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-248}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-241}"
;;
ocean)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-75}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-26}"
;;
raspberry)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-162}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-90}"
;;
unicorn)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-183}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-153}"
;;
utility)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-227}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-8}"
;;
esac
export _NB_COLOR_THEMES=(
blacklight
console
desert
electro
forest
monochrome
nb
ocean
raspberry
unicorn
utility
)
# User defined themes can be installed in the $NB_DIR/.plugins directory.
# Themes have an .nb-theme extension and contain a single if statment
# assigning the color environment variables to tput ANSI color numbers.
#
# Example:
#
# # filename: ~/.nb/.plugins/example.nb-theme
# if [[ "${NB_COLOR_THEME}" == "example" ]]
# then
# export NB_COLOR_PRIMARY=68
# export NB_COLOR_SECONDARY=8
# fi
#
# To view a list of available color numbers, run `nb settings colors`
__load_themes() {
if [[ -d "${NB_DIR}/.themes" ]] &&
[[ ! -e "${NB_DIR}/.plugins" ]]
then # migrate legacy .themes directory. TODO: Remove.
mv "${NB_DIR}/.themes" "${NB_DIR}/.plugins"
fi
if [[ -d "${NB_DIR}/.plugins" ]]
then
set +f
for __file in "${NB_DIR}/.plugins"/*.nb-theme*
do
if [[ -e "${__file}" ]]
then
local _basename
_basename="$(basename "${__file}")"
local _name="${_basename%%.*}"
_NB_COLOR_THEMES+=("${_name}")
source "${__file}"
fi
done
set -f
fi
}; __load_themes
# $NB_COLOR_PRIMARY
#
# Default: Value depends on terminal capabilities.
#
# Set highlighting color. This should be set to an xterm color number, usually
# a value between 1 and 256. For a table of common colors and their numbers
# run:
# nb settings colors
#
# Supported Values: [0..255+]
__set_nb_color_primary() {
local _colors=
_colors="$(tput colors)"
if [[ -n "${_colors}" ]] && [[ "${_colors}" -gt 8 ]]
then
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-69}"
else
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-4}"
fi
}; __set_nb_color_primary
# $NB_COLOR_SECONDARY
#
# Default: '8'
#
# Color for lines and other accents. This should be set to an xterm color
# number, usually a value between 1 and 256. For a table of common colors and
# their numbers, run:
# nb settings colors
#
# Supported Values: [0..255+]
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-8}"
# _color_dim()
#
# Usage:
# _color_dim <string>
#
# Print the given string with the dim color.
_color_dim() {
if ! ((_COLOR_ENABLED))
then
printf "%s%s\\n" "${_TPUT_SGR0}" "${1:-}"
return 0
fi
printf "%s%s%s%s" \
"${_TPUT_SGR0}" \
"${_TPUT_SETAF_8}" \
"${1:-}" \
"${_TPUT_SGR0}"
}
# _color_id_brackets()
#
# Usage:
# _color_id_brackets <id>
#
# Description:
# Print <id> with surrounding color brackets.
_color_id_brackets() {
if ! ((_COLOR_ENABLED))
then
printf "%s%s\\n" "${_TPUT_SGR0}" "${1:-}"
return 0
fi
printf "%s%s[%s%s%s]%s" \
"${_TPUT_SGR0}" \
"${_TPUT_SETAF_8}" \
"${_TPUT_COLOR_PRIMARY}" \
"${1:-}" \
"${_TPUT_SETAF_8}" \
"${_TPUT_SGR0}"
}
# _color_primary()
#
# Usage:
# _color_primary <string> [--underline]
#
# Description:
# Use `tput` to highlight the given string.
export _TPUT_SGR0= ; _TPUT_SGR0="$(tput sgr0)"
export _TPUT_SMUL= ; _TPUT_SMUL="$(tput smul)"
export _TPUT_SETAF_8= ; _TPUT_SETAF_8="$(tput setaf 8)"
export _TPUT_COLOR_PRIMARY=
_TPUT_COLOR_PRIMARY="$(tput setaf "${NB_COLOR_PRIMARY}")"
_color_primary() {
if ! ((_COLOR_ENABLED))
then
printf "%s%s\\n" "${_TPUT_SGR0}" "${1:-}"
return 0
fi
if [[ "${2:-}" == "--underline" ]]
then
printf "%s%s%s%s%s\\n" \
"${_TPUT_SGR0}" \
"${_TPUT_SMUL}" \
"${_TPUT_COLOR_PRIMARY}" \
"${1:-}" \
"${_TPUT_SGR0}"
else
printf "%s%s%s%s\\n" \
"${_TPUT_SGR0}" \
"${_TPUT_COLOR_PRIMARY}" \
"${1:-}" \
"${_TPUT_SGR0}"
fi
}
# _color_secondary()
#
# Usage:
# _color_secondary <string>
#
# Description:
# Highlight the given string using the accent color.
export _TPUT_COLOR_SECONDARY=
_TPUT_COLOR_SECONDARY="$(tput setaf "${NB_COLOR_SECONDARY}")"
_color_secondary() {
if ! ((_COLOR_ENABLED))
then
printf "%s%s\\n" "${_TPUT_SGR0}" "${1:-}"
return 0
fi
printf "%s%s%s\\n" "${_TPUT_COLOR_SECONDARY}" "${1:-}" "${_TPUT_SGR0}"
}
# Current Notebook ########################################### Current Notebook
# $NB_NOTEBOOK_PATH
#
# Default: `$NB_DIR/home`
#
# The path to the current notebook.
export NB_NOTEBOOK_PATH="${NB_NOTEBOOK_PATH:-"${NB_DIR}/home"}"
# $_GLOBAL_NOTEBOOK_PATH
#
# The path of the current global notebook.
export _GLOBAL_NOTEBOOK_PATH=
# $_LOCAL_NOTEBOOK_PATH
#
# The path of the local notebook, if one is found. The current working
# directory and all parents are searched.
export _LOCAL_NOTEBOOK_PATH=
# __set_notebook_paths()
#
# Usage:
# __set_notebook_paths
#
# Description:
# Set the the notebook path variables to reflect the current environment.
__set_notebook_paths() {
__set_local_notebook_path() {
if [[ "${PWD}" == "/" ]]
then
cd "${_CURRENT_WORKING_DIR}"
return 0
elif [[ -d "${PWD}/.git" ]] &&
[[ -f "${PWD}/.index" ]]
then
_LOCAL_NOTEBOOK_PATH="${PWD}"
cd "${_CURRENT_WORKING_DIR}"
return 0
else
cd ..
__set_local_notebook_path
fi
}; __set_local_notebook_path
__set_global_notebook() {
if [[ -e "${NB_DIR}/.current" ]]
then
local _global_notebook_name=
_global_notebook_name="$(cat "${NB_DIR}/.current")"
if [[ -d "${NB_DIR}/${_global_notebook_name}" ]]
then
_GLOBAL_NOTEBOOK_PATH="${NB_DIR}/${_global_notebook_name}"
fi
fi
}; __set_global_notebook
if [[ -n "${_LOCAL_NOTEBOOK_PATH:-}" ]]
then
NB_NOTEBOOK_PATH="${_LOCAL_NOTEBOOK_PATH}"
else
NB_NOTEBOOK_PATH="${_GLOBAL_NOTEBOOK_PATH}"
fi
}; __set_notebook_paths
# Cache & Temp ################################################### Cache & Temp
# $_NB_CACHE_PATH
#
# The full path to the cache directory.
export _NB_CACHE_PATH="${NB_DIR}/.cache"
if [[ -d "${NB_DIR}" ]] && [[ ! -e "${_NB_CACHE_PATH}" ]]
then
mkdir -p "${_NB_CACHE_PATH}"
fi
# $_NB_TEMP_DIRECTORY
#
# The full path to the temp directory.
export _NB_TEMP_DIRECTORY
_NB_TEMP_DIRECTORY="$(mktemp -d)"
# _temp()
#
# Usage:
# _temp cache [clear]
# _temp directory
# _temp file <basename>
# _temp cleanup
#
# Subcommands:
# cache Print the full path to the cache directory, or optionally clear
# the cache.
# directory Print the full path the the temp directory.
# file Print the full path for <basename> within the temp directory.
# cleanup Force delete the temp directory.
# Description:
# Manage temp and cache files and directories.
_temp() {
# _temp_validate_directory <path>
_temp_validate_directory() {
[[ -n "${1:?}" ]] &&
[[ -d "${1}" ]] &&
[[ "${1}" != "/" ]] &&
[[ "${1}" != "${HOME}" ]] &&
[[ "${1}" != "${PWD}" ]]
}
local _subcommand="${1:-}"
case "${_subcommand}" in
cache)
if [[ "${2:-}" == "clear" ]]
then
if _temp_validate_directory "${_NB_CACHE_PATH:?}"
then
rm -r "${_NB_CACHE_PATH:?}"
mkdir -p "${_NB_CACHE_PATH}"
fi
else
printf "%s\\n" "${_NB_CACHE_PATH}"
fi
;;
directory)
printf "%s\\n" "${_NB_TEMP_DIRECTORY:-}"
;;
file)
[[ -z "${2:-}" ]] && return 1
printf "%s/%s" "${_NB_TEMP_DIRECTORY}" "${2:-}"
;;
cleanup)
if _temp_validate_directory "${_NB_TEMP_DIRECTORY:?}"
then
rm -rf "${_NB_TEMP_DIRECTORY:?}"
fi
;;
esac
}
trap "_temp cleanup" EXIT
###############################################################################
# Git
###############################################################################
# $_GIT_ENABLED
#
# Default: '1'
#
# Supported Values: '0' '1'
export _GIT_ENABLED="${_GIT_ENABLED:-1}"
# _git_checkpoint()
#
# Usage:
# _git_checkpoint <target directory> <commit message>
#
# Description:
# Commit all files in <target directory> with the provided commit message.
_git_checkpoint() {
((_GIT_ENABLED)) || return 0
# Usage: _git_checkpoint_commit <target directory> <message>
_git_checkpoint_commit() {
local _target_directory="${1:-}"
local _message="${2:-}"
if [[ -z "${_target_directory:-}" ]] || [[ -z "${_message:-}" ]]
then
_exit_1 printf \
"Usage: _git_checkpoint_commit <message> <target directory>"
fi
git -C "${_target_directory}" add --all &&
git -C "${_target_directory}" commit -a -m "${_message}"
}
# Usage: _git_checkpoint_sync <target directory>
_git_checkpoint_sync() {
local _target_directory="${1:-}"
[[ -z "${_target_directory:-}" ]] &&
_exit_1 printf "Usage: _get_checkpoint_sync <target-directory>"
local _git_branch=
_git_branch="$(git -C "${_target_directory}" rev-parse --abbrev-ref HEAD)"
if git -C "${_target_directory}" show-branch "remotes/origin/${_git_branch}" > /dev/null 2>&1
then
git -C "${_target_directory}" fetch &>/dev/null || {
_warn printf "Unable to fetch from remote.\\n"
return 1
}
if ! git -C "${_target_directory}" rebase "origin/${_git_branch}" &>/dev/null
then
local _merge_error=1
local _conflicted_files
_conflicted_files=($(
GIT_PAGER='' git -C "${_target_directory}" diff --name-only --diff-filter=U
))
local _conflicted_text_files=()
local _conflicted_binary_files=()
if [[ -n "${_conflicted_files[*]:-}" ]]
then
if _contains ".index" "${_conflicted_files[@]}"
then
# Remove git conflict markers to keep both.
# More info: https://stackoverflow.com/a/55187779
_sed_i \
-e '/^<<<<<<</d' \
-e '/^>>>>>>>/d' \
-e '/=======/d' \
"${_target_directory}/.index"
git -C "${_target_directory}" add ".index"
fi
for __file in "${_conflicted_files[@]}"
do
[[ "${__file}" == ".index" ]] && continue
if _file_is_text "${__file}"
then
_conflicted_text_files+=("${__file}")
git -C "${_target_directory}" add \
"${__file}"
else
git -C "${_target_directory}" checkout \
"origin/${_git_branch}" -- "${__file}"
local _conflicted_file=
_conflicted_file="$(
_notebooks show "${_target_directory}" \
--filename "${__file%%.*}--conflicted-copy.${__file#*.}"
)"
_conflicted_binary_files+=("${_conflicted_file}")
git -C "${_target_directory}" mv \
"${__file}" "${_conflicted_file}"
git -C "${_target_directory}" checkout \
"${_git_branch}" -- "${__file}"
git -C "${_target_directory}" add "${_conflicted_file}"
git -C "${_target_directory}" add "${__file}"
fi
done
GIT_EDITOR=true git -C "${_target_directory}" rebase \
--continue &>/dev/null &&
_merge_error=0
fi
if ((_merge_error))
then
git -C "${_target_directory}" rebase --abort
_warn printf \