6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-12 14:16:18 +00:00

Add output flag and simple tree option

This commit is contained in:
Grzegorz Dlugoszewski
2020-06-08 14:00:59 +02:00
parent f3d0df1bfd
commit e5c3285040
8 changed files with 123 additions and 54 deletions

View File

@@ -3,7 +3,6 @@ package print
import (
"fmt"
"git-get/git"
"path/filepath"
"strings"
)
@@ -11,35 +10,6 @@ type Printer interface {
Print(root string, repos []*git.Repo) string
}
type FlatPrinter struct{}
func NewFlatPrinter() *FlatPrinter {
return &FlatPrinter{}
}
func (p *FlatPrinter) Print(root string, repos []*git.Repo) string {
val := root
for _, repo := range repos {
path := strings.TrimPrefix(repo.Path, root)
path = strings.Trim(path, string(filepath.Separator))
val += fmt.Sprintf("\n%s %s", path, renderWorktreeStatus(repo))
for _, branch := range repo.Status.Branches {
// Don't print the status of the current branch. It was already printed above.
if branch.Name == repo.Status.CurrentBranch {
continue
}
indent := strings.Repeat(" ", len(path))
val += fmt.Sprintf("\n%s %s", indent, renderBranchStatus(branch))
}
}
return val
}
const (
ColorRed = "\033[1;31m%s\033[0m"
ColorGreen = "\033[1;32m%s\033[0m"
@@ -47,7 +17,7 @@ const (
ColorYellow = "\033[1;33m%s\033[0m"
)
func renderWorktreeStatus(repo *git.Repo) string {
func printWorktreeStatus(repo *git.Repo) string {
clean := true
var status []string
@@ -56,7 +26,7 @@ func renderWorktreeStatus(repo *git.Repo) string {
if current := repo.CurrentBranchStatus(); current == nil {
status = append(status, fmt.Sprintf(ColorYellow, repo.Status.CurrentBranch))
} else {
status = append(status, renderBranchStatus(current))
status = append(status, printBranchStatus(current))
}
// TODO: this is ugly
@@ -85,7 +55,7 @@ func renderWorktreeStatus(repo *git.Repo) string {
return strings.Join(status, " ")
}
func renderBranchStatus(branch *git.BranchStatus) string {
func printBranchStatus(branch *git.BranchStatus) string {
// ok indicates that the branch has upstream and is not ahead or behind it
ok := true
var status []string