-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb
executable file
·26329 lines (22584 loc) · 703 KB
/
nb
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 and local web note-taking, bookmarking, and archiving with
# plain text data storage, encryption, filtering and search, pinning, #tagging,
# Git-backed versioning and syncing, Pandoc-backed conversion, global and local
# notebooks, customizable color themes, [[wiki-style linking]], 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 ┯ [email protected]
# ┕ https://www.williammelody.com
#
# Overview
# ========
#
# - Configuration and Setup
# - Helpers: Group 1 (alphabetical order)
# - Subcommands: Group 1 (alphabetical order)
# - Plugins
# - Option Parsing
# - _main() / Dispatch
# - Configuration: Group 2
# - Helpers: Group 2 (alphabetical order)
# - Subcommands: Group 2 (alphabetical order)
# - Subcommands: Group 3 (alphabetical order)
#
# Target Bash Version: 5 (target, recommended)
# Minimum Recommended Bash Version: 4 (fallback, recommended)
# Minimum Tested Bash Version: 3.2 (fallback, deprecated)
#
# 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/>.
###############################################################################
###############################################################################
# Shell Options & Strict Mode
#
# More Information:
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
# https://tldp.org/LDP/abs/html/options.html
###############################################################################
set -o errexit
set -o noglob
set -o nounset
set -o pipefail
set +o noclobber
shopt -s extglob
IFS=$'\n\t'
###############################################################################
# Environment
###############################################################################
# $_VERSION
#
# The most recent program version.
_VERSION="7.12.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}"
###############################################################################
# Utilities
###############################################################################
# _command_exists()
#
# Usage:
# _command_exists <name>
#
# Exit / Error / Return Status:
# 0 (success, true) If a command <name> is defined in the current environment.
# 1 (error, false) If not.
#
# More Information:
# http://stackoverflow.com/a/677212
_command_exists() {
if [[ "${1:-}" == "w3m" ]]
then # Detect WSL 1 (https://stackoverflow.com/a/38859331), where w3m errors.
[[ -f /proc/version ]] && grep -q Micro /proc/version && return 1
fi
hash "${1}" 2>/dev/null
}
# _contains()
#
# Usage:
# _contains <query> <list-item>...
#
# Exit / Error / Return Status:
# 0 (success, true) If the item is included in the list.
# 1 (error, false) If not.
#
# Example:
# _contains "${_query}" "${_list[@]}"
_contains() {
local _query="${1:-}"
shift
if [[ -z "${_query}" ]] ||
[[ -z "${*:-}" ]]
then
return 1
fi
local __element=
for __element in "${@}"
do
[[ "${__element}" == "${_query}" ]] && return 0
done
return 1
}
# _resolve_symlink()
#
# Usage:
# _resolve_symlink <path>
#
# Description:
# Resolve the real path or target for a symbolic link.
_resolve_symlink() {
if hash "realpath" 2>/dev/null
then
realpath "${@:-}"
else
readlink "${@:-}"
fi
}
# _sed_i()
#
# Usage:
# _sed_i <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 Information:
# https://stackoverflow.com/q/43171648
# https://stackoverflow.com/a/16746032
_sed_i() {
local _usage=
if _usage="$(sed --help 2>&1)"
then # GNU
sed -i "${@}"
else # BSD
if [[ "${_usage:-}" =~ unknown\ option ]]
then # OpenBSD
sed -i'' "${@}"
else # FreeBSD, macOS
sed -i '' "${@}"
fi
fi
}
# _tput()
#
# Usage:
# _tput <tput options>...
#
# Description:
# Run `tput` commands, with fallbacks to termcap names for FreeBSD and
# `stty` and escape sequences when `tput` is not found.
_tput() {
if _command_exists "tput"
then
# More info: https://stackoverflow.com/a/64214019
case "${1:-}" in
cols)
if tput cols &>/dev/null
then
tput cols
elif tput co &>/dev/null
then
tput co
else
printf "80\\n"
fi
return 0
;;
lines)
if tput lines &>/dev/null
then
tput lines
elif tput li &>/dev/null
then
tput li
else
printf "20\\n"
fi
return 0
;;
setaf|smul)
((${_COLOR_ENABLED:-1})) || return 0
;;
esac
tput "${@:-}" 2>/dev/null || {
case "${1:-}" in
sgr0)
tput me
;;
smul)
tput us
;;
setaf)
tput AF "${@:2}"
;;
esac
} 2>/dev/null || printf ""
else
case "${1:-}" in
cols|lines)
{
stty size 2>/dev/null || printf "20 80\\n"
} | if [[ "${1:-}" == "cols" ]]
then
cut -d " " -f 2
else
cut -d " " -f 1
fi
;;
setaf)
printf '\033[38;5;%sm' "${2:-}"
;;
sgr0)
printf '\033[m'
;;
smul)
printf '\033[4m'
;;
esac
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 _option="${1:-}"
local _value="${2:-}"
if [[ -n "${_value:-}" ]] && [[ ! "${_value:-}" =~ ^- ]]
then
printf "%s\\n" "${_value}"
else
_exit_1 printf \
"%s requires a valid argument.\\n" \
"$(_color_primary "${_option}")"
fi
}
# _option_value_is_present()
#
# Usage:
# _option_value_is_present <value>
#
# Exit / Error / Return 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}/.${_ME}rc"}"
# Handle symlinked NBRC_PATH.
if [[ -L "${NBRC_PATH}" ]]
then
NBRC_PATH="$(_resolve_symlink "${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}/.${_ME}"}"
# Handle symlinked NB_DIR.
if [[ -L "${NB_DIR:-}" ]]
then
NB_DIR="$(_resolve_symlink "${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 .${_ME}rc to reset to default:
${NBRC_PATH}
NB_DIR settings prompt:
${_ME} settings nb_dir
HEREDOC
fi
# $NB_AUDIO_TOOL
#
# Default: '' (first available)
#
# Example Values: 'mplayer', 'afplay'
export NB_AUDIO_TOOL="${NB_AUDIO_TOOL:-}"
# $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_BROWSER
#
# Default: value of $BROWSER
#
# Example Values: 'links', 'w3m'
export NB_BROWSER="${NB_BROWSER:-${BROWSER:-}}"
# $NB_DATA_TOOL
#
# Default: '' (first available)
#
# Example Values: 'visidata', 'sc-im'
export NB_DATA_TOOL="${NB_DATA_TOOL:-}"
# $NB_DEFAULT_EXTENSION
#
# Default: 'md'
#
# Example Values: 'md' 'org'
export NB_DEFAULT_EXTENSION="${NB_DEFAULT_EXTENSION:-md}"
# $NB_DIRECTORY_TOOL
#
# Default: '' (nb browse)
#
# Example Values: 'ranger', 'mc'
export NB_DIRECTORY_TOOL="${NB_DIRECTORY_TOOL:-}"
# $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_GUI_BROWSER
#
# Default: ''
#
# Example Value: 'firefox'
export NB_GUI_BROWSER="${NB_GUI_BROWSER:-}"
# $NB_HEADER
#
# Default: '2'
#
# Supported Values: '0' '1' '2' '3'
export NB_HEADER="${NB_HEADER:-2}"
# $NB_IMAGE_TOOL
#
# Default: '' (first available)
#
# Example Values: 'imgcat', 'catimg'
export NB_IMAGE_TOOL="${NB_IMAGE_TOOL:-}"
# $NB_SERVER_HOST
#
# Default: 'localhost'
export NB_SERVER_HOST="${NB_SERVER_HOST:-localhost}"
# $NB_SERVER_PORT
#
# Default: '6789'
export NB_SERVER_PORT="${NB_SERVER_PORT:-6789}"
# $NB_SYNTAX_THEME
#
# Default: 'base16'
#
# Supported Values: Theme names listed with `bat --list-themes`
export NB_SYNTAX_THEME="${NB_SYNTAX_THEME:-base16}"
# $_GIT_ENABLED
#
# Default: '1'
#
# Supported Values: '0' '1'
export _GIT_ENABLED="${_GIT_ENABLED:-1}"
# limit ################################################################# limit
# $NB_LIMIT
#
# Default: '15'
#
# Set to a positive number to limit the output of `nb` and `nls` to that value.
# Set to "auto" to automatically limit output to the current terminal height.
# Subtract an auto limit offset for multiline prompts with `auto-<number>`.
#
# Supported Values:
# - <number>
# - auto-<number>
# - auto-<number>
#
# Example Values:
# - 15
# - auto
# - auto-2
export _AUTO_LIMIT=
export _AUTO_LIMIT_MAX=
case "${NB_LIMIT:-}" in
auto*)
_AUTO_LIMIT="${NB_LIMIT#auto}"
_AUTO_LIMIT="${_AUTO_LIMIT%^*}"
_AUTO_LIMIT="${_AUTO_LIMIT:-0}"
if [[ "${NB_LIMIT:-}" =~ \^ ]]
then
_AUTO_LIMIT_MAX="${NB_LIMIT#*^}"
fi
export NB_LIMIT="${NB_LIMIT}"
;;
*)
export _AUTO_LIMIT=
export NB_LIMIT="${NB_LIMIT:-15}"
;;
esac
# $EDITOR ############################################################# $EDITOR
# $NB_EDITOR
#
# Default: value of $EDITOR or $VISUAL
#
# Example Values: 'vim', `code`'
NB_EDITOR="${NB_EDITOR:-${EDITOR:-${VISUAL:-}}}"
__set_editor() {
local _editors=(
code
subl
hx
micro
mate
macdown
nano
pico
vim
vi
emacs
)
if [[ -z "${NB_EDITOR:-}" ]]
then
local __editor=
for __editor in "${_editors[@]}"
do
if hash "${__editor}" 2>/dev/null
then
NB_EDITOR="${__editor}"
break
fi
done
if [[ -z "${NB_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
- Helix
https://helix-editor.com
- micro
https://github.com/zyedidia/micro
- 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
# Indicators ####################################################### Indicators
# $NB_INDICATOR_<type>
#
# Description:
# Reassign or turn off file type indicator icons.
#
# Examples:
# # reassign pinned indicator
# export NB_INDICATOR_PINNED="💖"
#
# # turn off bookmark indicators
# export NB_INDICATOR_BOOKMARK=""
#
# Parameter Expansion:
# | | set / not blank | set / blank | unset |
# ----------------------------------------------------
# | - | parameter | blank | replacement |
# | :+ | replacement | blank | blank |
# $NB_INDICATOR_AUDIO
export NB_INDICATOR_AUDIO="${NB_INDICATOR_AUDIO-🔉}"
export _NB_INDICATOR_AUDIO="${NB_INDICATOR_AUDIO:+"${NB_INDICATOR_AUDIO} "}"
# $NB_INDICATOR_BOOKMARK
export NB_INDICATOR_BOOKMARK="${NB_INDICATOR_BOOKMARK-🔖}"
export _NB_INDICATOR_BOOKMARK="${NB_INDICATOR_BOOKMARK:+"${NB_INDICATOR_BOOKMARK} "}"
# $NB_INDICATOR_DOCUMENT
export NB_INDICATOR_DOCUMENT="${NB_INDICATOR_DOCUMENT-📄}"
export _NB_INDICATOR_DOCUMENT="${NB_INDICATOR_DOCUMENT:+"${NB_INDICATOR_DOCUMENT} "}"
# $NB_INDICATOR_EBOOK
export NB_INDICATOR_EBOOK="${NB_INDICATOR_EBOOK-📖}"
export _NB_INDICATOR_EBOOK="${NB_INDICATOR_EBOOK:+"${NB_INDICATOR_EBOOK} "}"
# $NB_INDICATOR_ENCRYPTED
export NB_INDICATOR_ENCRYPTED="${NB_INDICATOR_ENCRYPTED-🔒}"
export _NB_INDICATOR_ENCRYPTED="${NB_INDICATOR_ENCRYPTED:+"${NB_INDICATOR_ENCRYPTED} "}"
# $NB_INDICATOR_FOLDER
export NB_INDICATOR_FOLDER="${NB_INDICATOR_FOLDER-📂}"
export _NB_INDICATOR_FOLDER="${NB_INDICATOR_FOLDER:+"${NB_INDICATOR_FOLDER} "}"
# $NB_INDICATOR_IMAGE
export NB_INDICATOR_IMAGE="${NB_INDICATOR_IMAGE-🌄}"
export _NB_INDICATOR_IMAGE="${NB_INDICATOR_IMAGE:+"${NB_INDICATOR_IMAGE} "}"
# $NB_INDICATOR_PINNED
export NB_INDICATOR_PINNED="${NB_INDICATOR_PINNED-📌}"
export _NB_INDICATOR_PINNED="${NB_INDICATOR_PINNED:+"${NB_INDICATOR_PINNED} "}"
# $NB_INDICATOR_TODO
export NB_INDICATOR_TODO="${NB_INDICATOR_TODO-✔️ }"
export _NB_INDICATOR_TODO="${NB_INDICATOR_TODO:+"${NB_INDICATOR_TODO} "}"
# $NB_INDICATOR_TODO_DONE
export NB_INDICATOR_TODO_DONE="${NB_INDICATOR_TODO_DONE-✅}"
export _NB_INDICATOR_TODO_DONE="${NB_INDICATOR_TODO_DONE:+"${NB_INDICATOR_TODO_DONE} "}"
# $NB_INDICATOR_VIDEO
export NB_INDICATOR_VIDEO="${NB_INDICATOR_VIDEO-📹}"
export _NB_INDICATOR_VIDEO="${NB_INDICATOR_VIDEO:+"${NB_INDICATOR_VIDEO} "}"
# 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:-62}"
;;
forest)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-29}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-59}"
;;
lavender)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-183}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-61}"
;;
mage)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-199}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-55}"
;;
mint)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-43}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-60}"
;;
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}"
;;
smoke|monochrome)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-248}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-241}"
;;
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}"
;;
1)
export NB_COLOR_PRIMARY="${NB_COLOR_PRIMARY:-164}"
export NB_COLOR_SECONDARY="${NB_COLOR_SECONDARY:-60}"
;;
esac
export _NB_COLOR_THEMES=(
blacklight
console
desert
electro
forest
nb
ocean
raspberry
smoke
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 statement
# 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
local __file=
for __file in "${NB_DIR}/.plugins"/*."${_ME}"-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_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_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 Variables
#
# Avoid multiple tput calls by assigning output to variables.
export _TPUT_SGR0=
if [[ -n "${TMUX:-}" ]]
then
# Avoid `tmux` and `less` quirks: https://unix.stackexchange.com/a/446541
_TPUT_SGR0="$(printf '\033[m')"
else
_TPUT_SGR0="$(_tput sgr0)"
fi
export _TPUT_COLOR_PRIMARY=
_TPUT_COLOR_PRIMARY="$(_tput setaf "${NB_COLOR_PRIMARY}")"
export _TPUT_COLOR_SECONDARY=
_TPUT_COLOR_SECONDARY="$(_tput setaf "${NB_COLOR_SECONDARY}")"
export _TPUT_SETAF_8=
_TPUT_SETAF_8="$(_tput setaf 8)"
export _TPUT_SMUL=
_TPUT_SMUL="$(_tput smul)"
# _color_brackets()
#
# Usage:
# _color_brackets <string>
#
# Description:
# Print <string> with surrounding color brackets.
_color_brackets() {
if ! ((_COLOR_ENABLED))
then
printf "[%s]" "${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_muted()
#
# Usage:
# _color_muted <string>