Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invoke default file manager when notes o is invoked. #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if [ -z "$EDITOR" ] && type editor &>/dev/null; then
EDITOR=editor
fi


without_notes_dir() {
cat | sed -e "s/^$escaped_notes_dir//g" | sed -E "s/^\/+//g"
}
Expand Down Expand Up @@ -51,22 +52,22 @@ search_filenames_and_contents() {
if [ "$#" -gt 0 ]; then
find_output=$(find "$notes_dir" -type f -exec bash -c \
"shopt -s nocasematch
grep -il \"$*\" \"{}\" || if [[ \"{}\" =~ \"$*\" ]]; then
echo \"{}\";
fi" \;\
grep -il \"$*\" \"{}\" || if [[ \"{}\" =~ \"$*\" ]]; then
echo \"{}\";
fi" \;\
)
else
find_output=$(find "$notes_dir" -type f)
fi
find_result=$?
formatted_output=$(printf "$find_output" | without_notes_dir)

if [[ $find_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
else
find_output=$(find "$notes_dir" -type f)
fi
find_result=$?
formatted_output=$(printf "$find_output" | without_notes_dir)

if [[ $find_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
}

find_notes() {
Expand Down Expand Up @@ -116,7 +117,7 @@ generate_name() {
new_note() {
local note_name="$*"
if [[ $note_name == "" ]]; then
note_name="$(generate_name)"
note_name="$(generate_name)"
fi
mkdir -p "$(dirname "$notes_dir/$note_name")"

Expand Down Expand Up @@ -175,11 +176,40 @@ get_full_note_path() {
echo "$note_path"
}

file_manager() {
if ! command -v uname > /dev/null; then
echo "failed to detect OS" >&2
exit 1
fi

local OS=`uname -s`
# reference, https://en.wikipedia.org/wiki/Uname
if [[ $OS = Linux ]]; then
echo "xdg-open"
elif [[ $OS = Darwin ]]; then
echo "open"
else
echo "Opening directory is currently not supported in $OS, if you
know a work around. You can open an issue by going to the following
url, https://github.com/pimterry/notes/issues/new " >&2

exit 1
fi

}

open_note() {
local note_path=$1

if [[ -z "$note_path" ]]; then
open "$notes_dir"
fm=$(file_manager)
if ! (command -v $fm > /dev/null); then
echo "No default file manager set." >&2
exit 1
fi

echo "Opening $notes_dir"
$fm "$notes_dir" &>/dev/null # FIX? discarding errors is bad!
return
fi

Expand Down Expand Up @@ -207,7 +237,7 @@ cat_note() {
}

usage() {
cat <<EOF
cat <<EOF
notes is a command line note taking tool.

Usage:
Expand Down