This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
5 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 |
---|---|---|
|
@@ -2,9 +2,10 @@ language: go | |
|
||
go: | ||
- 1.8.7 | ||
- 1.9.6 | ||
- 1.10.2 | ||
- 1.11.5 | ||
- 1.9.7 | ||
- 1.10.8 | ||
- 1.11.9 | ||
- 1.12.4 | ||
|
||
script: | ||
- go test ./... |
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,46 @@ | ||
// Package afmt (Advanced formatter or Advanced fmt) implement some extensions | ||
// for fmt package. The main feature is print to tree structure. | ||
package afmt | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// ColPrinter definition. | ||
type ColPrinter struct{} | ||
|
||
// NewColPrinter allocates new column printer object. | ||
func NewColPrinter() *ColPrinter { | ||
printer := &ColPrinter{} | ||
return printer | ||
} | ||
|
||
// Print adding spaces between operands when neither is a string. Newline is | ||
// always added in order to respect column maximum characters (lineLimit). It | ||
// return string with maximum length of line based on lineLimit. | ||
func (c *ColPrinter) Print(lineLimit int, a ...interface{}) (out string) { | ||
var buf string | ||
|
||
for _, text := range a { | ||
for _, s := range text.(string) { | ||
if !(len(buf) == 0 && unicode.IsSpace(s)) { | ||
buf += string(s) | ||
} | ||
|
||
if len(buf) >= lineLimit { | ||
if space := strings.LastIndexFunc(buf, unicode.IsSpace); space != -1 { | ||
out += buf[:space] + "\n" | ||
buf = buf[space+1:] | ||
} else { | ||
out += buf + "\n" | ||
buf = "" | ||
} | ||
} | ||
} | ||
buf += " " | ||
} | ||
|
||
out += buf[:len(buf)-1] | ||
return out | ||
} |
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,44 @@ | ||
// Package afmt (Advanced formatter or Advanced fmt) implement some extensions | ||
// for fmt package. The main feature is print to tree structure. | ||
package afmt | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func TestBasic(t *testing.T) { | ||
cp := NewColPrinter() | ||
|
||
for _, c := range []struct { | ||
in string | ||
want string | ||
param int | ||
}{ | ||
{"Lorem ipsum dolor sit amet", "Lorem\nipsum\ndolor\nsit\namet", 5}, | ||
{"Lorem ipsum dolor sit amet", "Lorem ipsum\ndolor sit amet", 15}, | ||
} { | ||
if cp.Print(c.param, c.in) != c.want { | ||
t.Errorf("Unpredictable result for column count: %d", c.param) | ||
} | ||
} | ||
} | ||
|
||
func TestExtended(t *testing.T) { | ||
cp := NewColPrinter() | ||
|
||
for _, c := range []struct { | ||
in1 string | ||
in2 string | ||
want string | ||
param int | ||
}{ | ||
{"Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet", "Lorem\nipsum\ndolor\nsit\namet\nLorem\nipsum\ndolor\nsit\namet", 5}, | ||
{"Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet", "Lorem ipsum\ndolor sit amet\nLorem ipsum\ndolor sit amet", 15}, | ||
} { | ||
if cp.Print(c.param, c.in1, c.in2) != c.want { | ||
fmt.Println(cp.Print(c.param, c.in1, c.in2)) | ||
t.Errorf("Unpredictable result for column count: %d", c.param) | ||
} | ||
} | ||
} |