2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 EnvLevel = "REDO_LEVEL"
35 EnvNoProgress = "REDO_NO_PROGRESS"
36 EnvDebug = "REDO_DEBUG"
37 EnvLogWait = "REDO_LOG_WAIT"
38 EnvLogLock = "REDO_LOG_LOCK"
39 EnvLogPid = "REDO_LOG_PID"
40 EnvLogJS = "REDO_LOG_JS"
41 EnvNoColor = "NO_COLOR"
64 flagDebug = flag.Bool("d", false, fmt.Sprintf("enable debug logging (%s=1)", EnvDebug))
77 t := term.NewTerminal(&b, "")
78 CDebug = string(t.Escape.Yellow)
79 CRedo = string(t.Escape.Green)
80 CWait = string(t.Escape.Blue)
81 CLock = string(t.Escape.Cyan)
82 CErr = string(t.Escape.Red)
83 CWarn = string(t.Escape.Magenta)
84 CJS = string(t.Escape.White)
85 CReset = string(t.Escape.Reset)
86 KeyEraseLine = fmt.Sprintf("%s[K", CReset[0:1])
89 if !(cmdName == CmdNameRedo || cmdName == CmdNameRedoIfchange) {
92 flagNoProgress = flag.Bool("no-progress", false,
93 fmt.Sprintf("no progress printing (%s=1), also implies -no-status", EnvNoProgress))
94 flagLogWait = flag.Bool("log-wait", false,
95 fmt.Sprintf("enable wait messages logging (%s=1)", EnvLogWait))
96 flagLogLock = flag.Bool("log-lock", false,
97 fmt.Sprintf("enable lock messages logging (%s=1)", EnvLogLock))
98 flagLogPid = flag.Bool("log-pid", false,
99 fmt.Sprintf("append PIDs (%s=1)", EnvLogPid))
100 flagLogJS = flag.Bool("log-js", false,
101 fmt.Sprintf("enable jobserver messages logging (%s=1)", EnvLogJS))
104 func erasedStatus(s, end string) string {
105 if NoProgress || NoColor {
108 return s + KeyEraseLine + end
111 func tracef(level, format string, args ...interface{}) {
114 p = fmt.Sprintf("[%d] ", MyPid)
118 p = erasedStatus(StderrPrefix+p+fmt.Sprintf(format, args...), "\n")
120 os.Stderr.WriteString(p)
129 if !(LogWait || Debug) {
139 if !(LogLock || Debug) {
144 if !(LogJS || Debug) {
153 msg := fmt.Sprintf(format, args...)
154 msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg)
155 msg = erasedStatus(msg, "\n")
157 os.Stderr.WriteString(msg)
161 func colourize(colour, s string) string {
165 return colour + s + CReset