Skip to content

Commit

Permalink
Alpha version
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign committed Apr 19, 2019
1 parent d113e1e commit 2fc7323
Show file tree
Hide file tree
Showing 19 changed files with 1,396 additions and 133 deletions.
15 changes: 15 additions & 0 deletions ansi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cli

// https://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
const (
ESC string = "\x1b"

//Control Sequence Introducer
CSI = ESC + "["

//Select Graphic Rendition
SGREnd = "m"
SGRReset = CSI + SGREnd

SepControlChar = ";"
)
36 changes: 36 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import "strings"

type Writer interface {
WriteString(s string) (int, error)
}

type Buffer interface {
Writer
String() string
}

type Builder struct {
buffer Buffer
}

func (c *Builder) WriteString(s string) *Builder {
_, err := c.buffer.WriteString(s)
if err != nil {
panic(err)
}
return c
}

func (c *Builder) String() string {
return c.buffer.String()
}

func New(buffer Buffer) *Builder {
return &Builder{buffer: buffer}
}

func NewStringBuilder() *Builder {
return New(&strings.Builder{})
}
37 changes: 37 additions & 0 deletions examples/ansi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examples_test

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/gol4ng/cli"
)

func Test_ansi_sgr(t *testing.T) {
w := cli.NewStringBuilder().

// add some content
Write("first content\n").

// add some content with style
Write("stylize content\n", cli.Italic, cli.Yellow).
Write("style resetted\n").
Red("style red\n").
Bold("style bold\n").

// begin style and write content
Style(cli.Underline, cli.Red).WriteString("apply a style\n").
WriteString("previous style continue until Reset was called or another style apply\n").

// new style began
Style(cli.Bold, cli.Blue).WriteString("new style\n")

// manually reset style
w.Reset().Write("style manually resetted\n")

assert.Equal(
t,
"first content\n\x1b[m\x1b[3;33mstylize content\n\x1b[mstyle resetted\n\x1b[m\x1b[31mstyle red\n\x1b[m\x1b[1mstyle bold\n\x1b[m\x1b[4;31mapply a style\nprevious style continue until Reset was called or another style apply\n\x1b[1;34mnew style\n\x1b[mstyle manually resetted\n\x1b[m",
w.String(),
)
}
33 changes: 0 additions & 33 deletions examples/loader_test.go

This file was deleted.

130 changes: 130 additions & 0 deletions examples/progress_bar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package examples_test

import (
"fmt"
"github.com/gol4ng/cli/loader"
"github.com/gol4ng/cli/loader/style"
)

func Example_progress_bar() {
progress := loader.NewDefaultProgressBar(10, 10)

fmt.Println(progress.Progress.String(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.SetStep(5), progress)

fmt.Println(progress.Fill(), progress)

fmt.Println(progress.SetMax(20), progress)
fmt.Println(progress.SetStep(15), progress)
fmt.Println(progress.SetMax(10), progress)

// Output:

// 0/10 ░░░░░░░░░░
// 1/10 █▒░░░░░░░░
// 5/10 █████▒░░░░
// 10/10 ██████████
// 10/20 █████▒░░░░
// 15/20 ███████▒░░
// 10/10 ██████████
}

func Example_progress_bar_custom() {
progress := loader.NewProgressBar(10, loader.NewBarRenderer(10, style.NewBarSimple("=", "-", ">")))

fmt.Println(progress.Progress.String(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.SetStep(5), progress)

fmt.Println(progress.Fill(), progress)

fmt.Println(progress.SetMax(20), progress)
fmt.Println(progress.SetStep(15), progress)
fmt.Println(progress.SetMax(10), progress)

// Output:

// 0/10 ----------
// 1/10 =>--------
// 5/10 =====>----
// 10/10 ==========
// 10/20 =====>----
// 15/20 =======>--
// 10/10 ==========
}

func Example_progress_bar_vertical_char() {
progress := loader.NewProgressBar(8, loader.NewSliceBar(loader.VerticalChar))

fmt.Println(progress.Progress.String(), progress, "|")
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)

// Output:
// 0/8 |
// 1/8 ▁
// 2/8 ▂
// 3/8 ▃
// 4/8 ▄
// 5/8 ▅
// 6/8 ▆
// 7/8 ▇
// 8/8 █
}

func Example_progress_bar_horizontal_char() {
progress := loader.NewProgressBar(8, loader.NewSliceBar(loader.HorizontalChar))

fmt.Println(progress.Progress.String(), progress, "|")
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)

// Output:
// 0/8 |
// 1/8 ▏
// 2/8 ▎
// 3/8 ▍
// 4/8 ▌
// 5/8 ▋
// 6/8 ▊
// 7/8 ▉
// 8/8 █
}

func Example_progress_dot_char() {
progress := loader.NewProgressBar(8, loader.NewSliceBar(loader.DotChar))

fmt.Println(progress.Progress.String(), progress, "|")
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)
fmt.Println(progress.Increase(), progress)

// Output:
//0/8 |
//1/8 ⠁
//2/8 ⠉
//3/8 ⠋
//4/8 ⠛
//5/8 ⠟
//6/8 ⠿
//7/8 ⡿
//8/8 ⣿
}
33 changes: 33 additions & 0 deletions examples/progress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package examples_test

import (
"fmt"
"github.com/gol4ng/cli/loader"
)

func Example_progress() {
progress := loader.NewProgress(10)

fmt.Println(progress.Decrease(), progress.Percent())
fmt.Println(progress.Fill(), progress.Percent())
fmt.Println(progress.Increase(), progress.Percent())

fmt.Println(progress.SetStep(5), progress.Percent())
fmt.Println(progress.Decrease(), progress.Percent())
fmt.Println(progress.Reset(), progress.Percent())

fmt.Println(progress.SetMax(20), progress.Percent())
fmt.Println(progress.SetStep(15), progress.Percent())
fmt.Println(progress.SetMax(10), progress.Percent())

// Output:
// 0/10 0
// 10/10 100
// 10/10 100
// 5/10 50
// 4/10 40
// 0/10 0
// 0/20 0
// 15/20 75
// 10/10 100
}
Loading

0 comments on commit 2fc7323

Please sign in to comment.