mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-31 23:19:40 +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 {
|
if self.getColumnAlignments != nil {
|
||||||
columnAlignments = self.getColumnAlignments()
|
columnAlignments = self.getColumnAlignments()
|
||||||
}
|
}
|
||||||
lines := utils.RenderDisplayStrings(
|
lines, _ := utils.RenderDisplayStrings(
|
||||||
self.getDisplayStrings(startIdx, utils.Min(endIdx, self.list.Len())),
|
self.getDisplayStrings(startIdx, utils.Min(endIdx, self.list.Len())),
|
||||||
columnAlignments)
|
columnAlignments)
|
||||||
return strings.Join(lines, "\n")
|
return strings.Join(lines, "\n")
|
||||||
|
@ -422,7 +422,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
|
|||||||
s.showYouAreHereLabel,
|
s.showYouAreHereLabel,
|
||||||
)
|
)
|
||||||
|
|
||||||
renderedLines := utils.RenderDisplayStrings(result, nil)
|
renderedLines, _ := utils.RenderDisplayStrings(result, nil)
|
||||||
renderedResult := strings.Join(renderedLines, "\n")
|
renderedResult := strings.Join(renderedLines, "\n")
|
||||||
t.Logf("\n%s", renderedResult)
|
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
|
// defaults to left-aligning each column. If you want to set the alignment of
|
||||||
// each column, pass in a slice of Alignment values.
|
// 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)
|
displayStringsArr, columnAlignments = excludeBlankColumns(displayStringsArr, columnAlignments)
|
||||||
padWidths := getPadWidths(displayStringsArr)
|
padWidths := getPadWidths(displayStringsArr)
|
||||||
columnConfigs := make([]ColumnConfig, len(padWidths))
|
columnConfigs := make([]ColumnConfig, len(padWidths))
|
||||||
|
columnPositions := make([]int, len(padWidths)+1)
|
||||||
|
columnPositions[0] = 0
|
||||||
for i, padWidth := range padWidths {
|
for i, padWidth := range padWidths {
|
||||||
// gracefully handle when columnAlignments is shorter than padWidths
|
// gracefully handle when columnAlignments is shorter than padWidths
|
||||||
alignment := AlignLeft
|
alignment := AlignLeft
|
||||||
@ -52,8 +56,9 @@ func RenderDisplayStrings(displayStringsArr [][]string, columnAlignments []Align
|
|||||||
Width: padWidth,
|
Width: padWidth,
|
||||||
Alignment: alignment,
|
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
|
// NOTE: this mutates the input slice for the sake of performance
|
||||||
|
@ -160,69 +160,82 @@ func TestRenderDisplayStrings(t *testing.T) {
|
|||||||
type scenario struct {
|
type scenario struct {
|
||||||
input [][]string
|
input [][]string
|
||||||
columnAlignments []Alignment
|
columnAlignments []Alignment
|
||||||
expected string
|
expectedOutput string
|
||||||
|
expectedColumnPositions []int
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []scenario{
|
tests := []scenario{
|
||||||
{
|
{
|
||||||
input: [][]string{{""}, {""}},
|
input: [][]string{{""}, {""}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "",
|
expectedOutput: "",
|
||||||
|
expectedColumnPositions: []int{0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a"}, {""}},
|
input: [][]string{{"a"}, {""}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "a\n",
|
expectedOutput: "a\n",
|
||||||
|
expectedColumnPositions: []int{0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a"}, {"b"}},
|
input: [][]string{{"a"}, {"b"}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "a\nb",
|
expectedOutput: "a\nb",
|
||||||
|
expectedColumnPositions: []int{0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a", "b"}, {"c", "d"}},
|
input: [][]string{{"a", "b"}, {"c", "d"}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "a b\nc d",
|
expectedOutput: "a b\nc d",
|
||||||
|
expectedColumnPositions: []int{0, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
|
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "a c\nd f",
|
expectedOutput: "a c\nd f",
|
||||||
|
expectedColumnPositions: []int{0, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
|
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "a c\nd f",
|
expectedOutput: "a c\nd f",
|
||||||
|
expectedColumnPositions: []int{0, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||||
columnAlignments: nil,
|
columnAlignments: nil,
|
||||||
expected: "abc d\ne f",
|
expectedOutput: "abc d\ne f",
|
||||||
|
expectedColumnPositions: []int{0, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||||
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
|
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
|
||||||
expected: "abc d\ne f",
|
expectedOutput: "abc d\ne f",
|
||||||
|
expectedColumnPositions: []int{0, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||||
columnAlignments: []Alignment{AlignRight, AlignLeft},
|
columnAlignments: []Alignment{AlignRight, AlignLeft},
|
||||||
expected: "abc d\n e f",
|
expectedOutput: "abc d\n e f",
|
||||||
|
expectedColumnPositions: []int{0, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"a", "", "bcd", "efg", "h"}, {"i", "", "j", "k", "l"}},
|
input: [][]string{{"a", "", "bcd", "efg", "h"}, {"i", "", "j", "k", "l"}},
|
||||||
columnAlignments: []Alignment{AlignLeft, AlignLeft, AlignRight, AlignLeft},
|
columnAlignments: []Alignment{AlignLeft, AlignLeft, AlignRight, AlignLeft},
|
||||||
expected: "a bcd efg h\ni j k l",
|
expectedOutput: "a bcd efg h\ni j k l",
|
||||||
|
expectedColumnPositions: []int{0, 2, 6, 10},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
|
||||||
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
|
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
|
||||||
expected: "abc d\n e f",
|
expectedOutput: "abc d\n e f",
|
||||||
|
expectedColumnPositions: []int{0, 4},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
output := RenderDisplayStrings(test.input, test.columnAlignments)
|
output, columnPositions := RenderDisplayStrings(test.input, test.columnAlignments)
|
||||||
assert.EqualValues(t, test.expected, strings.Join(output, "\n"))
|
assert.EqualValues(t, test.expectedOutput, strings.Join(output, "\n"))
|
||||||
|
assert.EqualValues(t, test.expectedColumnPositions, columnPositions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user