]> Cypherpunks.ru repositories - goredo.git/blob - status.go
cleanup -dry-run
[goredo.git] / status.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package main
19
20 import (
21         "flag"
22         "fmt"
23         "os"
24 )
25
26 const (
27         EnvStatusFd = "REDO_STATUS_FD"
28         EnvNoStatus = "REDO_NO_STATUS"
29
30         StatusRun = iota
31         StatusDone
32         StatusWait
33         StatusWaited
34 )
35
36 var (
37         FdStatus *os.File
38
39         flagNoStatus = flag.Bool("no-status", false, "disable statusline (REDO_NO_STATUS=1)")
40 )
41
42 func statusInit() {
43         if NoProgress || *flagNoStatus {
44                 return
45         }
46         if v := os.Getenv(EnvNoStatus); v == "1" {
47                 return
48         }
49         if v := os.Getenv(EnvStatusFd); v != "" {
50                 if v == "NO" {
51                         return
52                 }
53                 FdStatus = mustParseFd(v, EnvStatusFd)
54                 return
55         }
56         var r *os.File
57         var err error
58         r, FdStatus, err = os.Pipe()
59         if err != nil {
60                 panic(err)
61         }
62         go func() {
63                 running := 0
64                 waiting := 0
65                 done := 0
66                 var out string
67                 buf := make([]byte, 1)
68                 var n int
69                 for {
70                         n, err = r.Read(buf)
71                         if err != nil || n != 1 {
72                                 break
73                         }
74                         switch buf[0] {
75                         case StatusRun:
76                                 running++
77                         case StatusDone:
78                                 running--
79                                 done++
80                         case StatusWait:
81                                 waiting++
82                         case StatusWaited:
83                                 waiting--
84                         }
85                         if NoColor {
86                                 out = fmt.Sprintf(
87                                         "\rrun: %d wait: %d done: %d",
88                                         running, waiting, done,
89                                 )
90                         } else {
91                                 out = fmt.Sprintf(
92                                         "\rrun: %s%d%s wait: %s%d%s done: %s%d%s",
93                                         CRedo, running, CReset,
94                                         CWait, waiting, CReset,
95                                         CJS, done, CReset,
96                                 )
97                         }
98                         LogMutex.Lock()
99                         os.Stderr.WriteString(fillUpToTermSize(out, "\r"))
100                         LogMutex.Unlock()
101                 }
102         }()
103 }