-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move the HelpVars to sub pkg helper
- Loading branch information
Showing
4 changed files
with
104 additions
and
90 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
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,66 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
/************************************************************* | ||
* app/cmd help vars | ||
*************************************************************/ | ||
|
||
// HelpVarFormat allow var replace on render help info. | ||
// | ||
// Default support: | ||
// | ||
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}" | ||
const HelpVarFormat = "{$%s}" | ||
|
||
// HelpVars struct. provide string var function for render help template. | ||
type HelpVars struct { | ||
VarOpen, VarClose string | ||
|
||
// Vars you can add some vars map for render help info | ||
Vars map[string]string | ||
} | ||
|
||
// AddVar get command name | ||
func (hv *HelpVars) AddVar(name, value string) { | ||
if hv.Vars == nil { | ||
hv.Vars = make(map[string]string) | ||
} | ||
|
||
hv.Vars[name] = value | ||
} | ||
|
||
// AddVars add multi tpl vars | ||
func (hv *HelpVars) AddVars(vars map[string]string) { | ||
for n, v := range vars { | ||
hv.AddVar(n, v) | ||
} | ||
} | ||
|
||
// GetVar get a help var by name | ||
func (hv *HelpVars) GetVar(name string) string { | ||
return hv.Vars[name] | ||
} | ||
|
||
// GetVars get all tpl vars | ||
func (hv *HelpVars) GetVars() map[string]string { | ||
return hv.Vars | ||
} | ||
|
||
// ReplaceVars replace vars in the input string. | ||
func (hv *HelpVars) ReplaceVars(input string) string { | ||
// if not use var | ||
if !strings.Contains(input, "{$") { | ||
return input | ||
} | ||
|
||
var ss []string | ||
for n, v := range hv.Vars { | ||
ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v) | ||
} | ||
|
||
return strings.NewReplacer(ss...).Replace(input) | ||
} |
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,32 @@ | ||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/gcli/v3/helper" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestHelpVars(t *testing.T) { | ||
is := assert.New(t) | ||
vs := helper.HelpVars{ | ||
Vars: map[string]string{ | ||
"key0": "val0", | ||
"key1": "val1", | ||
}, | ||
} | ||
|
||
is.Len(vs.GetVars(), 2) | ||
is.Contains(vs.GetVars(), "key0") | ||
|
||
vs.AddVars(map[string]string{"key2": "val2"}) | ||
vs.AddVar("key3", "val3") | ||
|
||
is.Eq("val3", vs.GetVar("key3")) | ||
is.Eq("", vs.GetVar("not-exist")) | ||
|
||
is.Eq("hello val0", vs.ReplaceVars("hello {$key0}")) | ||
is.Eq("hello val0 val2", vs.ReplaceVars("hello {$key0} {$key2}")) | ||
// invalid input | ||
is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}")) | ||
} |
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