-
-
Save mike-weiner/4f2bf715118c40de9929fd7a4a0a52ed to your computer and use it in GitHub Desktop.
A tool to quickly create a new Markdown file in a given directory and open it with TextMate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| # jots | |
| # | |
| # A tool to quickly create a new Markdown file in a given directory and open it with TextMate. | |
| # Jots will be created with the naming scheme: yyyymmdd-<name>.md | |
| # | |
| # This script was inspired by https://github.com/tobi/try. | |
| # | |
| # Tool requirements: | |
| # - date, find, read, sed | |
| # - fzf https://formulae.brew.sh/formula/fzf | |
| # - mate https://macromates.com/manual/en/using_textmate_from_terminal | |
| # | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| # | |
| # Global variables | |
| # | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| fzf="fzf" | |
| jotsDirectory="$HOME/jots" | |
| mate="mate" | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| # | |
| # Main script | |
| # | |
| # ------------------------------------------------------------------------------------------------------------------ | |
| mkdir -p "$jotsDirectory" | |
| if ! command -v "$fzf" >/dev/null 2>&1; then | |
| echo "ERROR: Required $fzf dependency is missing." | |
| return 1 2>/dev/null | |
| fi | |
| if ! command -v "$mate" >/dev/null 2>&1; then | |
| echo "ERROR: Required $mate dependency is missing." | |
| return 1 2>/dev/null | |
| fi | |
| # Gather list of Markdown files in jotsDirectory | |
| jots=() | |
| while IFS= read -r jot; do | |
| jots+=("$jot") | |
| done < <(find "$jotsDirectory" -maxdepth 1 -type f \( -name "*.md" -o -name "*.md" \)) | |
| # Use fzf to provide fuzzy finder prompt to user for jot selection | |
| result=$(printf "%s\n" "${jots[@]}" | "$fzf" --prompt="jot>: " --print-query --ghost="search...") | |
| # Extract the query and selection | |
| query=$(sed -n 1p <<<"$result") | |
| selection=$(sed -n 2p <<<"$result") | |
| # If the query is empty and selection equals first item, treat it the same as a cancel | |
| if [[ -z "$query" && "$selection" == "${jots[0]}" ]]; then | |
| selection="" | |
| fi | |
| if [[ -z "$selection" ]]; then | |
| # Nothing selected -> create a new jot | |
| read -rp "Enter new jot name: " name | |
| if [[ -z "$name" ]]; then | |
| echo "Cancelled." | |
| exit 1 | |
| fi | |
| # Sanitize and format filename | |
| filename="$(date +%Y%m%d)-${name// /-}.md" | |
| filepath="$jotsDirectory/$filename" | |
| if [[ ! -f "$filepath" ]]; then | |
| # Create file w/ Markdown header | |
| { | |
| echo "# ${name}" | |
| echo "_Created on $(date '+%B %d, %Y')" | |
| echo | |
| } >"$filepath" | |
| fi | |
| else | |
| # Selected an existing file | |
| filepath="$selection" | |
| fi | |
| # Open jot in TextMake | |
| $mate "$filepath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment