Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: cflag - update some util func, add some new files
- Loading branch information
Showing
4 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', '~'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package termctrl |