Repository navigation
Expand file tree
/
Copy pathfortune-oneline
More file actions
executable file
·126 lines (110 loc) · 3.68 KB
/
Copy pathfortune-oneline
File metadata and controls
executable file
·126 lines (110 loc) · 3.68 KB
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
#!/bin/sh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2021 Jeremy Brubaker <јЬruЬаkе@оrіоnаrtѕ.іо>
#
# abstract: print a one-line fortune. Convert two-line fortunes to one-liners if possible
#
# Based on https://stackoverflow.com/a/36345272/3224379
#
# Defaults {{{1
#
DEFAULT_WIDTH=72
VERSION='2.0'
# Documentation {{{1
#
print_help() (
cat <<EOF
Usage: $0 [option] [cookie_file]
Print a fortune that is a specified number of lines (one by default). Add any
attribution to the last line if possible. Get fortunes from COOKIE_FILE if specified
-a choose from all lists of maxims (fortune(6) option)
-c show the cookie file from which the fortune came (fortune(6) option)
This will print one more line than requested as the cookie file
name is printed one line one by itself
-e consider all fortune files to be of equal size (fortune(6) option,
see man 6 fortune for explanation)
-l LINES get a fortune that is LINES long (Default: 1)
-w WIDTH use WIDTH as the maximum line width (Default: $DEFAULT_WIDTH)
-v display version info and exit
-h display this help and exit
EOF
) >&2
print_version() (
cat <<EOF
$0 $VERSION
Copyright (C) 2021 Orion Arts
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
EOF
) >&2
# Process options {{{1
#
DEFAULT_WIDTH=72
cookie_file=
width=$DEFAULT_WIDTH
lines=1
opt_a=
opt_e=
while getopts 'acel:w:vh' opt; do
case $opt in
a) opt_a=-a ;;
c) opt_c=-c ;;
e) opt_e=-e ;;
l) lines=$OPTARG ;;
w) width=$OPTARG ;;
v) print_version; exit ;;
h|?) print_help; exit ;;
esac
done
shift $((OPTIND-1))
[ $# -ge 1 ] && cookie_file=$1
# Generate fortune {{{1
#
# Get a fortune until it fits the requirements
until fortune $opt_e $opt_a $opt_c -sn "$width" "$cookie_file" |
awk -v width="$width" -v lines="$lines" -v opt_c=$opt_c '
# If -c set and cookie file name is not set
opt_c != "" && file == "" { file = $0 ; next }
# If -c set and we got the % separator line,
# reset NR to count real lines
opt_c != "" && NR == 2 { NR = 0; next }
# Get the number of lines requested
NR <= lines { L[i++] = $0 }
# If the line after the last requested line is an attribution,
# try to add it to the last requested line
NR == lines + 1 && $1 == "--" {
# Pretend we got the right amount of lines
NR--
# Add attribution to previous line
$1 = " --"
$0 = L[i-1] $0
# If this line is now too long, stop pretending
# we got the right amount of lines
if (length ($0) > width) NR++
# Save the edited (proper length) line
L[i-1] = $0
}
END {
if (file != "") print file
# Fail if we got too many lines
if (NR > lines) exit 1
# Print each line
for (k in L) print L[k]
}
'; do
true
done