Skip to content

Commit

Permalink
Merge pull request #360 from pterm/358-implement-interactive-printers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jun 20, 2022
2 parents e643d81 + ae51bcb commit 53db1bf
Show file tree
Hide file tree
Showing 26 changed files with 1,455 additions and 21 deletions.
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
)

0 comments on commit 53db1bf

Please sign in to comment.