Skip to content

Commit

Permalink
Merge pull request #461 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.118.0
  • Loading branch information
andyone authored Apr 27, 2024
2 parents 5a42e8b + a6468b9 commit 0cc25ed
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 46 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ jobs:
name: Typos
runs-on: ubuntu-latest

needs: SendCoverage

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog

### 12.118.0

- `[terminal/input]` Added method `SetHistoryCapacity`
- `[options]` Improved `Errors.String` output formatting
- `[terminal/input]` Added more usage examples
- `[terminal/input]` Code refactoring

### 12.117.0

- `[terminal/input]` Added new package for working with user input
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.117.0"
const VERSION = "12.118.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/depsy v1.1.0
github.com/essentialkaos/go-linenoise/v3 v3.4.0
github.com/essentialkaos/go-linenoise/v3 v3.6.0
golang.org/x/crypto v0.22.0
golang.org/x/sys v0.19.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1n
github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0=
github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI=
github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8=
github.com/essentialkaos/go-linenoise/v3 v3.4.0 h1:g72w8x+/HIwOMBVvNaPYp+wMWVHrYZwzFAF7OfZR5Ts=
github.com/essentialkaos/go-linenoise/v3 v3.4.0/go.mod h1:t1kNLY2bSMQCy1JXOefD2BDLs/TTPMtTv3DFNV5uDSI=
github.com/essentialkaos/go-linenoise/v3 v3.6.0 h1:deLcrodtLIkcHjNyW/MoQpjznXPVqvwlspxk7s/5YeY=
github.com/essentialkaos/go-linenoise/v3 v3.6.0/go.mod h1:Fi6kLdZdURkXHpRkIiX2nFGORNv81CXTZ2Mn72i/cn0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
8 changes: 6 additions & 2 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,12 @@ func (e Errors) String() string {

b := &strings.Builder{}

for _, err := range e {
fmt.Fprintf(b, " - %s\n", err.Error())
for i, err := range e {
fmt.Fprintf(b, " - %s", err.Error())

if i != len(e)-1 {
b.WriteRune('\n')
}
}

return b.String()
Expand Down
13 changes: 11 additions & 2 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package options
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -396,9 +397,7 @@ func (s *OptUtilSuite) TestParsing(c *C) {
_, errs = NewOptions().Parse([]string{"--test", "100"}, Map{"t:test": {Type: 10}})

c.Assert(errs, Not(HasLen), 0)
c.Assert(errs.IsEmpty(), Equals, false)
c.Assert(errs[0], ErrorMatches, `Option "--test" has unsupported type`)
c.Assert(errs.String(), Equals, " - Option \"--test\" has unsupported type\n")

// //////////////////////////////////////////////////////////////////////////////// //

Expand Down Expand Up @@ -527,6 +526,16 @@ func (s *OptUtilSuite) TestParsing(c *C) {
c.Assert(errs[0], Equals, ErrEmptyName)
}

func (s *OptUtilSuite) TestErrors(c *C) {
errs := Errors{
fmt.Errorf(`Option "--test" has unsupported type`),
fmt.Errorf(`Option "--dump" has unsupported type`),
}

c.Assert(errs.IsEmpty(), Equals, false)
c.Assert(errs.String(), Equals, " - Option \"--test\" has unsupported type\n - Option \"--dump\" has unsupported type")
}

func (s *OptUtilSuite) TestFormat(c *C) {
c.Assert(Format(""), Equals, "")
c.Assert(Format("test"), Equals, "--test")
Expand Down
126 changes: 117 additions & 9 deletions terminal/input/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package input
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import "fmt"
import (
"fmt"
"strings"
)

// ////////////////////////////////////////////////////////////////////////////////// //

Expand All @@ -16,20 +19,22 @@ func ExampleRead() {
input, err := Read("Please enter user name", true)

if err != nil {
fmt.Printf("Error: %v", err)
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("User name: %s\v", input)
fmt.Printf("User name: %s\n", input)

// You can read user input without providing any title
fmt.Println("Please enter user name")
input, err = Read("", true)

if err != nil {
fmt.Printf("Error: %v", err)
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("User name: %s\v", input)
fmt.Printf("User name: %s\n", input)
}

func ExampleReadPassword() {
Expand All @@ -41,10 +46,11 @@ func ExampleReadPassword() {
password, err := ReadPassword("Please enter password", true)

if err != nil {
fmt.Printf("Error: %v", err)
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("User password: %s\v", password)
fmt.Printf("User password: %s\n", password)
}

func ExampleReadPasswordSecure() {
Expand All @@ -56,10 +62,11 @@ func ExampleReadPasswordSecure() {
password, err := ReadPasswordSecure("Please enter password", true)

if err != nil {
fmt.Printf("Error: %v", err)
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("User password: %s\v", string(password.Data))
fmt.Printf("User password: %s\n", string(password.Data))

password.Destroy()
}
Expand All @@ -77,3 +84,104 @@ func ExampleReadAnswer() {
fmt.Println("File removed")
}
}

func ExampleAddHistory() {
input, err := Read("Please enter user name", true)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

// Save entered value to the input history
AddHistory(input)

fmt.Printf("User name: %s\n", input)
}

func ExampleSetHistoryCapacity() {
input, err := Read("Please enter user name", true)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

// Limit history size to last 3 entries
SetHistoryCapacity(3)

// Save entered value to the input history
AddHistory(input)

fmt.Printf("User name: %s\n", input)
}

func ExampleSetCompletionHandler() {
commands := []string{"add", "delete", "search", "help", "quit"}

SetCompletionHandler(func(input string) []string {
var result []string

for _, c := range commands {
if strings.HasPrefix(c, input) {
result = append(result, c)
}
}

return result
})

SetHintHandler(func(input string) string {
for _, c := range commands {
if strings.HasPrefix(c, input) {
return c[len(input):]
}
}

return ""
})

input, err := Read("Please enter command", true)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("Command: %s\n", input)
}

func ExampleSetHintHandler() {
commands := []string{"add", "delete", "search", "help", "quit"}

SetCompletionHandler(func(input string) []string {
var result []string

for _, c := range commands {
if strings.HasPrefix(c, input) {
result = append(result, c)
}
}

return result
})

SetHintHandler(func(input string) string {
for _, c := range commands {
if strings.HasPrefix(c, input) {
return c[len(input):]
}
}

return ""
})

input, err := Read("Please enter command", true)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("Command: %s\n", input)
}
39 changes: 26 additions & 13 deletions terminal/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// CompletionHandler is completion handler
type CompletionHandler = func(input string) []string

// HintHandler is hint handler
type HintHandler = func(input string) string

// ////////////////////////////////////////////////////////////////////////////////// //

// ErrKillSignal is error type when user cancel input
var ErrKillSignal = linenoise.ErrKillSignal

// ////////////////////////////////////////////////////////////////////////////////// //

// Prompt is prompt string
var Prompt = "> "

Expand All @@ -46,13 +56,11 @@ var HideLength = false
// custom masking symbol, so it always will be an asterisk (*).
var HidePassword = false

var (
// MaskSymbolColorTag is fmtc color tag used for MaskSymbol output
MaskSymbolColorTag = ""
// MaskSymbolColorTag is fmtc color tag used for MaskSymbol output
var MaskSymbolColorTag = ""

// TitleColorTag is fmtc color tag used for input titles
TitleColorTag = "{s}"
)
// TitleColorTag is fmtc color tag used for input titles
var TitleColorTag = "{s}"

// AlwaysYes is a flag, if set ReadAnswer will always return true (useful for working
// with option for forced actions)
Expand All @@ -64,12 +72,12 @@ var tmux int8

// ////////////////////////////////////////////////////////////////////////////////// //

// Read reads user's input
// Read reads user input
func Read(title string, nonEmpty bool) (string, error) {
return readUserInput(title, nonEmpty, false)
}

// ReadAnswer reads user's answer for yes/no question
// ReadAnswer reads user's answer to yes/no question
func ReadAnswer(title string, defaultAnswers ...string) (bool, error) {
var defaultAnswer string

Expand Down Expand Up @@ -109,13 +117,13 @@ func ReadAnswer(title string, defaultAnswers ...string) (bool, error) {
}
}

// ReadPassword reads password or some private input which will be hidden
// ReadPassword reads password or some private input that will be hidden
// after pressing Enter
func ReadPassword(title string, nonEmpty bool) (string, error) {
return readUserInput(title, nonEmpty, true)
}

// ReadPasswordSecure reads password or some private input which will be hidden
// ReadPasswordSecure reads password or some private input that will be hidden
// after pressing Enter
func ReadPasswordSecure(title string, nonEmpty bool) (*secstr.String, error) {
password, err := readUserInput(title, nonEmpty, true)
Expand All @@ -132,13 +140,18 @@ func AddHistory(data string) {
linenoise.AddHistory(data)
}

// SetCompletionHandler adds function for autocompletion
func SetCompletionHandler(h func(input string) []string) {
// SetHistoryCapacity sets maximum capacity of history
func SetHistoryCapacity(capacity int) error {
return linenoise.SetHistoryCapacity(capacity)
}

// SetCompletionHandler adds autocompletion function (using Tab key)
func SetCompletionHandler(h CompletionHandler) {
linenoise.SetCompletionHandler(h)
}

// SetHintHandler adds function for input hints
func SetHintHandler(h func(input string) string) {
func SetHintHandler(h HintHandler) {
linenoise.SetHintHandler(h)
}

Expand Down
Loading

0 comments on commit 0cc25ed

Please sign in to comment.