Commits.to bash script

I’ve created the un*x bash script below to semi-automate making a commits.to link when I notice that I’ve written a promise in a piece of text anywhere.

I copy the text to the clipboad (it can be the whole body of text, not just the promise itself) then run this script. The script reads what’s in the clipboard, parses it to find the actual promise, creates a commits.to URL, shows it to me, then gives me the option of opening the URL in a browser or cancelling. If I choose to open it, the URL is also copied to the clipboard so I can paste it wherever I’d written the text.

The parsing to find the promise inside the text is pretty basic. The script looks for “I will / I shall / I’ll / I should” then copies everything after that up to the first full stop or to the end of the text if no full stop is found.

If anyone improves on it, I’d love to see what you do!

#!/bin/bash

####
#### Customise these settings:
####

#
# Enter your commits.to URL including protocol.
# Don't put a slash at the end.
# E.g., 'http://alice.commits.to'
#
BASE_URL='http://alys.commits.to'

#
# Edit this to contain a command that will PASTE text FROM your clipboard:
#
CLIPBOARD_PASTE_COMMAND='/usr/bin/clipit -c'

#
# Edit this to contain a command that will COPY text from standard input TO your clipboard:
#
CLIPBOARD_COPY_COMMAND='/usr/bin/clipit'

#
# Edit this to contain a command that will open a URL in your default browser:
#
URL_OPEN_COMMAND='/usr/bin/xdg-open'

####
#### End of customisation.
####

# get contents of clipboard:
text=$($CLIPBOARD_PASTE_COMMAND)
# text="These are some words that you can use instead of the above line to test the regex below. I'll do some action by 11pm tomorrow. This sentence is not part of the commitment. Nor this"

# match the first string that starts with "I will" or similar and ends with . or end of string; also remove bad characters:
commitment=$(echo $text | perl -pe "s/.*(I will|I shall|I'll|I should)\s*(.+?)(\.|\!|$).*/\$2/i; s/ +/_/g; s/[^a-zA-Z0-9._-]//g")
url=$BASE_URL/$commitment

# display the URL:
echo
echo "$url"
echo
read -p "Want to make this commitment? [Y/n] " proceed
proceed=${proceed:-Y}

if [ "$proceed" == "Y" ] || [ "$proceed" == "y" ] ; then
	# open the URL in your browser:
	$($URL_OPEN_COMMAND "$url")

	# copy the URL to your clipboard:
	echo "$url" | $($CLIPBOARD_COPY_COMMAND)

	echo
	echo "Great! Copied to clipboard: $url"
else
	echo
	echo "Commitment not made."
fi
4 Likes

I made an emacs command which works in conjunction with this script to make a commitment out of the current line or region:

(defun commit-to-line-or-region ()
  "Commit to something in the current line or region."
(interactive)
(let (pos1 pos2 bds)
  (if (use-region-p)
     (setq pos1 (region-beginning) pos2 (region-end))
    (progn
      (setq bds (bounds-of-thing-at-point 'line))
      (setq pos1 (car bds) pos2 (cdr bds))))

  (copy-region-as-kill pos1 pos2)
  (shell-command "/bin/bash /home/brent/local/mybin/commit &")
  ))
2 Likes

I also improved the script a tiny bit, by replacing the line

text=$($CLIPBOARD_PASTE_COMMAND)

with this:

if [ $# -gt 0 ]
then
    text="$@"
else
    text=$($CLIPBOARD_PASTE_COMMAND)
fi

If any command-line arguments are provided, then it treats those as the text to get a commitment from; otherwise it uses the clipboard as before. This way I can also use it by just typing e.g.

commit Do the thing

on the command line. For example maybe I make a verbal commitment and need to record it quickly. Or maybe (as just happened today) the actual text of the email I wrote said something like “blah blah blah … I’ll get to the bottom of this.” where I didn’t just want to record “get to the bottom of this” as a commitment, nor did I want to reword the email just to make it into the right form.

2 Likes