m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/ch
blob: e989716a48a2e3ebb4b9972581914fa9cfdf447d (plain)
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
#!/bin/bash

# Helper script for my cheatsheets system.
# USAGE: ch
# This will open a fzf menu over the `~/cheatsheets` directory.
# You can select one of the files there, or use the special command `new` which
# creates a new file (you will be prompted for a title, which should not include
# the .md extension).
# NOTE: Currently hardcodes cheatsheets directory to `~/cheatsheets`.

prefix=~/cheatsheets/
suffix=.md
sheets=("$prefix"*"$suffix")
sheets=("${sheets[@]#"$prefix"}")
sheets=("${sheets[@]%"$suffix"}")
sheets=("${sheets[@]}" new)

sheet=$(printf '%s\n' "${sheets[@]}" | fzf)

if [[ "$sheet" == "new" ]]; then
    echo -n "enter new cheatsheet name: "
    read new_sheet
    file="${prefix}${new_sheet}${suffix}"
    $VISUAL $file
    exit 0
fi

file="${prefix}${sheet}${suffix}"

[[ -f $file ]] && $VISUAL $file