-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathclip.nb-plugin
125 lines (106 loc) · 3.23 KB
/
clip.nb-plugin
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
#!/usr/bin/env bash
###############################################################################
# clip
#
# A plugin for `nb` providing clipboard functionality.
#
# Author: hyb (https://github.com/ohyhyb)
###############################################################################
# Add the new subcommand names with `_subcommands add <name>`.
_subcommands add "clip"
# Define help and usage text with `_subcommands describe <subcommand> <usage>`.
_subcommands describe "clip" <<HEREDOC
Usage:
nb clip [<notebook>:][<id> | <filename> | <path> | <title> | <extension>]
Description:
Save the clipboard contents and copy contents of text or markdown items to
the clipboard.
When called with no arguments or when no matching file is found, the text
content on the clipboard is saved to a new file, pending a prompt.
Examples:
# copy the content of item 123 to the clipboard
nb clip 123
# save the clipboard contents to a new file with a \`.js\` file extension
nb clip .js
# save the clipboard contents as a new \`.cr\` file in the "snippets" notebook
nb snippets:clip .cr
HEREDOC
# Define the subcommand as a function, named with a leading underscore.
_clip() {
local _force=0
local _selector=
local __arg=
for __arg in "${@:-}"
do
case "${__arg:-}" in
'') : ;;
--force) _force=1 ;;
*) _selector="${__arg}" ;;
esac
done
local _source_relative_path=
if [[ -n "${_selector:-}" ]]
then
_source_relative_path="$(
_show "${_selector:-}" --relative-path 2>/dev/null || :
)"
fi
local _notebook_path=
_notebook_path="$(_notebooks current --path)"
if _command_exists "_color_dim" && ! _command_exists "_color_muted"
then
_color_muted() { _color_dim "${@:-}"; }
fi
if [[ -z "${_source_relative_path:-}" ]] ||
[[ ! -e "${_notebook_path}/${_source_relative_path}" ]]
then
printf "Saving clipboard contents%s\\n" "$(_color_muted "...")"
if ! ((_force))
then
while true
do
local __yn=
IFS='' read -r -e -d $'\n' -p "\
$(_color_primary "Proceed?") $(_color_brackets "y/N") " __yn
case ${__yn} in
[Yy]*)
break
;;
*)
printf "Exiting%s\\n" "$(_color_muted "...")"
exit 0
;;
esac
done
fi
{
if _command_exists "xclip" && [[ ! "${OSTYPE}" =~ ^darwin ]]
then
xclip -o
elif _command_exists "pbpaste"
then
pbpaste
fi
} | {
_add "${_selector}"
}
return 0
elif [[ ! -f "${_notebook_path}/${_source_relative_path}" ]]
then
printf "Not a file: %s\\n" "${_selector}"
exit 1
fi
if _show "${_source_relative_path}" --type text ||
_show "${_source_relative_path}" --type md
then
if _command_exists "xclip" && [[ ! "${OSTYPE}" =~ ^darwin ]]
then
cat "${_notebook_path}/${_source_relative_path}" | xclip -sel clip
elif _command_exists "pbcopy"
then
cat "${_notebook_path}/${_source_relative_path}" | pbcopy
fi && printf "Copied $(_color_primary "${_source_relative_path}") contents to clipboard.%s\\n"
else
_exit_1 printf "Not a text or markdown file.%s\\n"
fi
}