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

Version 13.9.2 #516

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### [13.9.2](https://kaos.sh/ek/13.9.2)

- `[knf]` Added helper `Q`
- `[fmtc]` Code refactoring
- `[usage]` Code refactoring

### [13.9.1](https://kaos.sh/ek/13.9.1)

- `[errors]` Fixed bug with extra newline character at the end of `Error` output
Expand Down
46 changes: 22 additions & 24 deletions fmtc/cond_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ import "io"

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

type CondWrapper struct {
match bool
}
type CondWrapper bool

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

// If returns wrapper for printing messages if condition is true
func If(cond bool) CondWrapper {
return CondWrapper{cond}
return CondWrapper(cond)
}

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

// Print formats using the default formats for its operands and writes to standard
// output.
func (cw CondWrapper) Print(a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -38,7 +36,7 @@ func (cw CondWrapper) Print(a ...any) (int, error) {
// output. Spaces are always added between operands and a newline is appended. It
// returns the number of bytes written and any write error encountered.
func (cw CondWrapper) Println(a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -48,7 +46,7 @@ func (cw CondWrapper) Println(a ...any) (int, error) {
// Printf formats according to a format specifier and writes to standard output. It
// returns the number of bytes written and any write error encountered.
func (cw CondWrapper) Printf(f string, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -59,7 +57,7 @@ func (cw CondWrapper) Printf(f string, a ...any) (int, error) {
// Spaces are added between operands when neither is a string. It returns the
// number of bytes written and any write error encountered.
func (cw CondWrapper) Fprint(w io.Writer, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -70,7 +68,7 @@ func (cw CondWrapper) Fprint(w io.Writer, a ...any) (int, error) {
// Spaces are always added between operands and a newline is appended. It returns
// the number of bytes written and any write error encountered.
func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -80,7 +78,7 @@ func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (int, error) {
// Fprintf formats according to a format specifier and writes to w. It returns
// the number of bytes written and any write error encountered.
func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -90,7 +88,7 @@ func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error) {
// Sprint formats using the default formats for its operands and returns the
// resulting string. Spaces are added between operands when neither is a string.
func (cw CondWrapper) Sprint(a ...any) string {
if !cw.match {
if !cw {
return ""
}

Expand All @@ -100,7 +98,7 @@ func (cw CondWrapper) Sprint(a ...any) string {
// Sprintf formats according to a format specifier and returns the resulting
// string.
func (cw CondWrapper) Sprintf(f string, a ...any) string {
if !cw.match {
if !cw {
return ""
}

Expand All @@ -111,7 +109,7 @@ func (cw CondWrapper) Sprintf(f string, a ...any) string {
// resulting string. Spaces are always added between operands and a newline is
// appended.
func (cw CondWrapper) Sprintln(a ...any) string {
if !cw.match {
if !cw {
return ""
}

Expand All @@ -120,7 +118,7 @@ func (cw CondWrapper) Sprintln(a ...any) string {

// TPrint removes all content on the current line and prints the new message
func (cw CondWrapper) TPrint(a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -129,7 +127,7 @@ func (cw CondWrapper) TPrint(a ...any) (int, error) {

// TPrintf removes all content on the current line and prints the new message
func (cw CondWrapper) TPrintf(f string, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -139,7 +137,7 @@ func (cw CondWrapper) TPrintf(f string, a ...any) (int, error) {
// TPrintln removes all content on the current line and prints the new message
// with a new line symbol at the end
func (cw CondWrapper) TPrintln(a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -149,7 +147,7 @@ func (cw CondWrapper) TPrintln(a ...any) (int, error) {
// LPrint formats using the default formats for its operands and writes to standard
// output limited by the text size
func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -159,7 +157,7 @@ func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error) {
// LPrintf formats according to a format specifier and writes to standard output
// limited by the text size
func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -169,7 +167,7 @@ func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error) {
// LPrintln formats using the default formats for its operands and writes to standard
// output limited by the text size
func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -179,7 +177,7 @@ func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error) {
// TLPrint removes all content on the current line and prints the new message
// limited by the text size
func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -189,7 +187,7 @@ func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error) {
// TLPrintf removes all content on the current line and prints the new message
// limited by the text size
func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -199,7 +197,7 @@ func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error) {
// TPrintln removes all content on the current line and prints the new message
// limited by the text size with a new line symbol at the end
func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -208,7 +206,7 @@ func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (int, error) {

// NewLine prints a newline to standard output
func (cw CondWrapper) NewLine() (int, error) {
if !cw.match {
if !cw {
return 0, nil
}

Expand All @@ -217,7 +215,7 @@ func (cw CondWrapper) NewLine() (int, error) {

// Bell prints alert (bell) symbol
func (cw CondWrapper) Bell() {
if !cw.match {
if !cw {
return
}

Expand Down
3 changes: 3 additions & 0 deletions knf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func ExampleGlobal() {
// Read string value
GetS("main:string")

// Use helper Q to create full property name
GetS(Q("main", "string"))

// Read integer value
GetI("main:int")

Expand Down
6 changes: 6 additions & 0 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ func Validate(validators Validators) errors.Errors {
return global.Validate(validators)
}

// Q is a helper to create a valid full property name (section + delimiter
// + property name)
func Q(section, prop string) string {
return section + _SYMBOL_DELIMITER + prop
}

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

// Add adds given validators and returns new slice
Expand Down
4 changes: 4 additions & 0 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,10 @@ func (s *KNFSuite) TestKNFParserExceptions(c *check.C) {
c.Assert(err.Error(), check.Equals, "Error at line 3: Unknown property {abcd:test}")
}

func (s *KNFSuite) TestHelpers(c *check.C) {
c.Assert(Q("section", "prop"), check.Equals, "section:prop")
}

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

func (s *KNFSuite) BenchmarkBasic(c *check.C) {
Expand Down
48 changes: 28 additions & 20 deletions usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ func NewInfo(args ...string) *Info {

// AddGroup adds new command group
func (i *Info) AddGroup(group string) {
if i == nil {
if i == nil || group == "" {
return
}

i.curGroup = group
}

// AddCommand adds command (name, description, args)
func (i *Info) AddCommand(a ...string) *Command {
if i == nil || len(a) < 2 {
// AddCommand adds command
func (i *Info) AddCommand(name, desc string, args ...string) *Command {
if i == nil || name == "" || desc == "" {
return nil
}

Expand All @@ -198,9 +198,9 @@ func (i *Info) AddCommand(a ...string) *Command {
}

cmd := &Command{
Name: a[0],
Desc: a[1],
Args: a[2:],
Name: name,
Desc: desc,
Args: args,
Group: group,
info: i,
}
Expand All @@ -211,17 +211,17 @@ func (i *Info) AddCommand(a ...string) *Command {
}

// AddOption adds option (name, description, args)
func (i *Info) AddOption(a ...string) *Option {
if i == nil || len(a) < 2 {
func (i *Info) AddOption(name, desc string, args ...string) *Option {
if i == nil || name == "" || desc == "" {
return nil
}

long, short := parseOptionName(a[0])
long, short := parseOptionName(name)
opt := &Option{
Long: long,
Short: short,
Desc: a[1],
Arg: strings.Join(a[2:], " "),
Desc: desc,
Arg: strings.Join(args, " "),
info: i,
}

Expand All @@ -231,25 +231,33 @@ func (i *Info) AddOption(a ...string) *Option {
}

// AddExample adds example of application usage
func (i *Info) AddExample(a ...string) {
if i == nil || len(a) == 0 {
func (i *Info) AddExample(cmd string, desc ...string) {
if i == nil || cmd == "" {
return
}

a = append(a, "")
var cmdDesc string

if len(desc) != 0 {
cmdDesc = desc[0]
}

i.Examples = append(i.Examples, &Example{a[0], a[1], false, i})
i.Examples = append(i.Examples, &Example{cmd, cmdDesc, false, i})
}

// AddRawExample adds example of application usage without command prefix
func (i *Info) AddRawExample(a ...string) {
if i == nil || len(a) == 0 {
func (i *Info) AddRawExample(cmd string, desc ...string) {
if i == nil || cmd == "" {
return
}

a = append(a, "")
var cmdDesc string

if len(desc) != 0 {
cmdDesc = desc[0]
}

i.Examples = append(i.Examples, &Example{a[0], a[1], true, i})
i.Examples = append(i.Examples, &Example{cmd, cmdDesc, true, i})
}

// AddSpoiler adds spoiler
Expand Down
16 changes: 9 additions & 7 deletions usage/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,28 @@ func (s *UsageSuite) TestUsage(c *C) {

info.AddSpoiler("This is usage of spoiler with {#240}c{#241}o{#242}l{#243}o{#244}r{#245}s {#246}s{#247}u{#248}p{#249}p{#250}o{#251}r{#252}t{!}")

info.AddCommand() // will be ignored
info.AddCommand("", "") // will be ignored
info.AddCommand("test", "") // will be ignored
info.AddCommand("print", "Print command")

info.AddGroup("Command group")

info.AddCommand("read")
info.AddCommand("read", "Read command")
info.AddCommand("read1", "Read command with arguments", "arg1", "arg2")
info.AddCommand("read2", "Read command with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg")

info.AddOption("t:test")
info.AddOption("", "") // will be ignored
info.AddOption("test", "") // will be ignored
info.AddOption("t:test", "Test option ")
info.AddOption("test1", "Test option with argument", "arg")
info.AddOption("test2", "Test option with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg")

info.BoundOptions("read", "t:test", "test1")

info.AddExample() // will be ignored
info.AddExample("") // will be ignored
info.AddExample("abc")
info.AddExample("abc", "Example with very long description that not fits default 88 symbols limit and link https://domain.com/#EC103814B9CCB1E305CE20D6A25E681D3735D2301D5BB631B8DFA0ABB2394A99631B8DFA0ABB2394A99")
info.AddRawExample() // will be ignored
info.AddRawExample("") // will be ignored
info.AddRawExample("echo 123 | myapp")
info.AddRawExample("echo 123 | myapp", "Example with description")

Expand Down Expand Up @@ -184,9 +185,10 @@ func (s *UsageSuite) TestVersionInfo(c *C) {

func (s *UsageSuite) TestNils(c *C) {
var i *Info

c.Assert(func() { i.AddGroup("test") }, NotPanics)
c.Assert(func() { i.AddCommand("test") }, NotPanics)
c.Assert(func() { i.AddOption("test") }, NotPanics)
c.Assert(func() { i.AddCommand("test", "test") }, NotPanics)
c.Assert(func() { i.AddOption("test", "test") }, NotPanics)
c.Assert(func() { i.AddExample("test") }, NotPanics)
c.Assert(func() { i.AddRawExample("test") }, NotPanics)
c.Assert(func() { i.AddSpoiler("test") }, NotPanics)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.9.1"
const VERSION = "13.9.2"