mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-27 23:08:02 +02:00
Make RenderDisplayStrings return the column positions
Not used by anything yet, but we'll need it later in this branch.
This commit is contained in:
parent
7a8df7795c
commit
7953f7fa86
@ -23,7 +23,7 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
|
||||
if self.getColumnAlignments != nil {
|
||||
columnAlignments = self.getColumnAlignments()
|
||||
}
|
||||
lines := utils.RenderDisplayStrings(
|
||||
lines, _ := utils.RenderDisplayStrings(
|
||||
self.getDisplayStrings(startIdx, utils.Min(endIdx, self.list.Len())),
|
||||
columnAlignments)
|
||||
return strings.Join(lines, "\n")
|
||||
|
@ -422,7 +422,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
|
||||
s.showYouAreHereLabel,
|
||||
)
|
||||
|
||||
renderedLines := utils.RenderDisplayStrings(result, nil)
|
||||
renderedLines, _ := utils.RenderDisplayStrings(result, nil)
|
||||
renderedResult := strings.Join(renderedLines, "\n")
|
||||
t.Logf("\n%s", renderedResult)
|
||||
|
||||
|
@ -37,10 +37,14 @@ func WithPadding(str string, padding int, alignment Alignment) string {
|
||||
|
||||
// defaults to left-aligning each column. If you want to set the alignment of
|
||||
// each column, pass in a slice of Alignment values.
|
||||
func RenderDisplayStrings(displayStringsArr [][]string, columnAlignments []Alignment) []string {
|
||||
// returns a list of strings that should be joined with "\n", and an array of
|
||||
// the column positions
|
||||
func RenderDisplayStrings(displayStringsArr [][]string, columnAlignments []Alignment) ([]string, []int) {
|
||||
displayStringsArr, columnAlignments = excludeBlankColumns(displayStringsArr, columnAlignments)
|
||||
padWidths := getPadWidths(displayStringsArr)
|
||||
columnConfigs := make([]ColumnConfig, len(padWidths))
|
||||
columnPositions := make([]int, len(padWidths)+1)
|
||||
columnPositions[0] = 0
|
||||
for i, padWidth := range padWidths {
|
||||
// gracefully handle when columnAlignments is shorter than padWidths
|
||||
alignment := AlignLeft
|
||||
@ -52,8 +56,9 @@ func RenderDisplayStrings(displayStringsArr [][]string, columnAlignments []Align
|
||||
Width: padWidth,
|
||||
Alignment: alignment,
|
||||
}
|
||||
columnPositions[i+1] = columnPositions[i] + padWidth + 1
|
||||
}
|
||||
return getPaddedDisplayStrings(displayStringsArr, columnConfigs)
|
||||
return getPaddedDisplayStrings(displayStringsArr, columnConfigs), columnPositions
|
||||
}
|
||||
|
||||
// NOTE: this mutates the input slice for the sake of performance
|
||||
|
@ -158,71 +158,84 @@ func TestTruncateWithEllipsis(t *testing.T) {
|
||||
|
||||
func TestRenderDisplayStrings(t *testing.T) {
|
||||
type scenario struct {
|
||||
input [][]string
|
||||
columnAlignments []Alignment
|
||||
expected string
|
||||
input [][]string
|
||||
columnAlignments []Alignment
|
||||
expectedOutput string
|
||||
expectedColumnPositions []int
|
||||
}
|
||||
|
||||
tests := []scenario{
|
||||
{
|
||||
input: [][]string{{""}, {""}},
|
||||
columnAlignments: nil,
|
||||
expected: "",
|
||||
input: [][]string{{""}, {""}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "",
|
||||
expectedColumnPositions: []int{0},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a"}, {""}},
|
||||
columnAlignments: nil,
|
||||
expected: "a\n",
|
||||
input: [][]string{{"a"}, {""}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "a\n",
|
||||
expectedColumnPositions: []int{0},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a"}, {"b"}},
|
||||
columnAlignments: nil,
|
||||
expected: "a\nb",
|
||||
input: [][]string{{"a"}, {"b"}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "a\nb",
|
||||
expectedColumnPositions: []int{0},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a", "b"}, {"c", "d"}},
|
||||
columnAlignments: nil,
|
||||
expected: "a b\nc d",
|
||||
input: [][]string{{"a", "b"}, {"c", "d"}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "a b\nc d",
|
||||
expectedColumnPositions: []int{0, 2},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
|
||||
columnAlignments: nil,
|
||||
expected: "a c\nd f",
|
||||
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "a c\nd f",
|
||||
expectedColumnPositions: []int{0, 2},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
|
||||
columnAlignments: nil,
|
||||
expected: "a c\nd f",
|
||||
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "a c\nd f",
|
||||
expectedColumnPositions: []int{0, 2},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: nil,
|
||||
expected: "abc d\ne f",
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: nil,
|
||||
expectedOutput: "abc d\ne f",
|
||||
expectedColumnPositions: []int{0, 4},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
|
||||
expected: "abc d\ne f",
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
|
||||
expectedOutput: "abc d\ne f",
|
||||
expectedColumnPositions: []int{0, 4},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignRight, AlignLeft},
|
||||
expected: "abc d\n e f",
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignRight, AlignLeft},
|
||||
expectedOutput: "abc d\n e f",
|
||||
expectedColumnPositions: []int{0, 4},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"a", "", "bcd", "efg", "h"}, {"i", "", "j", "k", "l"}},
|
||||
columnAlignments: []Alignment{AlignLeft, AlignLeft, AlignRight, AlignLeft},
|
||||
expected: "a bcd efg h\ni j k l",
|
||||
input: [][]string{{"a", "", "bcd", "efg", "h"}, {"i", "", "j", "k", "l"}},
|
||||
columnAlignments: []Alignment{AlignLeft, AlignLeft, AlignRight, AlignLeft},
|
||||
expectedOutput: "a bcd efg h\ni j k l",
|
||||
expectedColumnPositions: []int{0, 2, 6, 10},
|
||||
},
|
||||
{
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
|
||||
expected: "abc d\n e f",
|
||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
|
||||
expectedOutput: "abc d\n e f",
|
||||
expectedColumnPositions: []int{0, 4},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := RenderDisplayStrings(test.input, test.columnAlignments)
|
||||
assert.EqualValues(t, test.expected, strings.Join(output, "\n"))
|
||||
output, columnPositions := RenderDisplayStrings(test.input, test.columnAlignments)
|
||||
assert.EqualValues(t, test.expectedOutput, strings.Join(output, "\n"))
|
||||
assert.EqualValues(t, test.expectedColumnPositions, columnPositions)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user