Skip to content

Commit

Permalink
Update snotes
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Aug 25, 2013
1 parent d501907 commit bda5f4a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .snotes/config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ SNOTES_CLEANUP="yes"
# files you don't want to be cleaned
SNOTES_DONTCLEAN_REGEXP='^.*\.gpg$|^.*\.asc$'

# which char should be used to prefix additional options ('changed', etc.)
SNOTES_OPTION_CHAR='!'

# how many commits should be shown for "changed:note"
SNOTES_CHANGED_COMMITS=2
23 changes: 13 additions & 10 deletions bin/snotes
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ done

if [ ! -d "$HOME/.snotes" ]; then
info "creating folder ~/.snotes"
mkdir $HOME/.snotes || die "Could not create $HOME/.snotes"
mkdir "$HOME/.snotes" || die "Could not create $HOME/.snotes"
fi

if [ ! -f "$HOME/.snotes/config" ]; then
info "creating config file ~/.snotes/cofig"
cat > $HOME/.snotes/config <<'EOF'
cat > "$HOME/.snotes/config" <<'EOF'
SNOTES_EDITOR="xterm -e ${VISUAL:-${EDITOR:-vi}}"
SNOTES_DB="$HOME/.snotes/notes"
Expand All @@ -43,6 +43,9 @@ SNOTES_CLEANUP="yes"
#SNOTES_DONTCLEAN_REGEXP='^.*\.gpg$|^.*\.asc$'
SNOTES_DONTCLEAN_REGEXP='^$'
# which char should be used to prefix additional options ('changed', etc.)
SNOTES_OPTION_CHAR='!'
# how many commits should be shown for "changed:note"
SNOTES_CHANGED_COMMITS=2
EOF
Expand Down Expand Up @@ -92,10 +95,10 @@ adding keywords to the individual notes.
You might have noticed snotes' git dependency. All your notes
and edits are stored in a git repository. This allows you to keep
track of your changes easily. For example, if you add "/changed" or "/c"
track of your changes easily. For example, if you add "!changed" or "!c"
after the name of a note in `snotes` you want to look at, snotes will present you
with a diff of recent changes you applied to that specific note.
Adding "/blame" or "/b" after the name of a note will on the other hand
Adding "!blame" or "!b" after the name of a note will on the other hand
provide you with a detailed `git blame` of that note. Technically, the git
repository also makes it quite easy to synchronize your notes from different
platforms.
Expand Down Expand Up @@ -131,23 +134,23 @@ fi
cd "$SNOTES_DB"

if [ "${SNOTES_SEARCH}" = "yes" ]; then
regex=`echo | dmenu ${DMENU_ARGS}`
regex="$(echo | dmenu ${DMENU_ARGS})"
[ -n "$regex" ] || exit 0 # no regex means we don't need to search
select="`(grep -sil $regex *; find . -path ./.git -prune -o -regex ".*$regex.*" -exec basename \{\} \; ) | sort -u | dmenu ${DMENU_ARGS}`"
select="$( (grep -risl $regex *; find . \( -path ./.git -prune -o -regex ".*$regex.*" \) -a -type f | sed -e 's:^\./::' ) | sort -u | dmenu ${DMENU_ARGS})"
else
select=`ls | dmenu ${DMENU_ARGS}`
select="$(find . \( -path ./.git -prune -o -true \) -a -type f | sed -e 's:^\./::' | sort | dmenu ${DMENU_ARGS})"
fi

if [ -z "$select" ]; then
exit 0
fi

