Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Added new functionality: PrintCol
Browse files Browse the repository at this point in the history
  • Loading branch information
praserx committed Apr 30, 2019
1 parent 4a2b7a5 commit ae5ef3b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 5 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@

It is an extension of standard Golang formatter. It contains (or it will contain) some advanced features for command line print. All features are described bellow.

## TreePrint
## Features

- Line wrap (`PrintCol`)
- Tree print (`PrintTree`)

### PrintCol

The `PrintCol` function (and its derivations) allow wrap text based on integer specification. It means that you can set your stop mark and start print of text on the new line.

```go
afmt.PrintCol(80, "This is my longest text ever.")
```

### PrintTree

The TreePrinter expect any type as argument. It use interfaces, which are analyzed and printed. So you can use string, integer, array, structure and etc. The power of TreePrinter is that you can use any structure and TreePrinter prints it for you. There are many settings, which you can use.

Expand Down Expand Up @@ -63,7 +76,7 @@ MyStructure:

## Planned features

- Line wrap (by value)
- Text padding
- Progress bar

Is something missing? Create issue and describe your idea!
Expand Down
47 changes: 47 additions & 0 deletions afmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package afmt

import (
"fmt"
"io"
"os"
)

// PrintTree print input to standard output structured to tree representation.
Expand All @@ -18,3 +20,48 @@ func PrintTree(structure interface{}) error {

return err
}

// FprintCol formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string. Newline is always
// added in order to respect column maximum characters. It returns the number of
// bytes written and any write error encountered.
func FprintCol(w io.Writer, lineLimit int, a ...interface{}) (n int, err error) {
p := NewColPrinter()
str := p.Print(lineLimit, a...)
n, err = fmt.Fprint(w, str)
return n, err
}

// FprintlnCol formats using the default formats for its operands and writes to
// w. Spaces are always added between operands and a newline is appended.
// Newline is always added in order to respect column maximum characters. It
// returns the number of bytes written and any write error encountered.
func FprintlnCol(w io.Writer, lineLimit int, a ...interface{}) (n int, err error) {
p := NewColPrinter()
str := p.Print(lineLimit, a...)
n, err = fmt.Fprintln(w, str)
return n, err
}

// PrintCol formats using the default formats for its operands and writes to
// standard output. Spaces are added between operands when neither is a string.
// Newline is always added in order to respect column maximum characters. It
// returns the number of bytes written and any write error encountered.
func PrintCol(lineLimit int, a ...interface{}) (n int, err error) {
p := NewColPrinter()
str := p.Print(lineLimit, a...)
n, err = fmt.Fprint(os.Stdout, str)
return n, err
}

// PrintlnCol formats using the default formats for its operands and writes to
// standard output. Spaces are always added between operands and a newline is
// appended. Newline is always added in order to respect column maximum
// characters. It returns the number of bytes written and any write error
// encountered.
func PrintlnCol(lineLimit int, a ...interface{}) (n int, err error) {
p := NewColPrinter()
str := p.Print(lineLimit, a...)
n, err = fmt.Fprintln(os.Stdout, str)
return n, err
}
46 changes: 46 additions & 0 deletions col_printer.go
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
}
44 changes: 44 additions & 0 deletions col_printer_test.go
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)
}
}
}

0 comments on commit ae5ef3b

Please sign in to comment.