-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronocode
executable file
·159 lines (122 loc) · 3.48 KB
/
chronocode
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# ------------------------------------------------------------------------------
# settings settings
# ------------------------------------------------------------------------------
set -o errtrace # any trap on ERR is inherited by shell functions,
# command substitutions, and commands executed in a subshell
# environment
set -o nounset # treat unset variables and parameters other than the special
# parameters "@" and "*" as an error when performing
# parameter expansion
set -o pipefail # the return value of a pipeline is the value of the last
# (rightmost) command to exit with a non-zero status, or
# zero if all commands in the pipeline exit successfully
# set language
export LANG="en_US.UTF-8"
# use dot as decimal separator
export LC_NUMERIC="en_US.UTF-8"
# terminal colors
readonly NONE=$(tput sgr0)
readonly RED=$(tput setaf 1)
readonly GREEN=$(tput setaf 2)
readonly YELLOW=$(tput setaf 3)
readonly BLUE=$(tput setaf 4)
readonly MAGENTA=$(tput setaf 5)
readonly CYAN=$(tput setaf 6)
readonly WHITE=$(tput setaf 7)
# ------------------------------------------------------------------------------
# functions functions
# ------------------------------------------------------------------------------
# -------
# failmsg
# -------
#
# This function prints a red colored message via stderr.
#
function failmsg()
{
# push to stderr
echo -e "${RED}[FAIL]${NONE} $1" 1>&2
}
# -------
# warnmsg
# -------
#
# This function prints a yellow colored message via stderr.
#
function warnmsg()
{
# push to stderr
echo -e "${YELLOW}[WARN]${NONE} $1" 1>&2
}
# -------
# infomsg
# -------
#
# This function prints a blue colored message via stderr.
#
function infomsg()
{
# push to stderr
echo -e "${BLUE}[INFO]${NONE} $1" 1>&2
}
# -------
# donemsg
# -------
#
# This function prints a green colored message via stderr.
#
function donemsg()
{
# push to stderr
echo -e "${GREEN}[DONE]${NONE} $1" 1>&2
}
# ------------------------------------------------------------------------------
# commands commands
# ------------------------------------------------------------------------------
# get output directory
readonly TARGETDIR=$(chronocfg -d 2>"/dev/null")
# check return value
if [ -z "$TARGETDIR" ] ; then
# notify user
failmsg "no output directory given"
# signalize trouble
exit 1
fi
# check directory
if [ ! -d "$TARGETDIR" ] ; then
# try to create target directory
mkdir --parents "$TARGETDIR"
# check if target directory is still missing
if [ ! -d "$TARGETDIR" ] ; then
# notify user
failmsg "unable to create directory: \"$TARGETDIR\""
# signalize trouble
exit 1
fi
fi
# get final filename first (timestamp)
readonly OUTFILE="$(chrononext "$TARGETDIR").code"
# insert some empty lines
echo -e "\n\n\n" > "$OUTFILE"
# query user
if ! chronoquery "$OUTFILE" ; then
# signalize trouble
exit 1
fi
# check filesize
if [ -s "$OUTFILE" ] ; then
# show created filename
donemsg "file created: $(basename "$OUTFILE")"
# show created filename
chronosend "file created: $(basename "$OUTFILE")"
else
# remove empty file
rm -f "$OUTFILE"
# notify user
failmsg "no text found"
# signalize trouble
exit 1
fi
# signalize success
exit 0