Skip to content

Commit

Permalink
[terminal] New features
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 14, 2023
1 parent e3fac2e commit 9d9441e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

### 12.70.0

* `[terminal]` Added method `Info` for showing informative messages
* `[terminal]` Added method `Panel` for showing panel with custom label, title, and message
* `[terminal]` Added method `ErrorPanel` for showing panel with error message
* `[terminal]` Added method `WarnPanel` for showing panel with warning message
* `[terminal]` Added method `InfoPanel` for showing panel with informative message
* `[terminal]` Method `PrintErrorMessage` marked as deprecated (_use method `Error` instead_)
* `[terminal]` Method `PrintWarnMessage` marked as deprecated (_use method `Warn` instead_)

Expand Down
50 changes: 46 additions & 4 deletions terminal/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ExampleReadPassword() {
MaskSymbol = "•"
MaskSymbolColorTag = "{s}"

// User must enter password
// User must enter the password
password, err := ReadPassword("Please enter password", true)

if err != nil {
Expand All @@ -54,7 +54,7 @@ func ExampleReadPasswordSecure() {
MaskSymbol = "•"
MaskSymbolColorTag = "{s}"

// User must enter password
// User must enter the password
password, err := ReadPasswordSecure("Please enter password", true)

if err != nil {
Expand Down Expand Up @@ -108,10 +108,52 @@ func ExamplePrintActionStatus() {

func ExampleError() {
// Print red text to stderr
Error("Error while sending data")
Error("Error while sending data to %s", "https://example.com")
}

func ExampleWarn() {
// Print yellow text to stderr
Warn("Warning file is not found")
Warn("Warning file %s is not found", "/home/john/test.txt")
}

func ExampleInfo() {
// Print cyan text to stdout
Warn("User %q will be created automatically", "bob")
}

func ExampleErrorPanel() {
ErrorPanel(
"Can't send data to remote server.",
`{*}Lorem ipsum{!} dolor sit amet, {r*}consectetur{!} adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
)
}

func ExampleWarnPanel() {
WarnPanel(
"Can't find user bob on system.",
`{*}Lorem ipsum{!} dolor sit amet, {r*}consectetur{!} adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
)
}

func ExampleInfoPanel() {
InfoPanel(
"Auto-saving is enabled - data will be saved after editing.",
`{*}Lorem ipsum{!} dolor sit amet, {r*}consectetur{!} adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
)
}

func ExamplePanel() {
Panel(
"Yolo", "{m}",
"Excepteur sint occaecat cupidatat non proident.",
`{*}Lorem ipsum{!} dolor sit amet, {r*}consectetur{!} adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
)
}
73 changes: 59 additions & 14 deletions terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
"strings"
"unicode/utf8"

"github.com/essentialkaos/go-linenoise/v3"

"github.com/essentialkaos/ek/v12/ansi"
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/mathutil"
"github.com/essentialkaos/ek/v12/secstr"

"github.com/essentialkaos/go-linenoise/v3"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -40,23 +41,33 @@ var MaskSymbol = "*"
// HideLength is flag for hiding password length
var HideLength = false

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

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

// TitleColorTag is fmtc color tag used for input titles
var TitleColorTag = "{c}"
// ErrorColorTag is fmtc color tag used for error messages
ErrorColorTag = "{r}"

// ErrorColorTag is fmtc color tag used for error messages
var ErrorColorTag = "{r}"
// WarnColorTag is fmtc color tag used for warning messages
WarnColorTag = "{y}"

// WarnColorTag is fmtc color tag used for warning messages
var WarnColorTag = "{y}"
// InfoColorTag is fmtc color tag used for info messages
InfoColorTag = "{c-}"
)

// ErrorPrefix is prefix for error messages
var ErrorPrefix = ""
var (
// ErrorPrefix is prefix for error messages
ErrorPrefix = ""

// WarnPrefix is prefix for warning messages
var WarnPrefix = ""
// WarnPrefix is prefix for warning messages
WarnPrefix = ""

// InfoPrefix is prefix for info messages
InfoPrefix = ""
)

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

Expand Down Expand Up @@ -152,6 +163,40 @@ func Warn(message string, args ...any) {
}
}

// Info prints info message
func Info(message string, args ...any) {
if len(args) == 0 {
fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", message)
} else {
fmtc.Fprintf(os.Stdout, InfoPrefix+InfoColorTag+"%s{!}\n", fmt.Sprintf(message, args...))
}
}

// ErrorPanel shows panel with error message
func ErrorPanel(title, message string) {
Panel("ERROR", ErrorColorTag, title, message)
}

// WarnPanel shows panel with warning message
func WarnPanel(title, message string) {
Panel("WARNING", WarnColorTag, title, message)
}

// InfoPanel shows panel with warning message
func InfoPanel(title, message string) {
Panel("INFO", InfoColorTag, title, message)
}

// Panel show panel with given label, title, and message
func Panel(label, colorTag, title, message string) {
fmtc.Printf(colorTag+"{@*} %s {!} "+colorTag+"%s{!}\n", label, title)

fmtc.Print(fmtutil.Wrap(
fmtc.Sprint(message),
fmtc.Sprint(colorTag+"┃{!} "), 88,
) + "\n")
}

// AddHistory adds line to input history
func AddHistory(data string) {
linenoise.AddHistory(data)
Expand Down

0 comments on commit 9d9441e

Please sign in to comment.