-
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
1 parent
d113e1e
commit 2fc7323
Showing
19 changed files
with
1,396 additions
and
133 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,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 = ";" | ||
) |
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,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{}) | ||
} |
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,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(), | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 ⣿ | ||
} |
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,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 | ||
} |
Oops, something went wrong.