Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
)
}
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
}
121 changes: 121 additions & 0 deletions loader/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package loader

import (
"strings"

"github.com/gol4ng/cli"
"github.com/gol4ng/cli/loader/style"
"github.com/gol4ng/cli/unicode"
)

var VerticalChar = []rune{
' ',
unicode.LowerOneEighthBlock,
unicode.LowerOneQuarterBlock,
unicode.LowerThreeEighthsBlock,
unicode.LowerHalfBlock,
unicode.LowerFiveEighthsBlock,
unicode.LowerThreeQuartersBlock,
unicode.LowerSevenEighthsBlock,
unicode.FullBlock,
}

var HorizontalChar = []rune{
' ',
unicode.LeftOneEighthBlock,
unicode.LeftOneQuarterBlock,
unicode.LeftThreeEighthsBlock,
unicode.LeftHalfBlock,
unicode.LeftFiveEighthsBlock,
unicode.LeftThreeQuartersBlock,
unicode.LeftSevenEighthsBlock,
unicode.FullBlock,
}

var DotChar = []rune{
' ',
unicode.BraillePatternDots_1,
unicode.BraillePatternDots_14,
unicode.BraillePatternDots_124,
unicode.BraillePatternDots_1245,
unicode.BraillePatternDots_12345,
unicode.BraillePatternDots_123456,
unicode.BraillePatternDots_1234567,
unicode.BraillePatternDots_12345678,
}

type BarStyle interface {
BarChar() string
BarLen() uint
EmptyChar() string
EmptyLen() uint
CursorChar() string
CursorLen() uint
}

type Bar struct {
BarStyle
length uint
}

func (d *Bar) Render(progress Progress) string {
return d.RenderIn(progress, &strings.Builder{})
}

func (d *Bar) RenderIn(progress Progress, buffer cli.Buffer) string {
percent := progress.Percent()
barLen := int(d.length * uint(percent) / 100 * d.BarStyle.BarLen())
emptyLen := int(d.length * d.BarStyle.EmptyLen())
if percent > 0 {
buffer.WriteString(strings.Repeat(string(d.BarStyle.BarChar()), barLen))
emptyLen -= barLen

if percent < 100 {
cursor := d.BarStyle.CursorChar()
if emptyLen < int(d.BarStyle.CursorLen()) {
cursor = cursor[:emptyLen]
}
buffer.WriteString(cursor)
emptyLen -= int(d.BarStyle.CursorLen())
}
}
if emptyLen > 0 {
buffer.WriteString(strings.Repeat(string(d.BarStyle.EmptyChar()), int(emptyLen)))
}

return buffer.String()
}

func NewDefaultBarRenderer(length uint) *Bar {
return NewBarRenderer(length, style.NewBar())
}

func NewBarRenderer(length uint, style BarStyle) *Bar {
return &Bar{
length: length,
BarStyle: style,
}
}

type SliceBar struct {
chars []rune
}

func (d *SliceBar) Render(progress Progress) string {
return d.RenderIn(progress, &strings.Builder{})
}

func (d *SliceBar) RenderIn(progress Progress, buffer cli.Buffer) string {
percent := progress.Percent()
threshold := float64(100) / float64(len(d.chars))
for i := 0; i < len(d.chars); i++ {
if percent < threshold*float64(i+1) {
return string(d.chars[i])
}
}
return string(d.chars[len(d.chars)-1])
}

func NewSliceBar(chars []rune) *SliceBar {
return &SliceBar{chars: chars}
}
Loading