-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
HACKING.txt
128 lines (93 loc) · 3.89 KB
/
HACKING.txt
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
Style guidelines
===============
Indentation
-----------
Code must be indented using hard tabs.
Naming
------
Short version: $GLOBAL, $local, func_name()
Variables must be declared and scoped.
GLOBAL_VARIABLES # are uppercase unless there's a good reason not to.
# (e.g., path as a special meaning in Zsh)
local samplevar # are lowercase and scoped to the function # name
# should be readable. Do not make unnecessary
# shortcuts that would impede others to read fluidly
# Please comment your functions before coding them: it helps
# clear the mind on the objective. If it does too much, you
# probably want to split it. Any reusable code should be
# isolated.
any_function() {}
_internals() {} # Prepend an _ if the function is clearly internal,
# that is, if it's only called within the scope of
# another function.
Sample code:
# Sample public command.
#
# It shows developers how to write readable code.
# Returns 0 on success, or fails
public_command() {
local tombpath="$1" # First argument is the path to the tomb
local orientation="${2:-South}" # Second optional argument
local something is happening
[[ -z $tombpath ]] && {
_failure "Usage public_command tombpath [orientation=South]" }
case $orientation in
(South|North|East|West) break;;
(*)
_failure "orientation must be one of South, North, East, or West."
;;
esac
_check_swap # Ensure the available memory is safe
_plot $tombpath # Set TOMB{PATH,DIR,FILE,NAME}
for is in $TOMBLOOPDEVS; do
[[ -k $is ]] && {
happening+="$is "
} || {
something+="$is "
}
done
_message "You gotta sort your bones."
return 0
}
Reporting to the user
---------------------
There are some nifty messaging functions to use. They all come with
shortcuts that you can use during development or for temporary
messages. The long name is to be used for translatable strings.
They display formatted messages, using colors when available.
DEBUG=1 make the _verbose messages visible.
QUIET=1 suppresses all messages (but the _verbose messages if DEBUG=1).
Here is the deal:
Name (Shortcut) Return When to use
=================================================================================
_verbose (xxx) You need to check the current state of the program.
_message (say) You want to tell the user about what's going on.
You can pass -n (shortcut: act) for inline messages.
_warning (no) You want to inform the user about an error condition.
_success (yes) You want to tell the user about a successful result.
_failure (die) exit 1 You want to exit the program with a fatal error.
You may pass a different exit code as exitval.
All messaging function take a single "message" argument.
_failure takes an exit code as an optional exitval environment variable.
Additionally you can use _print to pass translatable string without decoration.
Examples:
_verbose "Showing translatable debug message"
xxx "This is temporary debug"
_message "The program is doing something"
_message -n "Inline messages "
echo "are useful"
_success "All is fine"
_warning "Something's wrong"
_print "Did I really say that?"
_failure "Fatal error: exiting with default exit code 1"
_message "This is not reached, nor the next 'die' command"
exitval=127 die "I am Jack's dead corpse."
Will display something like:
tomb [D] Showing translatable debug message
tomb [D] This is temporary debug
tomb . The program is doing something
tomb > Inline messages are useful
tomb (*) All is fine
tomb [W] Something's wrong
Did I really say that?
tomb [E] Fatal error: exiting with default exit code 1