diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2019-02-18 18:01:37 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2019-02-18 18:01:37 -0500 |
commit | 019f0dfd8d8b3a77552a39ddf83d0f4fc6e8b2f8 (patch) | |
tree | 87b134b09e6fb572f88c8486bbb516e857516844 /fi-progress | |
parent | a0571345b9a2e8f0d13d30981581e3643eff7d21 (diff) |
combined progress tracking
Diffstat (limited to 'fi-progress')
-rw-r--r-- | fi-progress/progress.go | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/fi-progress/progress.go b/fi-progress/progress.go new file mode 100644 index 0000000..a49923f --- /dev/null +++ b/fi-progress/progress.go @@ -0,0 +1,137 @@ +// Copyright 2019 Luke Shumaker <lukeshu@parabola.nu> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +package main + +import ( + "fmt" + "os" + "regexp" + + "git.lukeshu.com/go/libfastimport" + + "git.parabola.nu/~lukeshu/fastimport-go-utils/fiutil" +) + +func usage() { + fmt.Printf("Usage: %s REGEX1 [REGEX2...]\n", os.Args[0]) + os.Exit(0) +} + +func main() { + if len(os.Args) < 2 { + fiutil.ErrUsage("must give at least 1 regexp") + } + + frontend := libfastimport.NewFrontend(os.Stdin, os.Stdin, nil) + backend := libfastimport.NewBackend(os.Stdout, os.Stdout, nil) + + filter := &Progress{ + backend: backend, + } + for _, str := range os.Args[1:] { + if str == "--help" { + usage() + } + re, err := regexp.Compile(str) + if err != nil { + fiutil.ErrUsage(fmt.Sprintf("bad regexp: %q: %v", str, err)) + } + filter.filters = append(filter.filters, re) + } + + filter.start() + err := fiutil.RunHandler(frontend, filter) + filter.end() + if err != nil { + fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err) + os.Exit(1) + } +} + +type Progress struct { + backend *libfastimport.Backend + filters []*regexp.Regexp + curLine int +} + +func (h *Progress) start() { + for _ = range h.filters { + fmt.Fprintln(os.Stderr, "progress ") + h.curLine++ + } +} + +func (h *Progress) end() { + if h.curLine == len(h.filters) { + return + } + fmt.Fprintf(os.Stderr, "\r\x1B[%dB", len(h.filters)-h.curLine) +} + +func (h *Progress) CmdProgress(cmd libfastimport.CmdProgress) error { + for i := range h.filters { + if h.filters[i].MatchString(cmd.Str) { + d := i - h.curLine + var prefix string + switch { + case d < 0: + prefix = fmt.Sprintf("\x1B[%dA", -d) + case d == 0: + prefix = "" + case d > 0: + prefix = fmt.Sprintf("\x1B[%dB", d) + } + fmt.Fprintf(os.Stderr, "\r%sprogress %s\x1B[0K", prefix, cmd.Str) + h.curLine = i + return nil + } + } + return h.backend.Do(cmd) +} + +// pass through everything else +func (h *Progress) CmdBlob(cmd libfastimport.CmdBlob) error { return h.backend.Do(cmd) } +func (h *Progress) CmdCheckpoint(cmd libfastimport.CmdCheckpoint) error { return h.backend.Do(cmd) } +func (h *Progress) CmdComment(cmd libfastimport.CmdComment) error { return h.backend.Do(cmd) } +func (h *Progress) CmdCommit(cmd libfastimport.CmdCommit) error { return h.backend.Do(cmd) } +func (h *Progress) CmdCommitEnd(cmd libfastimport.CmdCommitEnd) error { return h.backend.Do(cmd) } +func (h *Progress) CmdDone(cmd libfastimport.CmdDone) error { return h.backend.Do(cmd) } +func (h *Progress) CmdFeature(cmd libfastimport.CmdFeature) error { return h.backend.Do(cmd) } +func (h *Progress) CmdOption(cmd libfastimport.CmdOption) error { return h.backend.Do(cmd) } +func (h *Progress) CmdReset(cmd libfastimport.CmdReset) error { return h.backend.Do(cmd) } +func (h *Progress) CmdTag(cmd libfastimport.CmdTag) error { return h.backend.Do(cmd) } +func (h *Progress) FileCopy(cmd libfastimport.FileCopy) error { return h.backend.Do(cmd) } +func (h *Progress) FileDelete(cmd libfastimport.FileDelete) error { return h.backend.Do(cmd) } +func (h *Progress) FileDeleteAll(cmd libfastimport.FileDeleteAll) error { return h.backend.Do(cmd) } +func (h *Progress) FileModify(cmd libfastimport.FileModify) error { return h.backend.Do(cmd) } +func (h *Progress) FileModifyInline(cmd libfastimport.FileModifyInline) error { + return h.backend.Do(cmd) +} +func (h *Progress) FileRename(cmd libfastimport.FileRename) error { return h.backend.Do(cmd) } +func (h *Progress) NoteModify(cmd libfastimport.NoteModify) error { return h.backend.Do(cmd) } +func (h *Progress) NoteModifyInline(cmd libfastimport.NoteModifyInline) error { + return h.backend.Do(cmd) +} + +func (h *Progress) CmdCatBlob(cmd libfastimport.CmdCatBlob) (sha1 string, data string, err error) { + return h.backend.CatBlob(cmd) +} +func (h *Progress) CmdGetMark(cmd libfastimport.CmdGetMark) (sha1 string, err error) { + return h.backend.GetMark(cmd) +} +func (h *Progress) CmdLs(cmd libfastimport.CmdLs) (mode libfastimport.Mode, dataref string, path libfastimport.Path, err error) { + return h.backend.Ls(cmd) +} |