Skip to content

Commit

Permalink
feat(cmd): alias performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
drwicid authored and JanDeDobbeleer committed Dec 23, 2024
1 parent 07b1418 commit f83131e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/shell/aliae.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (a Aliae) Render() {
DotFile.WriteString("\n\n")
}

if first && context.Current.Shell == CMD {
DotFile.WriteString(cmdAliasPre())
}

if !first {
DotFile.WriteString("\n")
}
Expand All @@ -101,4 +105,8 @@ func (a Aliae) Render() {

first = false
}

if context.Current.Shell == CMD {
DotFile.WriteString(cmdAliasPost())
}
}
7 changes: 3 additions & 4 deletions src/shell/aliae_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ func TestAliasCommand(t *testing.T) {
Expected: "Set-Alias -Name foo -Value bar",
},
{
Case: "CMD",
Shell: CMD,
Expected: `local p = assert(io.popen("doskey foo=bar"))
p:close()`,
Case: "CMD",
Shell: CMD,
Expected: "macrofile:write(\"foo=bar\", \"\\n\")",
},
{
Case: "FISH",
Expand Down
18 changes: 16 additions & 2 deletions src/shell/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ const (

func (a *Alias) cmd() *Alias {
if a.Type == Command {
a.template = `local p = assert(io.popen("doskey {{ .Name }}={{ escapeString .Value }}"))
p:close()`
a.template = "macrofile:write(\"{{ .Name }}={{ escapeString .Value }}\", \"\\n\")"
}

return a
}

func cmdAliasPre() string {
return `
local filename = os.tmpname()
local macrofile = io.open(filename, "w+")
`
}

func cmdAliasPost() string {
return `
macrofile:close()
local _ = io.popen(string.format("doskey /macrofile=%s", filename)):close()
os.remove(filename)
`
}

func (e *Echo) cmd() *Echo {
e.template = `message = [[
{{ .Message }}
Expand Down

0 comments on commit f83131e

Please sign in to comment.