6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 19:09:45 +00:00
Files
git-get/pkg/print/flat.go
2025-08-24 15:44:22 +02:00

59 lines
1.3 KiB
Go

package print
import (
"fmt"
"os"
"strings"
)
// FlatPrinter prints a list of repos in a flat format.
type FlatPrinter struct{}
// NewFlatPrinter creates a FlatPrinter.
func NewFlatPrinter() *FlatPrinter {
return &FlatPrinter{}
}
// Print generates a flat list of repositories and their statuses - each repo in new line with full path.
func (p *FlatPrinter) Print(repos []Printable) string {
var str strings.Builder
for _, repo := range repos {
str.WriteString(strings.TrimSuffix(repo.Path(), string(os.PathSeparator)))
if len(repo.Errors()) > 0 {
str.WriteString(" " + red("error") + "\n")
continue
}
str.WriteString(" " + blue(repo.Current()))
current := repo.BranchStatus(repo.Current())
worktree := repo.WorkTreeStatus()
if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree)
}
if worktree == "" && current == "" {
str.WriteString(" " + green("ok"))
} else {
str.WriteString(" " + strings.Join([]string{yellow(current), red(worktree)}, " "))
}
for _, branch := range repo.Branches() {
status := repo.BranchStatus(branch)
if status == "" {
status = green("ok")
}
indent := strings.Repeat(" ", len(repo.Path())-1)
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, blue(branch), yellow(status)))
}
str.WriteString("\n")
}
return str.String() + Errors(repos)
}