case "${select##*/}" in
case "${select##*${SNOTES_OPTION_CHAR}}" in
blame|b)
file="blame:${select%/*}"
file="blame:${select%${SNOTES_OPTION_CHAR}*}"
;;
changed|c)
file="changed:${select%/*}"
file="changed:${select%${SNOTES_OPTION_CHAR}*}"
;;
*)
file="note:${select}"
Expand Down
44 changes: 28 additions & 16 deletions bin/snotes-open
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ die(){

newtemp(){
local tmp
tmp="`mktemp -t snotes.tmp.XXXXXX`"
tmp="$(mktemp -t snotes.tmp.XXXXXX)"
[ -e "$tmp" ] || die "Could not create temporary file"
echo $tmp
echo "$tmp"
}

cancreate(){
[ ! -e "$1" -o -f "$1" ] && (touch "$1" && rm -f "$1") > /dev/null 2>&1
}

validnote(){
echo $1 | grep -q -E -v \
-e "^/" \
-e "(^|/)\.\./" \
-e "(^|/)\./" \
-e "^~[^/]*/" \
-e "^\.git"
}

type git >/dev/null 2>&1 || die "git not in PATH"

. "$HOME/.snotes/config" || die "config ~/.snotes/config messed up"
Expand All @@ -40,7 +49,7 @@ unset snotes_file_flags

blame(){
local tmp
tmp="`newtemp`"
tmp="$(newtemp)"
git blame --relative-date -- "$1" | sed 's/^\^\?/commit:/' > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
Expand All @@ -49,7 +58,7 @@ blame(){

commit(){
local tmp
tmp="`newtemp`"
tmp="$(newtemp)"
git show --color=never "$1" > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
Expand All @@ -59,9 +68,9 @@ commit(){
changed(){
local tmp
local diffcommit
tmp="`newtemp`"
tmp="$(newtemp)"

diffcommit="`git log --oneline --color=never --max-count=${SNOTES_CHANGED_COMMITS} -- \"$1\" | tail -n1 | cut -d' ' -f1`"
diffcommit="$(git log --oneline --color=never --max-count=${SNOTES_CHANGED_COMMITS} -- "$1" | tail -n1 | cut -d' ' -f1)"
git diff --color=never "${diffcommit}^" -- "$1" > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
Expand All @@ -70,7 +79,7 @@ changed(){

cleanup(){
local tmp
tmp=`newtemp`
tmp="$(newtemp)"

# cleanup
cat "$1" | perl -e '
Expand All @@ -88,25 +97,28 @@ do {
rm "$tmp"
}

[ -n "${1##*:}" ] || die "command with an empty argument"
snotes_file="${1##*:}"

[ -n "$snotes_file" ] || die "command with an empty argument"
validnote "$snotes_file" || die "note name is invalid. please don't try anything fancy"


case "${1%:*}" in
note)
snotes_file="${1##*:}"
if [ ! -f "$snotes_file" ]; then
cancreate "$snotes_file" && snotes_file_flags=created || die "could not create file '`pwd`/$snotes_file'"
cancreate "$snotes_file" && snotes_file_flags=created || die "could not create file '$(pwd)/$snotes_file'"
fi
;;
blame)
[ -f "${1##*:}" ] || die "'`pwd`/${1##*:}' does not exist"
blame "${1##*:}"
[ -f "$snotes_file" ] || die "'$(pwd)/$snotes_file' does not exist"
blame "$snotes_file"
;;
commit)
commit "${1##*:}"
commit "$snotes_file"
;;
changed)
[ -f "${1##*:}" ] || die "'`pwd`/${1##*:}' does not exist"
changed "${1##*:}"
[ -f "$snotes_file" ] || die "'$(pwd)/$snotes_file' does not exist"
changed "$snotes_file"
;;
*)
die "unknown prefix '${1%:*}'"
Expand All @@ -116,7 +128,7 @@ esac
${SNOTES_EDITOR} "$snotes_file"

if [ "${SNOTES_CLEANUP}" = "yes" -a "$snotes_file_flags" != "temp" -a -f "$snotes_file" \
-a $(echo "$snotes_file" | grep -E -c "$SNOTES_DONTCLEAN_REGEXP") -eq 0 ]; then
-a "$(echo "$snotes_file" | grep -E -c "$SNOTES_DONTCLEAN_REGEXP")" -eq 0 ]; then
cleanup "$snotes_file"
fi

Expand Down

0 comments on commit bda5f4a

Please sign in to comment.