Skip to content

feat: temlated shell command #1852

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ func (e *setExpr) eval(app *app, args []string) {
return
}
gOpts.shellopts = strings.Split(e.val, ":")
case "shellcmd":
if e.val == "" {
gOpts.shellcmd = nil
return
}
gOpts.shellcmd = tokenize(e.val)
case "sortby":
method := sortMethod(e.val)
if !isValidSortMethod(method) {
Expand Down
25 changes: 24 additions & 1 deletion misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,31 @@ func unescape(s string) string {
}

// This function splits the given string by whitespaces. It is aware of escaped
// whitespaces so that they are not split unintentionally.
// whitespaces and quoted strings so that they are not split unintentionally.
func tokenize(s string) []string {
quoted := false
esc := false
toks := strings.FieldsFunc(s, func(r rune) bool {
if r == '\'' || r == '"' || r == '`' {
quoted = !quoted
}
if esc {
esc = false
return false
}
if r == '\\' {
esc = true
return false
}
return !quoted && unicode.IsSpace(r)
})
if len(toks) == 0 {
toks = append(toks, s)
}
return toks
}

func tokenize_old(s string) []string {
esc := false
var buf []rune
var toks []string
Expand Down
3 changes: 2 additions & 1 deletion misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ func TestTokenize(t *testing.T) {

for _, test := range tests {
if got := tokenize(test.s); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
t.Errorf("at input '%#v' expected '%#v' but got '%#v'", test.s, test.exp, got)
}
}
}


func TestSplitWord(t *testing.T) {
tests := []struct {
s string
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var gOpts struct {
rulerfmt string
preserve []string
shellopts []string
shellcmd []string
keys map[string]expr
cmdkeys map[string]expr
cmds map[string]expr
Expand Down Expand Up @@ -240,6 +241,7 @@ func init() {
gOpts.rulerfmt = " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;34m %f \033[0m| %i/%t"
gOpts.preserve = []string{"mode"}
gOpts.shellopts = nil
gOpts.shellcmd = nil
gOpts.tempmarks = "'"
gOpts.numberfmt = "\033[33m"
gOpts.tagfmt = "\033[31m"
Expand Down
26 changes: 25 additions & 1 deletion os.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,40 @@ func detachedCommand(name string, arg ...string) *exec.Cmd {
return cmd
}

func shellCommand2(s string, args []string) *exec.Cmd {
var words []string
for _, word := range gOpts.shellcmd {
switch word {
case "%a":
words = append(words, args...)
case "%c":
words = append(words, s)
default:
words = append(words, word)
}
}
cmd := exec.Command(words[0], words[1:]...)
return cmd
}

func shellCommand(s string, args []string) *exec.Cmd {

if len(gOpts.ifs) != 0 {
s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s)
}

if len(gOpts.shellcmd) > 0 {
return shellCommand2(s, args)
}

// original legacy configuration which uses shell, shellopts and shellflag

args = append([]string{gOpts.shellflag, s, "--"}, args...)

args = append(gOpts.shellopts, args...)

return exec.Command(gOpts.shell, args...)
cmd := exec.Command(gOpts.shell, args...)
return cmd
}

func shellSetPG(cmd *exec.Cmd) {
Expand Down
Loading