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

Added interactive printers 🎉🥳 #360

Merged
merged 27 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1200d36
added `InteractiveConfirmPrinter` printer
MarvinJWendt Jun 2, 2022
5eb30b3
added `InteractiveSelectPrinter` printer
MarvinJWendt Jun 2, 2022
6355fa9
updated `atomicgo.dev/cursor` and `atomicgo.dev/keyboard`
MarvinJWendt Jun 3, 2022
98afa99
added `InteractiveSelectPrinter` implementation
MarvinJWendt Jun 3, 2022
511a912
added fuzzy search to `InteractiveSelect`
MarvinJWendt Jun 12, 2022
c851bb2
added examples for interactive printers
MarvinJWendt Jun 15, 2022
460130a
fixed fuzzy search
MarvinJWendt Jun 15, 2022
21ed627
implemented select menu scrolling
MarvinJWendt Jun 16, 2022
5703d7b
fixed select printer
MarvinJWendt Jun 16, 2022
62bd0e5
fixed max height bug
MarvinJWendt Jun 16, 2022
8725f04
fixed max height bug
MarvinJWendt Jun 16, 2022
50ee390
fixed max height bug
MarvinJWendt Jun 16, 2022
9623eb6
feat(input): added text input printer
floaust Jun 17, 2022
381a469
Merge branch 'master' into 358-implement-interactive-printers
MarvinJWendt Jun 20, 2022
2b2936c
bump golangci-lint
MarvinJWendt Jun 20, 2022
d85b361
Merge remote-tracking branch 'origin/358-implement-interactive-printe…
MarvinJWendt Jun 20, 2022
92d4402
fix golangci-lint workflow to work with 1.18
MarvinJWendt Jun 20, 2022
f176e0b
fixed linting
MarvinJWendt Jun 20, 2022
e53e43c
fixed linting
MarvinJWendt Jun 20, 2022
15cc2a4
added better example to the interactive select printer
MarvinJWendt Jun 20, 2022
238d02a
added hint to multiline text input
MarvinJWendt Jun 20, 2022
263b472
fixed multiline input example CI logic
MarvinJWendt Jun 20, 2022
c4d0905
added `InteractiveMultiselect`
MarvinJWendt Jun 20, 2022
a9b81a7
added `WithDefaultText`
MarvinJWendt Jun 20, 2022
22dee76
changed test coverage to informational
MarvinJWendt Jun 20, 2022
0609e02
added tests
MarvinJWendt Jun 20, 2022
ae51bcb
added docs
MarvinJWendt Jun 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/golangci.yml
@@ -1,3 +1,5 @@


name: golangci-lint
on: [ push, pull_request ]
jobs:
Expand All @@ -10,10 +12,9 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.39
version: latest
2 changes: 1 addition & 1 deletion _examples/box/demo/main.go
Expand Up @@ -3,7 +3,7 @@ package main
import "github.com/pterm/pterm"

func main() {
pterm.Info.Println("This might not be rendered correctly on GitHub,\nbut it will work in a real terminal.\nThis is because GitHub does not use a monospaced font by default for SVGs.")
pterm.Info.Println("This might not be rendered correctly on GitHub,\nbut it will work in a real terminal.\nThis is because GitHub does not use a monospaced font by default for SVGs")

panel1 := pterm.DefaultBox.Sprint("Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt\nut labore et dolore\nmagna aliqua.")
panel2 := pterm.DefaultBox.WithTitle("title").Sprint("Ut enim ad minim veniam,\nquis nostrud exercitation\nullamco laboris\nnisi ut aliquip\nex ea commodo\nconsequat.")
Expand Down
19 changes: 19 additions & 0 deletions _examples/interactive_confirm/demo/ci.go
@@ -0,0 +1,19 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second * 2)
keyboard.SimulateKeyPress('y')
}()
}
}
18 changes: 18 additions & 0 deletions _examples/interactive_confirm/demo/main.go
@@ -0,0 +1,18 @@
package main

