This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
forked from emicklei/proto
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add aligned , moved formatter to pkg, work on formatter
Change-Id: I6cbf18dcf887ec61d9eda2f334a94eff6dea9360
- Loading branch information
Showing
9 changed files
with
276 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package proto | ||
|
||
import "strings" | ||
|
||
type aligned struct { | ||
source string | ||
left bool | ||
} | ||
|
||
var alignedEquals = leftAligned(" = ") | ||
|
||
func leftAligned(src string) aligned { return aligned{src, true} } | ||
func rightAligned(src string) aligned { return aligned{src, false} } | ||
|
||
func (a aligned) preferredWidth() int { return len(a.source) } | ||
|
||
func (a aligned) formatted(width int) string { | ||
if len(a.source) > width { | ||
return a.source[:width] | ||
} | ||
if a.left { | ||
return a.source + strings.Repeat(" ", width-len(a.source)) | ||
} | ||
return strings.Repeat(" ", width-len(a.source)) + a.source | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package proto | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestPrintListOfColumns(t *testing.T) { | ||
e0 := new(EnumField) | ||
e0.Name = "A" | ||
e0.Integer = 1 | ||
op0 := new(Option) | ||
op0.IsEmbedded = true | ||
op0.Name = "a" | ||
op0.Constant = Literal{Source: "1234"} | ||
e0.ValueOption = op0 | ||
|
||
e1 := new(EnumField) | ||
e1.Name = "ABC" | ||
e1.Integer = 12 | ||
op1 := new(Option) | ||
op1.IsEmbedded = true | ||
op1.Name = "ab" | ||
op1.Constant = Literal{Source: "1234"} | ||
e1.ValueOption = op1 | ||
|
||
list := []columnsPrintable{e0, e1} | ||
b := new(bytes.Buffer) | ||
f := NewFormatter(b, " ") | ||
f.printListOfColumns(list) | ||
formatted := ` | ||
A = 1 [a =1234]; | ||
ABC = 12 [ab=1234]; | ||
` | ||
if got, want := b.String(), formatted; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} |
Oops, something went wrong.