-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoop.sh
executable file
·147 lines (131 loc) · 5.72 KB
/
noop.sh
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
## `no`te-lo`op` REPL-ish for my plaintext notes
#
## Inspired by https://github.com/alichtman/fzf-notes
#
## Needed tools:
#
# ripgrep: https://github.com/BurntSushi/ripgrep
#
# fzf: https://github.com/junegunn/fzf
# git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && ~/.fzf/install
#
# preview.sh: part of https://github.com/junegunn/fzf.vim
# curl -sL https://raw.githubusercontent.com/junegunn/fzf.vim/master/bin/preview.sh > ~/bin/preview.sh && chmod +x ~/bin/preview.sh
#
## USAGE
#
# A loop around searching and editing text notes files.
#
# Just run `noop` and start searching for the name or contents of the file you
# want to open or edit. Hit enter to edit any found file. Searching is done
# with `fzf` and `ripgrep`.
#
# Search for `ccc`, and hit enter to be prompted for a new filename, and edit
# it.
#
# Search for `nnn`, and hit enter to open a new timestamped file and edit it.
#
# When you close the editor you're back at the search prompt.
#
# Use Ctrl-c to exit the search view. I just leave it running in a terminal on
# each computer, and share my notes via NextCloud.
if ! command -v rg &> /dev/null
then
echo "Please install ripgrep / rg to use noop"
echo " https://github.com/BurntSushi/ripgrep"
exit 1
fi
if ! command -v fzf &> /dev/null
then
echo "Please install fzf to use noop"
echo " git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && ~/.fzf/install"
exit 1
fi
if ! command -v preview.sh &> /dev/null
then
echo "Please install preview.sh to use noop"
echo " curl -sL https://raw.githubusercontent.com/junegunn/fzf.vim/master/bin/preview.sh > ~/bin/preview.sh && chmod +x ~/bin/preview.sh"
exit 1
fi
# Where should this script look for the notes dir. Must exist.
NOOP_NOTES_DIR="~/Notes"
# Set a default file extension for any new files created.
NOOP_FILE_DEFAULT_EXTENSION="md"
mainloop () {
echo ccc > CREATE_NEW_FILE
echo nnn > NEW_NOTE_NOW
while true; do
set +e # fzf returns 1 for no match, and 130 for Ctrl-c and Escape, so turn off 'e' so we can check later.
SEARCH_RESPONSE="$(rg --passthrough --line-number "" | fzf --preview="preview.sh {}" --preview-window=right:70%:wrap --expect=ctrl-c)"
SEARCH_EXIT_CODE=$?
set -e
# The --expect option to fzf has it returning two lines each time.
# The first line is blank if it's one of the default completion keys
# like enter or escape. Or 'ctrl-c' for Ctrl-c.
# The second line is in <filename>:<line_number>:<line content> format.
OLD_IFS=$IFS
IFS=" "
SEARCH_RESULT=""
while read -r RESPONSE_LINE; do
if [ "$RESPONSE_LINE" = "ctrl-c" ]; then
exit
fi
SEARCH_RESULT="$RESPONSE_LINE"
done < <(echo $SEARCH_RESPONSE)
IFS=$OLD_IFS
if [ -n "$SEARCH_RESULT" ]; then
# Result returned in <filename>:<line number>:<line content>
# format. Capture the filename and line number so we can open to
# the right location in the file.
FILE_TO_EDIT=$(echo $SEARCH_RESULT | awk -F: '{print $1}')
LINE_NUMBER=$(echo $SEARCH_RESULT | awk -F: '{print $2}')
#echo "SEARCH_RESULT=[$SEARCH_RESULT], FILE_TO_EDIT=[$FILE_TO_EDIT], LINE_NUMBER=[$LINE_NUMBER]"; exit;
if [ "$FILE_TO_EDIT" = "CREATE_NEW_FILE" ]; then
read -e -p "Enter a new file name: " FILE_TO_EDIT
if [ -n "$FILE_TO_EDIT" ]; then
FILE_TO_EDIT_EXTENSION="${FILE_TO_EDIT##*.}"
if [ "$FILE_TO_EDIT_EXTENSION" = "$FILE_TO_EDIT" ] && [ -n "$NOOP_FILE_DEFAULT_EXTENSION" ]; then
# TODO: Handle case where a directory name only is entered. We don't want mydir/.md here.
FILE_TO_EDIT="$FILE_TO_EDIT.$NOOP_FILE_DEFAULT_EXTENSION"
fi
mkdir -p "$(dirname "$FILE_TO_EDIT")"
$EDITOR "$FILE_TO_EDIT"
fi
elif [ "$FILE_TO_EDIT" = "NEW_NOTE_NOW" ]; then
FILE_TO_EDIT="$(date +%Y%m%d-%H%M%S)_$(hostname).$NOOP_FILE_DEFAULT_EXTENSION"
$EDITOR "$FILE_TO_EDIT"
else
$EDITOR "$FILE_TO_EDIT" +$LINE_NUMBER
fi
fi
done
}
# Resolve ~/ to $HOME here, so we can work with a notes dir with spaces.
NOOP_NOTES_DIR="$(echo $NOOP_NOTES_DIR | sed "s#^~/#$HOME/#")"
cd "$NOOP_NOTES_DIR"
mainloop
## LICENCE
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, either in source code form or as a compiled binary, for any
# purpose, commercial or non-commercial, and by any means.
#
# In jurisdictions that recognize copyright laws, the author or authors of this
# software dedicate any and all copyright interest in the software to the public
# domain. We make this dedication for the benefit of the public at large and
# to the detriment of our heirs and successors. We intend this dedication to
# be an overt act of relinquishment in perpetuity of all present and future
# rights to this software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
# THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information,
# please refer to <https://unlicense.org/>