import (
"github.com/pterm/pterm"
)

func main() {
result, _ := pterm.DefaultInteractiveConfirm.Show()
pterm.Println() // Blank line
pterm.Info.Printfln("You answered: %s", boolToText(result))
}

func boolToText(b bool) string {
if b {
return pterm.Green("Yes")
}
return pterm.Red("No")
}
44 changes: 44 additions & 0 deletions _examples/interactive_multiselect/demo/ci.go
@@ -0,0 +1,44 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
keyboard.SimulateKeyPress(keys.Down)
if i%2 == 0 {
time.Sleep(time.Millisecond * 100)
keyboard.SimulateKeyPress(keys.Enter)
}
time.Sleep(time.Millisecond * 500)
}
time.Sleep(time.Second)

for _, s := range "fuzzy" {
keyboard.SimulateKeyPress(s)
time.Sleep(time.Millisecond * 150)
}

time.Sleep(time.Second)

for i := 0; i < 2; i++ {
keyboard.SimulateKeyPress(keys.Down)
time.Sleep(time.Millisecond * 300)
}

keyboard.SimulateKeyPress(keys.Enter)
time.Sleep(time.Millisecond * 350)
keyboard.SimulateKeyPress(keys.Tab)
}()
}
}
22 changes: 22 additions & 0 deletions _examples/interactive_multiselect/demo/main.go
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/pterm/pterm"
)

func main() {
var options []string

for i := 0; i < 100; i++ {
options = append(options, fmt.Sprintf("Option %d", i))
}

for i := 0; i < 5; i++ {
options = append(options, fmt.Sprintf("You can use fuzzy searching (%d)", i))
}

selectedOptions, _ := pterm.DefaultInteractiveMultiselect.WithOptions(options).Show()
pterm.Info.Printfln("Selected options: %s", pterm.Green(selectedOptions))
}
38 changes: 38 additions & 0 deletions _examples/interactive_select/demo/ci.go
@@ -0,0 +1,38 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
keyboard.SimulateKeyPress(keys.Down)
time.Sleep(time.Millisecond * 250)
}
time.Sleep(time.Second)

for _, s := range "fuzzy" {
keyboard.SimulateKeyPress(s)
time.Sleep(time.Millisecond * 150)
}

time.Sleep(time.Second)

for i := 0; i < 2; i++ {
keyboard.SimulateKeyPress(keys.Down)
time.Sleep(time.Millisecond * 300)
}

keyboard.SimulateKeyPress(keys.Enter)
}()
}
}
22 changes: 22 additions & 0 deletions _examples/interactive_select/demo/main.go
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/pterm/pterm"
)

func main() {
var options []string

for i := 0; i < 100; i++ {
options = append(options, fmt.Sprintf("Option %d", i))
}

for i := 0; i < 5; i++ {
options = append(options, fmt.Sprintf("You can use fuzzy searching (%d)", i))
}

selectedOption, _ := pterm.DefaultInteractiveSelect.WithOptions(options).Show()
pterm.Info.Printfln("Selected option: %s", pterm.Green(selectedOption))
}
35 changes: 35 additions & 0 deletions _examples/interactive_textinput/demo/ci.go
@@ -0,0 +1,35 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second)
input := "Hello; World!"
for _, r := range []rune(input) {
keyboard.SimulateKeyPress(r)
time.Sleep(time.Millisecond * 250)
}

for i := 0; i < 7; i++ {
keyboard.SimulateKeyPress(keys.Left)
time.Sleep(time.Millisecond * 150)
}

keyboard.SimulateKeyPress(keys.Backspace)
time.Sleep(time.Millisecond * 500)
keyboard.SimulateKeyPress(',')
time.Sleep(time.Millisecond * 500)
keyboard.SimulateKeyPress(keys.Enter)
}()
}
}
11 changes: 11 additions & 0 deletions _examples/interactive_textinput/demo/main.go
@@ -0,0 +1,11 @@
package main

