-
Notifications
You must be signed in to change notification settings - Fork 1
/
sotd
executable file
·85 lines (69 loc) · 2.92 KB
/
sotd
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
## Quickly and interactively generate shave of the day boilerplate for /r/wetshaving.
## Output is written to 'archive' directory, tagged with date, e.g. sotd.2021-05-03.
## The output targets the flavor of markdown used on reddit. Optionally can also write data as json.
## This TOTALLY depends on the marvelous fzf utility being installed on your machine.
## See https://github.com/junegunn/fzf#installation for installation details.
## You need fzf in your life. This will not work without fzf, just saying.
## Program was developed and tested on macos. Likely to work on linux as is, but have not tried it.
## Unlikely to work on Windows without changes. Maybe on Windows Linux Subsystem,
## but author does not have a Windows machine. You up for porting it?
##-- Define utility functions.
function push { # append line to the sotd file
printf "%s\n" "$1" >> "${sotd}"
}
function select_from { # Get the user's product choices
choice="$(fzf < "$1")" # This is where the magic happens
markdown="$2"
# User choice of 'none' results in nothing being returned
if [[ "${choice}" != "none" ]]; then
echo "${markdown} ${choice}"
fi
}
# Represent the user choices as json, a little something for the data affionados.
# This is only used if the option to write data as json is (optionally )uncommented
# in the 'USER CHOICES' section, below'
function json(){
printf "%s\n" "{\
\"date\":\"${when}\",\
\"razor\":\"${razor}\",\
\"blade\":\"${blade}\",\
\"lather\":\"${lather}\",\
\"brush\":\"${brush}\",\
\"post\":\"${post}\",\
\"frag\":\"${frag}\"\
}"
}
##-- The utility functions are all defined, let's use them.
# Get the current date.
when="$(date +%Y-%m-%d)"
# Create the name of the today's sotd file.
# Notice the file path is relative to the install dir.
sotd="archive/sotd.${when}"
# Create the empty sotd file.
touch "${sotd}"
# If file already exists due to mulitple runs on same day, wipe it. Allows for easily re-running script,
# say in case of error.
echo "" > "${sotd}"
# Grab the USER CHOICES
razor=$(select_from "./products/razors" "* **Razor:**")
blade=$(select_from "./products/blades" "* **Blade:**")
lather=$(select_from "./products/lathers" "* **Lather:**")
brush=$(select_from "./products/brushes" "* **Brush:**")
post=$(select_from "./products/posts" "* **Post Shave:**")
# Uncomment the line below if you want to include a fragrance choice
frag=$(select_from "./products/frags" "* **Fragrance:**")
# Write the user choices to the sotd file.
for item in "${razor}" "${blade}" "${lather}" "${brush}" "${post}" "${frag}"
do
if [[ "${item}" != "" ]]; then
push "${item}"
fi
done
# To also archive the data as json, uncomment line below.
# json > "${sotd}.json"
# Copy the sotd to the clipboard for enhanced pasting convenience.
# This only works on MacOS. Uncomment if so desired.
# pbcopy < "${sotd}"
# We wrote the sotd to the archive dir, now show it in the console.
cat "${sotd}"