Skip to content

Commit

Permalink
up: cflag - update some util func, add some new files
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 27, 2022
1 parent 88afe8e commit ae3ee9f
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 2 deletions.
135 changes: 135 additions & 0 deletions cflag/ext.go
@@ -0,0 +1,135 @@
package cflag

import (
"fmt"
"strconv"

"github.com/gookit/goutil/strutil"
)

/*************************************************************************
* options: some special flag vars
* - implemented flag.Value interface
*************************************************************************/

// Ints The int flag list, implemented flag.Value interface
type Ints []int

// String to string
func (s *Ints) String() string {
return fmt.Sprintf("%v", *s)
}

// Set new value
func (s *Ints) Set(value string) error {
intVal, err := strconv.Atoi(value)
if err == nil {
*s = append(*s, intVal)
}

return err
}

// Strings The string flag list, implemented flag.Value interface
type Strings []string

// String to string
func (s *Strings) String() string {
return fmt.Sprintf("%v", *s)
}

// Set new value
func (s *Strings) Set(value string) error {
*s = append(*s, value)
return nil
}

// Booleans The bool flag list, implemented flag.Value interface
type Booleans []bool

// String to string
func (s *Booleans) String() string {
return fmt.Sprintf("%v", *s)
}

// Set new value
func (s *Booleans) Set(value string) error {
boolVal, err := strconv.ParseBool(value)
if err == nil {
*s = append(*s, boolVal)
}

return err
}

// EnumString The string flag list, implemented flag.Value interface
type EnumString struct {
val string
enum []string
}

// String to string
func (s *EnumString) String() string {
return s.val
}

// SetEnum values
func (s *EnumString) SetEnum(enum []string) {
s.enum = enum
}

// Set new value, will check value is right
func (s *EnumString) Set(value string) error {
var ok bool
for _, item := range s.enum {
if value == item {
ok = true
break
}
}

if !ok {
return fmt.Errorf("value must one of the: %v", s.enum)
}
return nil
}

// String a special string
//
// Usage:
//
// // case 1:
// var names gcli.String
// c.VarOpt(&names, "names", "", "multi name by comma split")
//
// --names "tom,john,joy"
// names.Split(",") -> []string{"tom","john","joy"}
//
// // case 2:
// var ids gcli.String
// c.VarOpt(&ids, "ids", "", "multi id by comma split")
//
// --names "23,34,56"
// names.Ints(",") -> []int{23,34,56}
type String string

// Set value
func (s *String) Set(val string) error {
*s = String(val)
return nil
}

// String to string
func (s *String) String() string {
return string(*s)
}

// Split value to []string
func (s *String) Split(sep string) []string {
return strutil.ToStrings(string(*s), sep)
}

// Ints value to []int
func (s *String) Ints(sep string) []int {
return strutil.Ints(string(*s), sep)
}
24 changes: 22 additions & 2 deletions cflag/util.go
Expand Up @@ -3,7 +3,10 @@ package cflag
import (
"flag"
"reflect"
"regexp"
"strings"

"github.com/gookit/color"
)

// IsZeroValue determines whether the string represents the zero
Expand Down Expand Up @@ -88,14 +91,31 @@ func IsFlagHelpErr(err error) bool {
return err == flag.ErrHelp
}

// regex: "`[\w ]+`"
// regex: "`.+`"
var codeReg = regexp.MustCompile("`" + `.+` + "`")

// WrapColorForCode WrapColorForCode. convert "hello `keywords`" to "hello <mga>keywords</>"
func WrapColorForCode(s string) string {
if !strings.ContainsRune(s, '`') {
return s
}

return codeReg.ReplaceAllStringFunc(s, func(code string) string {
code = strings.Trim(code, "`")
return color.WrapTag(code, "mga")
})
}

// ParseStopMark string
const ParseStopMark = "--"

// ReplaceShorts replace shorts to full option. will stop on ParseStopMark
//
// For example:
// eg: '-f' -> '--file'.
// eg: '-n=tom' -> '--name=tom'.
//
// eg: '-f' -> '--file'.
// eg: '-n=tom' -> '--name=tom'.
func ReplaceShorts(args []string, shortsMap map[string]string) []string {
if len(args) == 0 {
return args
Expand Down
78 changes: 78 additions & 0 deletions cliutil/termctrl/keyboard.go
@@ -0,0 +1,78 @@
package termctrl

// Giant list of key constants.
//
// Everything above KeyUnknown matches an actual ASCII key value.
// After that, we have various pseudo-keys in order to
// represent complex byte sequences that correspond to keys like Page up, Right
// arrow, etc.
const (
KeyCharA = 1 + iota
KeyCharB
KeyCharC
KeyCharD
KeyCharE
KeyCharF
KeyCharG
KeyCharH
KeyCharI
KeyCharJ
KeyCharK
KeyCharL
KeyCharM
KeyCharN
KeyCharO
KeyCharP
KeyCharQ
KeyCharR
KeyCharS
KeyCharT
KeyCharU
KeyCharV
KeyCharW
KeyCharX
KeyCharY
KeyCharZ
KeyEscape

KeyLeftBracket = '['
KeyRightBracket = ']'
KeyEnter = '\r'
KeyBackspace = 127

KeyUnknown = 0xd800 /* UTF-16 surrogate area */ + iota
KeyUp
KeyDown
KeyLeft
KeyRight
KeyHome
KeyEnd
KeyPasteStart
KeyPasteEnd
KeyInsert
KeyDelete
KeyPgUp
KeyPgDn
KeyPause
KeyF1
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyF10
KeyF11
KeyF12
)

// TODO ...
const (
KeyCtrl = ' '
KeyAlt = ' '
)

var PasteStart = []byte{KeyEscape, '[', '2', '0', '0', '~'}
var PasteEnd = []byte{KeyEscape, '[', '2', '0', '1', '~'}
1 change: 1 addition & 0 deletions cliutil/termctrl/movecursor.go
@@ -0,0 +1 @@
package termctrl

0 comments on commit ae3ee9f

Please sign in to comment.