import (
"github.com/pterm/pterm"
)

func main() {
result, _ := pterm.DefaultInteractiveTextInput.WithMultiLine(false).Show()
pterm.Println() // Blank line
pterm.Info.Printfln("You answered: %s", result)
}
49 changes: 49 additions & 0 deletions _examples/interactive_textinput/multi-line/ci.go
@@ -0,0 +1,49 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second)
input := "1111111\n2222222"
for _, r := range input {
if r == '\n' {
keyboard.SimulateKeyPress(keys.Enter)
} else {
keyboard.SimulateKeyPress(r)
}
time.Sleep(time.Millisecond * 250)
}

for i := 0; i < 7; i++ {
keyboard.SimulateKeyPress(keys.Left)
time.Sleep(time.Millisecond * 150)
}

keyboard.SimulateKeyPress(keys.Backspace)
time.Sleep(time.Millisecond * 500)
keyboard.SimulateKeyPress(keys.Enter)
time.Sleep(time.Millisecond * 500)
input = "33333333\n4\n5555555"
for _, r := range input {
if r == '\n' {
keyboard.SimulateKeyPress(keys.Enter)
} else {
keyboard.SimulateKeyPress(r)
}
time.Sleep(time.Millisecond * 250)
}

keyboard.SimulateKeyPress(keys.Tab)
}()
}
}
11 changes: 11 additions & 0 deletions _examples/interactive_textinput/multi-line/main.go
@@ -0,0 +1,11 @@
package main

import (
"github.com/pterm/pterm"
)

func main() {
result, _ := pterm.DefaultInteractiveTextInput.WithMultiLine().Show() // Text input with multi line enabled
pterm.Println() // Blank line
pterm.Info.Printfln("You answered: %s", result)
}
2 changes: 1 addition & 1 deletion area_printer.go
Expand Up @@ -3,7 +3,7 @@ package pterm
import (
"strings"

"github.com/atomicgo/cursor"
"atomicgo.dev/cursor"

"github.com/pterm/pterm/internal"
)
Expand Down
8 changes: 4 additions & 4 deletions ci/main.go
Expand Up @@ -15,13 +15,13 @@ import (
)

type Examples struct {
sync.Mutex
m map[string]string
mu sync.Mutex
m map[string]string
}

func (e *Examples) Add(name, content string) {
e.Lock()
defer e.Unlock()
e.mu.Lock()
defer e.mu.Unlock()
e.m[name] = content
}

Expand Down
8 changes: 8 additions & 0 deletions codecov.yml
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
12 changes: 6 additions & 6 deletions color.go
Expand Up @@ -212,32 +212,32 @@ func (c Color) Printfln(format string, a ...interface{}) *TextPrinter {
// PrintOnError prints every error which is not nil.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p Color) PrintOnError(a ...interface{}) *TextPrinter {
func (c Color) PrintOnError(a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(err)
c.Println(err)
}
}
}

tp := TextPrinter(p)
tp := TextPrinter(c)
return &tp
}

// PrintOnErrorf wraps every error which is not nil and prints it.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p Color) PrintOnErrorf(format string, a ...interface{}) *TextPrinter {
func (c Color) PrintOnErrorf(format string, a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(fmt.Errorf(format, err))
c.Println(fmt.Errorf(format, err))
}
}
}

tp := TextPrinter(p)
tp := TextPrinter(c)
return &tp
}

Expand Down
6 changes: 4 additions & 2 deletions go.mod
Expand Up @@ -3,9 +3,11 @@ module github.com/pterm/pterm
go 1.15

require (
github.com/MarvinJWendt/testza v0.3.5
github.com/atomicgo/cursor v0.0.1
atomicgo.dev/cursor v0.1.1
atomicgo.dev/keyboard v0.2.8
github.com/MarvinJWendt/testza v0.4.2
github.com/gookit/color v1.5.0
github.com/lithammer/fuzzysearch v1.1.5
github.com/mattn/go-runewidth v0.0.13
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)