mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-21 12:16:54 +02:00
standardise rendering of lists in panels
This commit is contained in:
parent
3b765e5417
commit
c00c834b35
@ -14,9 +14,9 @@ type Branch struct {
|
|||||||
Recency string
|
Recency string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisplayString returns the dispaly string of branch
|
// GetDisplayStrings returns the dispaly string of branch
|
||||||
func (b *Branch) GetDisplayString() string {
|
func (b *Branch) GetDisplayStrings() []string {
|
||||||
return utils.WithPadding(b.Recency, 4) + utils.ColoredString(b.Name, b.GetColor())
|
return []string{b.Recency, utils.ColoredString(b.Name, b.GetColor())}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetColor branch color
|
// GetColor branch color
|
||||||
|
26
pkg/commands/commit.go
Normal file
26
pkg/commands/commit.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Commit : A git commit
|
||||||
|
type Commit struct {
|
||||||
|
Sha string
|
||||||
|
Name string
|
||||||
|
Pushed bool
|
||||||
|
DisplayString string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Commit) GetDisplayStrings() []string {
|
||||||
|
red := color.New(color.FgRed)
|
||||||
|
yellow := color.New(color.FgYellow)
|
||||||
|
white := color.New(color.FgWhite)
|
||||||
|
|
||||||
|
shaColor := yellow
|
||||||
|
if c.Pushed {
|
||||||
|
shaColor = red
|
||||||
|
}
|
||||||
|
|
||||||
|
return []string{shaColor.Sprint(c.Sha), white.Sprint(c.Name)}
|
||||||
|
}
|
36
pkg/commands/file.go
Normal file
36
pkg/commands/file.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import "github.com/fatih/color"
|
||||||
|
|
||||||
|
// File : A file from git status
|
||||||
|
// duplicating this for now
|
||||||
|
type File struct {
|
||||||
|
Name string
|
||||||
|
HasStagedChanges bool
|
||||||
|
HasUnstagedChanges bool
|
||||||
|
Tracked bool
|
||||||
|
Deleted bool
|
||||||
|
HasMergeConflicts bool
|
||||||
|
DisplayString string
|
||||||
|
Type string // one of 'file', 'directory', and 'other'
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDisplayStrings returns the display string of a file
|
||||||
|
func (f *File) GetDisplayStrings() []string {
|
||||||
|
// potentially inefficient to be instantiating these color
|
||||||
|
// objects with each render
|
||||||
|
red := color.New(color.FgRed)
|
||||||
|
green := color.New(color.FgGreen)
|
||||||
|
if !f.Tracked && !f.HasStagedChanges {
|
||||||
|
return []string{red.Sprint(f.DisplayString)}
|
||||||
|
}
|
||||||
|
|
||||||
|
output := green.Sprint(f.DisplayString[0:1])
|
||||||
|
output += red.Sprint(f.DisplayString[1:3])
|
||||||
|
if f.HasUnstagedChanges {
|
||||||
|
output += red.Sprint(f.Name)
|
||||||
|
} else {
|
||||||
|
output += green.Sprint(f.Name)
|
||||||
|
}
|
||||||
|
return []string{output}
|
||||||
|
}
|
@ -104,17 +104,17 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetStashEntries stash entryies
|
// GetStashEntries stash entryies
|
||||||
func (c *GitCommand) GetStashEntries() []StashEntry {
|
func (c *GitCommand) GetStashEntries() []*StashEntry {
|
||||||
rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'")
|
rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'")
|
||||||
stashEntries := []StashEntry{}
|
stashEntries := []*StashEntry{}
|
||||||
for i, line := range utils.SplitLines(rawString) {
|
for i, line := range utils.SplitLines(rawString) {
|
||||||
stashEntries = append(stashEntries, stashEntryFromLine(line, i))
|
stashEntries = append(stashEntries, stashEntryFromLine(line, i))
|
||||||
}
|
}
|
||||||
return stashEntries
|
return stashEntries
|
||||||
}
|
}
|
||||||
|
|
||||||
func stashEntryFromLine(line string, index int) StashEntry {
|
func stashEntryFromLine(line string, index int) *StashEntry {
|
||||||
return StashEntry{
|
return &StashEntry{
|
||||||
Name: line,
|
Name: line,
|
||||||
Index: index,
|
Index: index,
|
||||||
DisplayString: line,
|
DisplayString: line,
|
||||||
@ -127,10 +127,10 @@ func (c *GitCommand) GetStashEntryDiff(index int) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetStatusFiles git status files
|
// GetStatusFiles git status files
|
||||||
func (c *GitCommand) GetStatusFiles() []File {
|
func (c *GitCommand) GetStatusFiles() []*File {
|
||||||
statusOutput, _ := c.GitStatus()
|
statusOutput, _ := c.GitStatus()
|
||||||
statusStrings := utils.SplitLines(statusOutput)
|
statusStrings := utils.SplitLines(statusOutput)
|
||||||
files := []File{}
|
files := []*File{}
|
||||||
|
|
||||||
for _, statusString := range statusStrings {
|
for _, statusString := range statusStrings {
|
||||||
change := statusString[0:2]
|
change := statusString[0:2]
|
||||||
@ -140,7 +140,7 @@ func (c *GitCommand) GetStatusFiles() []File {
|
|||||||
_, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change]
|
_, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change]
|
||||||
_, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange]
|
_, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange]
|
||||||
|
|
||||||
file := File{
|
file := &File{
|
||||||
Name: filename,
|
Name: filename,
|
||||||
DisplayString: statusString,
|
DisplayString: statusString,
|
||||||
HasStagedChanges: !hasNoStagedChanges,
|
HasStagedChanges: !hasNoStagedChanges,
|
||||||
@ -168,7 +168,7 @@ func (c *GitCommand) StashSave(message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MergeStatusFiles merge status files
|
// MergeStatusFiles merge status files
|
||||||
func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File {
|
func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File) []*File {
|
||||||
if len(oldFiles) == 0 {
|
if len(oldFiles) == 0 {
|
||||||
return newFiles
|
return newFiles
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File {
|
|||||||
appendedIndexes := []int{}
|
appendedIndexes := []int{}
|
||||||
|
|
||||||
// retain position of files we already could see
|
// retain position of files we already could see
|
||||||
result := []File{}
|
result := []*File{}
|
||||||
for _, oldFile := range oldFiles {
|
for _, oldFile := range oldFiles {
|
||||||
for newIndex, newFile := range newFiles {
|
for newIndex, newFile := range newFiles {
|
||||||
if oldFile.Name == newFile.Name {
|
if oldFile.Name == newFile.Name {
|
||||||
@ -415,7 +415,7 @@ func (c *GitCommand) IsInMergeState() (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveFile directly
|
// RemoveFile directly
|
||||||
func (c *GitCommand) RemoveFile(file File) error {
|
func (c *GitCommand) RemoveFile(file *File) error {
|
||||||
// if the file isn't tracked, we assume you want to delete it
|
// if the file isn't tracked, we assume you want to delete it
|
||||||
if file.HasStagedChanges {
|
if file.HasStagedChanges {
|
||||||
if err := c.OSCommand.RunCommand("git reset -- " + file.Name); err != nil {
|
if err := c.OSCommand.RunCommand("git reset -- " + file.Name); err != nil {
|
||||||
@ -471,17 +471,17 @@ func includesString(list []string, a string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCommits obtains the commits of the current branch
|
// GetCommits obtains the commits of the current branch
|
||||||
func (c *GitCommand) GetCommits() []Commit {
|
func (c *GitCommand) GetCommits() []*Commit {
|
||||||
pushables := c.GetCommitsToPush()
|
pushables := c.GetCommitsToPush()
|
||||||
log := c.GetLog()
|
log := c.GetLog()
|
||||||
commits := []Commit{}
|
commits := []*Commit{}
|
||||||
// now we can split it up and turn it into commits
|
// now we can split it up and turn it into commits
|
||||||
lines := utils.SplitLines(log)
|
lines := utils.SplitLines(log)
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
splitLine := strings.Split(line, " ")
|
splitLine := strings.Split(line, " ")
|
||||||
sha := splitLine[0]
|
sha := splitLine[0]
|
||||||
pushed := includesString(pushables, sha)
|
pushed := includesString(pushables, sha)
|
||||||
commits = append(commits, Commit{
|
commits = append(commits, &Commit{
|
||||||
Sha: sha,
|
Sha: sha,
|
||||||
Name: strings.Join(splitLine[1:], " "),
|
Name: strings.Join(splitLine[1:], " "),
|
||||||
Pushed: pushed,
|
Pushed: pushed,
|
||||||
@ -519,7 +519,7 @@ func (c *GitCommand) Show(sha string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Diff returns the diff of a file
|
// Diff returns the diff of a file
|
||||||
func (c *GitCommand) Diff(file File) string {
|
func (c *GitCommand) Diff(file *File) string {
|
||||||
cachedArg := ""
|
cachedArg := ""
|
||||||
fileName := c.OSCommand.Quote(file.Name)
|
fileName := c.OSCommand.Quote(file.Name)
|
||||||
if file.HasStagedChanges && !file.HasUnstagedChanges {
|
if file.HasStagedChanges && !file.HasUnstagedChanges {
|
||||||
|
@ -1,32 +1,5 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
// File : A staged/unstaged file
|
|
||||||
type File struct {
|
|
||||||
Name string
|
|
||||||
HasStagedChanges bool
|
|
||||||
HasUnstagedChanges bool
|
|
||||||
Tracked bool
|
|
||||||
Deleted bool
|
|
||||||
HasMergeConflicts bool
|
|
||||||
DisplayString string
|
|
||||||
Type string // one of 'file', 'directory', and 'other'
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit : A git commit
|
|
||||||
type Commit struct {
|
|
||||||
Sha string
|
|
||||||
Name string
|
|
||||||
Pushed bool
|
|
||||||
DisplayString string
|
|
||||||
}
|
|
||||||
|
|
||||||
// StashEntry : A git stash entry
|
|
||||||
type StashEntry struct {
|
|
||||||
Index int
|
|
||||||
Name string
|
|
||||||
DisplayString string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Conflict : A git conflict with a start middle and end corresponding to line
|
// Conflict : A git conflict with a start middle and end corresponding to line
|
||||||
// numbers in the file where the conflict bars appear
|
// numbers in the file where the conflict bars appear
|
||||||
type Conflict struct {
|
type Conflict struct {
|
||||||
|
13
pkg/commands/stash_entry.go
Normal file
13
pkg/commands/stash_entry.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
// StashEntry : A git stash entry
|
||||||
|
type StashEntry struct {
|
||||||
|
Index int
|
||||||
|
Name string
|
||||||
|
DisplayString string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDisplayStrings returns the dispaly string of branch
|
||||||
|
func (s *StashEntry) GetDisplayStrings() []string {
|
||||||
|
return []string{s.DisplayString}
|
||||||
|
}
|
@ -34,7 +34,7 @@ func NewBranchListBuilder(log *logrus.Entry, gitCommand *commands.GitCommand) (*
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BranchListBuilder) obtainCurrentBranch() commands.Branch {
|
func (b *BranchListBuilder) obtainCurrentBranch() *commands.Branch {
|
||||||
// I used go-git for this, but that breaks if you've just done a git init,
|
// I used go-git for this, but that breaks if you've just done a git init,
|
||||||
// even though you're on 'master'
|
// even though you're on 'master'
|
||||||
branchName, err := b.GitCommand.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
branchName, err := b.GitCommand.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
||||||
@ -44,11 +44,11 @@ func (b *BranchListBuilder) obtainCurrentBranch() commands.Branch {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return commands.Branch{Name: strings.TrimSpace(branchName), Recency: " *"}
|
return &commands.Branch{Name: strings.TrimSpace(branchName), Recency: " *"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BranchListBuilder) obtainReflogBranches() []commands.Branch {
|
func (b *BranchListBuilder) obtainReflogBranches() []*commands.Branch {
|
||||||
branches := make([]commands.Branch, 0)
|
branches := make([]*commands.Branch, 0)
|
||||||
rawString, err := b.GitCommand.OSCommand.RunCommandWithOutput("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD")
|
rawString, err := b.GitCommand.OSCommand.RunCommandWithOutput("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return branches
|
return branches
|
||||||
@ -58,14 +58,14 @@ func (b *BranchListBuilder) obtainReflogBranches() []commands.Branch {
|
|||||||
for _, line := range branchLines {
|
for _, line := range branchLines {
|
||||||
timeNumber, timeUnit, branchName := branchInfoFromLine(line)
|
timeNumber, timeUnit, branchName := branchInfoFromLine(line)
|
||||||
timeUnit = abbreviatedTimeUnit(timeUnit)
|
timeUnit = abbreviatedTimeUnit(timeUnit)
|
||||||
branch := commands.Branch{Name: branchName, Recency: timeNumber + timeUnit}
|
branch := &commands.Branch{Name: branchName, Recency: timeNumber + timeUnit}
|
||||||
branches = append(branches, branch)
|
branches = append(branches, branch)
|
||||||
}
|
}
|
||||||
return branches
|
return branches
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BranchListBuilder) obtainSafeBranches() []commands.Branch {
|
func (b *BranchListBuilder) obtainSafeBranches() []*commands.Branch {
|
||||||
branches := make([]commands.Branch, 0)
|
branches := make([]*commands.Branch, 0)
|
||||||
|
|
||||||
bIter, err := b.GitCommand.Repo.Branches()
|
bIter, err := b.GitCommand.Repo.Branches()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -73,14 +73,14 @@ func (b *BranchListBuilder) obtainSafeBranches() []commands.Branch {
|
|||||||
}
|
}
|
||||||
err = bIter.ForEach(func(b *plumbing.Reference) error {
|
err = bIter.ForEach(func(b *plumbing.Reference) error {
|
||||||
name := b.Name().Short()
|
name := b.Name().Short()
|
||||||
branches = append(branches, commands.Branch{Name: name})
|
branches = append(branches, &commands.Branch{Name: name})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return branches
|
return branches
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BranchListBuilder) appendNewBranches(finalBranches, newBranches, existingBranches []commands.Branch, included bool) []commands.Branch {
|
func (b *BranchListBuilder) appendNewBranches(finalBranches, newBranches, existingBranches []*commands.Branch, included bool) []*commands.Branch {
|
||||||
for _, newBranch := range newBranches {
|
for _, newBranch := range newBranches {
|
||||||
if included == branchIncluded(newBranch.Name, existingBranches) {
|
if included == branchIncluded(newBranch.Name, existingBranches) {
|
||||||
finalBranches = append(finalBranches, newBranch)
|
finalBranches = append(finalBranches, newBranch)
|
||||||
@ -89,7 +89,7 @@ func (b *BranchListBuilder) appendNewBranches(finalBranches, newBranches, existi
|
|||||||
return finalBranches
|
return finalBranches
|
||||||
}
|
}
|
||||||
|
|
||||||
func sanitisedReflogName(reflogBranch commands.Branch, safeBranches []commands.Branch) string {
|
func sanitisedReflogName(reflogBranch *commands.Branch, safeBranches []*commands.Branch) string {
|
||||||
for _, safeBranch := range safeBranches {
|
for _, safeBranch := range safeBranches {
|
||||||
if strings.ToLower(safeBranch.Name) == strings.ToLower(reflogBranch.Name) {
|
if strings.ToLower(safeBranch.Name) == strings.ToLower(reflogBranch.Name) {
|
||||||
return safeBranch.Name
|
return safeBranch.Name
|
||||||
@ -99,15 +99,15 @@ func sanitisedReflogName(reflogBranch commands.Branch, safeBranches []commands.B
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build the list of branches for the current repo
|
// Build the list of branches for the current repo
|
||||||
func (b *BranchListBuilder) Build() []commands.Branch {
|
func (b *BranchListBuilder) Build() []*commands.Branch {
|
||||||
branches := make([]commands.Branch, 0)
|
branches := make([]*commands.Branch, 0)
|
||||||
head := b.obtainCurrentBranch()
|
head := b.obtainCurrentBranch()
|
||||||
safeBranches := b.obtainSafeBranches()
|
safeBranches := b.obtainSafeBranches()
|
||||||
if len(safeBranches) == 0 {
|
if len(safeBranches) == 0 {
|
||||||
return append(branches, head)
|
return append(branches, head)
|
||||||
}
|
}
|
||||||
reflogBranches := b.obtainReflogBranches()
|
reflogBranches := b.obtainReflogBranches()
|
||||||
reflogBranches = uniqueByName(append([]commands.Branch{head}, reflogBranches...))
|
reflogBranches = uniqueByName(append([]*commands.Branch{head}, reflogBranches...))
|
||||||
for i, reflogBranch := range reflogBranches {
|
for i, reflogBranch := range reflogBranches {
|
||||||
reflogBranches[i].Name = sanitisedReflogName(reflogBranch, safeBranches)
|
reflogBranches[i].Name = sanitisedReflogName(reflogBranch, safeBranches)
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ func (b *BranchListBuilder) Build() []commands.Branch {
|
|||||||
return branches
|
return branches
|
||||||
}
|
}
|
||||||
|
|
||||||
func branchIncluded(branchName string, branches []commands.Branch) bool {
|
func branchIncluded(branchName string, branches []*commands.Branch) bool {
|
||||||
for _, existingBranch := range branches {
|
for _, existingBranch := range branches {
|
||||||
if strings.ToLower(existingBranch.Name) == strings.ToLower(branchName) {
|
if strings.ToLower(existingBranch.Name) == strings.ToLower(branchName) {
|
||||||
return true
|
return true
|
||||||
@ -127,8 +127,8 @@ func branchIncluded(branchName string, branches []commands.Branch) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func uniqueByName(branches []commands.Branch) []commands.Branch {
|
func uniqueByName(branches []*commands.Branch) []*commands.Branch {
|
||||||
finalBranches := make([]commands.Branch, 0)
|
finalBranches := make([]*commands.Branch, 0)
|
||||||
for _, branch := range branches {
|
for _, branch := range branches {
|
||||||
if branchIncluded(branch.Name, finalBranches) {
|
if branchIncluded(branch.Name, finalBranches) {
|
||||||
continue
|
continue
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/git"
|
"github.com/jesseduffield/lazygit/pkg/git"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
||||||
@ -109,7 +110,7 @@ func (gui *Gui) handleMerge(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) getSelectedBranch(v *gocui.View) commands.Branch {
|
func (gui *Gui) getSelectedBranch(v *gocui.View) *commands.Branch {
|
||||||
lineNumber := gui.getItemPosition(v)
|
lineNumber := gui.getItemPosition(v)
|
||||||
return gui.State.Branches[lineNumber]
|
return gui.State.Branches[lineNumber]
|
||||||
}
|
}
|
||||||
@ -151,12 +152,15 @@ func (gui *Gui) refreshBranches(g *gocui.Gui) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gui.State.Branches = builder.Build()
|
gui.State.Branches = builder.Build()
|
||||||
|
|
||||||
v.Clear()
|
v.Clear()
|
||||||
displayStrings := make([]string, len(gui.State.Branches))
|
list, err := utils.RenderList(gui.State.Branches)
|
||||||
for i, branch := range gui.State.Branches {
|
if err != nil {
|
||||||
displayStrings[i] = branch.GetDisplayString()
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprint(v, strings.Join(displayStrings, "\n"))
|
|
||||||
|
fmt.Fprint(v, list)
|
||||||
|
|
||||||
gui.resetOrigin(v)
|
gui.resetOrigin(v)
|
||||||
return gui.refreshStatus(g)
|
return gui.refreshStatus(g)
|
||||||
})
|
})
|
||||||
|
@ -3,26 +3,12 @@ package gui
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) renderCommit(commit commands.Commit) string {
|
|
||||||
red := color.New(color.FgRed)
|
|
||||||
yellow := color.New(color.FgYellow)
|
|
||||||
white := color.New(color.FgWhite)
|
|
||||||
|
|
||||||
shaColor := yellow
|
|
||||||
if commit.Pushed {
|
|
||||||
shaColor = red
|
|
||||||
}
|
|
||||||
|
|
||||||
return shaColor.Sprint(commit.Sha) + " " + white.Sprint(commit.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
||||||
g.Update(func(*gocui.Gui) error {
|
g.Update(func(*gocui.Gui) error {
|
||||||
gui.State.Commits = gui.GitCommand.GetCommits()
|
gui.State.Commits = gui.GitCommand.GetCommits()
|
||||||
@ -30,12 +16,14 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.Clear()
|
v.Clear()
|
||||||
displayStrings := make([]string, len(gui.State.Commits))
|
list, err := utils.RenderList(gui.State.Commits)
|
||||||
for i, commit := range gui.State.Commits {
|
if err != nil {
|
||||||
displayStrings[i] = gui.renderCommit(commit)
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprint(v, strings.Join(displayStrings, "\n"))
|
fmt.Fprint(v, list)
|
||||||
|
|
||||||
gui.refreshStatus(g)
|
gui.refreshStatus(g)
|
||||||
if g.CurrentView().Name() == "commits" {
|
if g.CurrentView().Name() == "commits" {
|
||||||
gui.handleCommitSelect(g, v)
|
gui.handleCommitSelect(g, v)
|
||||||
@ -106,7 +94,7 @@ func (gui *Gui) handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move to files panel
|
// TODO: move to files panel
|
||||||
func (gui *Gui) anyUnStagedChanges(files []commands.File) bool {
|
func (gui *Gui) anyUnStagedChanges(files []*commands.File) bool {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.Tracked && file.HasUnstagedChanges {
|
if file.Tracked && file.HasUnstagedChanges {
|
||||||
return true
|
return true
|
||||||
@ -169,13 +157,13 @@ func (gui *Gui) handleRenameCommitEditor(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) getSelectedCommit(g *gocui.Gui) (commands.Commit, error) {
|
func (gui *Gui) getSelectedCommit(g *gocui.Gui) (*commands.Commit, error) {
|
||||||
v, err := g.View("commits")
|
v, err := g.View("commits")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if len(gui.State.Commits) == 0 {
|
if len(gui.State.Commits) == 0 {
|
||||||
return commands.Commit{}, errors.New(gui.Tr.SLocalize("NoCommitsThisBranch"))
|
return &commands.Commit{}, errors.New(gui.Tr.SLocalize("NoCommitsThisBranch"))
|
||||||
}
|
}
|
||||||
lineNumber := gui.getItemPosition(v)
|
lineNumber := gui.getItemPosition(v)
|
||||||
if lineNumber > len(gui.State.Commits)-1 {
|
if lineNumber > len(gui.State.Commits)-1 {
|
||||||
|
@ -10,14 +10,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) stagedFiles() []commands.File {
|
func (gui *Gui) stagedFiles() []*commands.File {
|
||||||
files := gui.State.Files
|
files := gui.State.Files
|
||||||
result := make([]commands.File, 0)
|
result := make([]*commands.File, 0)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.HasStagedChanges {
|
if file.HasStagedChanges {
|
||||||
result = append(result, file)
|
result = append(result, file)
|
||||||
@ -26,9 +26,9 @@ func (gui *Gui) stagedFiles() []commands.File {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) trackedFiles() []commands.File {
|
func (gui *Gui) trackedFiles() []*commands.File {
|
||||||
files := gui.State.Files
|
files := gui.State.Files
|
||||||
result := make([]commands.File, 0)
|
result := make([]*commands.File, 0)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.Tracked {
|
if file.Tracked {
|
||||||
result = append(result, file)
|
result = append(result, file)
|
||||||
@ -117,9 +117,9 @@ func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.Errors.ErrSubProcess
|
return gui.Errors.ErrSubProcess
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) getSelectedFile(g *gocui.Gui) (commands.File, error) {
|
func (gui *Gui) getSelectedFile(g *gocui.Gui) (*commands.File, error) {
|
||||||
if len(gui.State.Files) == 0 {
|
if len(gui.State.Files) == 0 {
|
||||||
return commands.File{}, gui.Errors.ErrNoFiles
|
return &commands.File{}, gui.Errors.ErrNoFiles
|
||||||
}
|
}
|
||||||
filesView, err := g.View("files")
|
filesView, err := g.View("files")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -185,7 +185,7 @@ func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
gui.renderString(g, "main", gui.Tr.SLocalize("NoChangedFiles"))
|
gui.renderString(g, "main", gui.Tr.SLocalize("NoChangedFiles"))
|
||||||
return gui.renderfilesOptions(g, nil)
|
return gui.renderfilesOptions(g, nil)
|
||||||
}
|
}
|
||||||
gui.renderfilesOptions(g, &file)
|
gui.renderfilesOptions(g, file)
|
||||||
var content string
|
var content string
|
||||||
if file.HasMergeConflicts {
|
if file.HasMergeConflicts {
|
||||||
return gui.refreshMergePanel(g)
|
return gui.refreshMergePanel(g)
|
||||||
@ -276,25 +276,6 @@ func (gui *Gui) updateHasMergeConflictStatus() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) renderFile(file commands.File) string {
|
|
||||||
// potentially inefficient to be instantiating these color
|
|
||||||
// objects with each render
|
|
||||||
red := color.New(color.FgRed)
|
|
||||||
green := color.New(color.FgGreen)
|
|
||||||
if !file.Tracked && !file.HasStagedChanges {
|
|
||||||
return red.Sprint(file.DisplayString)
|
|
||||||
}
|
|
||||||
|
|
||||||
output := green.Sprint(file.DisplayString[0:1])
|
|
||||||
output += red.Sprint(file.DisplayString[1:3])
|
|
||||||
if file.HasUnstagedChanges {
|
|
||||||
output += red.Sprint(file.Name)
|
|
||||||
} else {
|
|
||||||
output += green.Sprint(file.Name)
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
|
func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
|
||||||
item, err := gui.getSelectedFile(g)
|
item, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -321,13 +302,12 @@ func (gui *Gui) refreshFiles(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
gui.refreshStateFiles()
|
gui.refreshStateFiles()
|
||||||
|
|
||||||
displayStrings := make([]string, len(gui.State.Files))
|
|
||||||
for i, file := range gui.State.Files {
|
|
||||||
displayStrings[i] = gui.renderFile(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
filesView.Clear()
|
filesView.Clear()
|
||||||
fmt.Fprint(filesView, strings.Join(displayStrings, "\n"))
|
list, err := utils.RenderList(gui.State.Files)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprint(filesView, list)
|
||||||
|
|
||||||
gui.correctCursor(filesView)
|
gui.correctCursor(filesView)
|
||||||
if filesView == g.CurrentView() {
|
if filesView == g.CurrentView() {
|
||||||
|
@ -71,10 +71,10 @@ type Gui struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type guiState struct {
|
type guiState struct {
|
||||||
Files []commands.File
|
Files []*commands.File
|
||||||
Branches []commands.Branch
|
Branches []*commands.Branch
|
||||||
Commits []commands.Commit
|
Commits []*commands.Commit
|
||||||
StashEntries []commands.StashEntry
|
StashEntries []*commands.StashEntry
|
||||||
PreviousView string
|
PreviousView string
|
||||||
HasMergeConflicts bool
|
HasMergeConflicts bool
|
||||||
ConflictIndex int
|
ConflictIndex int
|
||||||
@ -83,17 +83,17 @@ type guiState struct {
|
|||||||
EditHistory *stack.Stack
|
EditHistory *stack.Stack
|
||||||
Platform commands.Platform
|
Platform commands.Platform
|
||||||
Updating bool
|
Updating bool
|
||||||
Keys []Binding
|
Keys []*Binding
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGui builds a new gui handler
|
// NewGui builds a new gui handler
|
||||||
func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater) (*Gui, error) {
|
func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater) (*Gui, error) {
|
||||||
|
|
||||||
initialState := guiState{
|
initialState := guiState{
|
||||||
Files: make([]commands.File, 0),
|
Files: make([]*commands.File, 0),
|
||||||
PreviousView: "files",
|
PreviousView: "files",
|
||||||
Commits: make([]commands.Commit, 0),
|
Commits: make([]*commands.Commit, 0),
|
||||||
StashEntries: make([]commands.StashEntry, 0),
|
StashEntries: make([]*commands.StashEntry, 0),
|
||||||
ConflictIndex: 0,
|
ConflictIndex: 0,
|
||||||
ConflictTop: true,
|
ConflictTop: true,
|
||||||
Conflicts: make([]commands.Conflict, 0),
|
Conflicts: make([]commands.Conflict, 0),
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package gui
|
package gui
|
||||||
|
|
||||||
import "github.com/jesseduffield/gocui"
|
import (
|
||||||
|
"github.com/jesseduffield/gocui"
|
||||||
|
)
|
||||||
|
|
||||||
// Binding - a keybinding mapping a key and modifier to a handler. The keypress
|
// Binding - a keybinding mapping a key and modifier to a handler. The keypress
|
||||||
// is only handled if the given view has focus, or handled globally if the view
|
// is only handled if the given view has focus, or handled globally if the view
|
||||||
@ -14,8 +16,26 @@ type Binding struct {
|
|||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) GetKeybindings() []Binding {
|
// GetDisplayStrings returns the display string of a file
|
||||||
bindings := []Binding{
|
func (b *Binding) GetDisplayStrings() []string {
|
||||||
|
return []string{b.GetKey(), b.Description}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Binding) GetKey() string {
|
||||||
|
r, ok := b.Key.(rune)
|
||||||
|
key := ""
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
key = string(r)
|
||||||
|
} else if b.KeyReadable != "" {
|
||||||
|
key = b.KeyReadable
|
||||||
|
}
|
||||||
|
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) GetKeybindings() []*Binding {
|
||||||
|
bindings := []*Binding{
|
||||||
{
|
{
|
||||||
ViewName: "",
|
ViewName: "",
|
||||||
Key: 'q',
|
Key: 'q',
|
||||||
@ -360,7 +380,7 @@ func (gui *Gui) GetKeybindings() []Binding {
|
|||||||
// Would make these keybindings global but that interferes with editing
|
// Would make these keybindings global but that interferes with editing
|
||||||
// input in the confirmation panel
|
// input in the confirmation panel
|
||||||
for _, viewName := range []string{"status", "files", "branches", "commits", "stash", "menu"} {
|
for _, viewName := range []string{"status", "files", "branches", "commits", "stash", "menu"} {
|
||||||
bindings = append(bindings, []Binding{
|
bindings = append(bindings, []*Binding{
|
||||||
{ViewName: viewName, Key: gocui.KeyTab, Modifier: gocui.ModNone, Handler: gui.nextView},
|
{ViewName: viewName, Key: gocui.KeyTab, Modifier: gocui.ModNone, Handler: gui.nextView},
|
||||||
{ViewName: viewName, Key: gocui.KeyArrowLeft, Modifier: gocui.ModNone, Handler: gui.previousView},
|
{ViewName: viewName, Key: gocui.KeyArrowLeft, Modifier: gocui.ModNone, Handler: gui.previousView},
|
||||||
{ViewName: viewName, Key: gocui.KeyArrowRight, Modifier: gocui.ModNone, Handler: gui.nextView},
|
{ViewName: viewName, Key: gocui.KeyArrowRight, Modifier: gocui.ModNone, Handler: gui.nextView},
|
||||||
|
@ -49,50 +49,20 @@ func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.returnFocus(g, v)
|
return gui.returnFocus(g, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) GetKey(binding Binding) string {
|
|
||||||
r, ok := binding.Key.(rune)
|
|
||||||
key := ""
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
key = string(r)
|
|
||||||
} else if binding.KeyReadable != "" {
|
|
||||||
key = binding.KeyReadable
|
|
||||||
}
|
|
||||||
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) GetMaxKeyLength(bindings []Binding) int {
|
|
||||||
max := 0
|
|
||||||
for _, binding := range bindings {
|
|
||||||
keyLength := len(gui.GetKey(binding))
|
|
||||||
if keyLength > max {
|
|
||||||
max = keyLength
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return max
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
|
||||||
var (
|
var (
|
||||||
contentGlobal, contentPanel []string
|
bindingsGlobal, bindingsPanel []*Binding
|
||||||
bindingsGlobal, bindingsPanel []Binding
|
|
||||||
)
|
)
|
||||||
// clear keys slice, so we don't have ghost elements
|
// clear keys slice, so we don't have ghost elements
|
||||||
gui.State.Keys = gui.State.Keys[:0]
|
gui.State.Keys = gui.State.Keys[:0]
|
||||||
bindings := gui.GetKeybindings()
|
bindings := gui.GetKeybindings()
|
||||||
padWidth := gui.GetMaxKeyLength(bindings)
|
|
||||||
|
|
||||||
for _, binding := range bindings {
|
for _, binding := range bindings {
|
||||||
key := gui.GetKey(binding)
|
if binding.GetKey() != "" && binding.Description != "" {
|
||||||
if key != "" && binding.Description != "" {
|
|
||||||
content := fmt.Sprintf("%s %s", utils.WithPadding(key, padWidth), binding.Description)
|
|
||||||
switch binding.ViewName {
|
switch binding.ViewName {
|
||||||
case "":
|
case "":
|
||||||
contentGlobal = append(contentGlobal, content)
|
|
||||||
bindingsGlobal = append(bindingsGlobal, binding)
|
bindingsGlobal = append(bindingsGlobal, binding)
|
||||||
case v.Name():
|
case v.Name():
|
||||||
contentPanel = append(contentPanel, content)
|
|
||||||
bindingsPanel = append(bindingsPanel, binding)
|
bindingsPanel = append(bindingsPanel, binding)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,24 +70,25 @@ func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
|
|||||||
|
|
||||||
// append dummy element to have a separator between
|
// append dummy element to have a separator between
|
||||||
// panel and global keybindings
|
// panel and global keybindings
|
||||||
contentPanel = append(contentPanel, "")
|
bindingsPanel = append(bindingsPanel, &Binding{})
|
||||||
bindingsPanel = append(bindingsPanel, Binding{})
|
|
||||||
|
|
||||||
content := append(contentPanel, contentGlobal...)
|
|
||||||
gui.State.Keys = append(bindingsPanel, bindingsGlobal...)
|
gui.State.Keys = append(bindingsPanel, bindingsGlobal...)
|
||||||
contentJoined := strings.Join(content, "\n")
|
|
||||||
|
|
||||||
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, contentJoined)
|
list, err := utils.RenderList(gui.State.Keys)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, list)
|
||||||
menuView, _ := g.SetView("menu", x0, y0, x1, y1, 0)
|
menuView, _ := g.SetView("menu", x0, y0, x1, y1, 0)
|
||||||
menuView.Title = strings.Title(gui.Tr.SLocalize("menu"))
|
menuView.Title = strings.Title(gui.Tr.SLocalize("menu"))
|
||||||
menuView.FgColor = gocui.ColorWhite
|
menuView.FgColor = gocui.ColorWhite
|
||||||
|
menuView.Clear()
|
||||||
|
fmt.Fprint(menuView, list)
|
||||||
|
|
||||||
if err := gui.renderMenuOptions(g); err != nil {
|
if err := gui.renderMenuOptions(g); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprint(menuView, contentJoined)
|
|
||||||
|
|
||||||
g.Update(func(g *gocui.Gui) error {
|
g.Update(func(g *gocui.Gui) error {
|
||||||
_, err := g.SetViewOnTop("menu")
|
_, err := g.SetViewOnTop("menu")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2,10 +2,10 @@ package gui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
|
func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
|
||||||
@ -16,13 +16,12 @@ func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
gui.State.StashEntries = gui.GitCommand.GetStashEntries()
|
gui.State.StashEntries = gui.GitCommand.GetStashEntries()
|
||||||
|
|
||||||
displayStrings := make([]string, len(gui.State.StashEntries))
|
|
||||||
for i, stashEntry := range gui.State.StashEntries {
|
|
||||||
displayStrings[i] = stashEntry.DisplayString
|
|
||||||
}
|
|
||||||
|
|
||||||
v.Clear()
|
v.Clear()
|
||||||
fmt.Fprint(v, strings.Join(displayStrings, "\n"))
|
list, err := utils.RenderList(gui.State.StashEntries)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprint(v, list)
|
||||||
|
|
||||||
return gui.resetOrigin(v)
|
return gui.resetOrigin(v)
|
||||||
})
|
})
|
||||||
@ -35,7 +34,7 @@ func (gui *Gui) getSelectedStashEntry(v *gocui.View) *commands.StashEntry {
|
|||||||
}
|
}
|
||||||
stashView, _ := gui.g.View("stash")
|
stashView, _ := gui.g.View("stash")
|
||||||
lineNumber := gui.getItemPosition(stashView)
|
lineNumber := gui.getItemPosition(stashView)
|
||||||
return &gui.State.StashEntries[lineNumber]
|
return gui.State.StashEntries[lineNumber]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) renderStashOptions(g *gocui.Gui) error {
|
func (gui *Gui) renderStashOptions(g *gocui.Gui) error {
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -107,3 +109,67 @@ func Min(x, y int) int {
|
|||||||
}
|
}
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Displayable interface {
|
||||||
|
GetDisplayStrings() []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderList takes a slice of items, confirms they implement the Displayable
|
||||||
|
// interface, then generates a list of their displaystrings to write to a panel's
|
||||||
|
// buffer
|
||||||
|
func RenderList(slice interface{}) (string, error) {
|
||||||
|
s := reflect.ValueOf(slice)
|
||||||
|
if s.Kind() != reflect.Slice {
|
||||||
|
return "", errors.New("RenderList given a non-slice type")
|
||||||
|
}
|
||||||
|
|
||||||
|
displayables := make([]Displayable, s.Len())
|
||||||
|
|
||||||
|
for i := 0; i < s.Len(); i++ {
|
||||||
|
value, ok := s.Index(i).Interface().(Displayable)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("item does not implement the Displayable interface")
|
||||||
|
}
|
||||||
|
displayables[i] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return renderDisplayableList(displayables)
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderDisplayableList takes a list of displayable items, obtains their display
|
||||||
|
// strings via GetDisplayStrings() and then returns a single string containing
|
||||||
|
// each item's string representation on its own line, with appropriate horizontal
|
||||||
|
// padding between the item's own strings
|
||||||
|
func renderDisplayableList(items []Displayable) (string, error) {
|
||||||
|
displayStrings := make([][]string, len(items))
|
||||||
|
|
||||||
|
for i, item := range items {
|
||||||
|
displayStrings[i] = item.GetDisplayStrings()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(displayStrings) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// use first element to determine how many times to do this
|
||||||
|
padWidths := make([]int, len(displayStrings[0]))
|
||||||
|
for i, _ := range padWidths {
|
||||||
|
for _, strings := range displayStrings {
|
||||||
|
if len(strings) != len(padWidths) {
|
||||||
|
return "", errors.New("Each item must return the same number of strings to display")
|
||||||
|
}
|
||||||
|
if len(strings[i]) > padWidths[i] {
|
||||||
|
padWidths[i] = len(strings[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paddedDisplayStrings := make([]string, len(displayStrings))
|
||||||
|
for i, strings := range displayStrings {
|
||||||
|
for j, padWidth := range padWidths {
|
||||||
|
paddedDisplayStrings[i] += WithPadding(strings[j], padWidth) + " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(paddedDisplayStrings, "\n"), nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user