Skip to content

Commit

Permalink
refactor: ♻️ may improve initiation speed of shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
Equationzhao committed Jan 31, 2024
1 parent d0fd525 commit aa7c2ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
18 changes: 4 additions & 14 deletions internal/cli/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,11 @@ func init() {
Name: "init",
Usage: `show the init script for shell, support zsh, bash, fish, powershell, nushell`,
Action: func(context *cli.Context, s string) error {
shell.Init()
switch s {
case "zsh":
_, _ = fmt.Print(string(shell.ZSHContent))
case "bash":
_, _ = fmt.Print(string(shell.BASHContent))
case "fish":
_, _ = fmt.Print(string(shell.FISHContent))
case "powershell", "pwsh":
_, _ = fmt.Print(string(shell.PSContent))
case "nushell", "nu":
_, _ = fmt.Print(string(shell.NUContent))
default:
return fmt.Errorf("unsupported shell: %s \n %s[zsh|bash|fish|powershell|nushell]", s, constval.Success)
init, err := shell.Init(s)
if err != nil {
return err
}
_, _ = fmt.Println(init)
return Err4Exit{}
},
Category: "SHELL",
Expand Down
25 changes: 18 additions & 7 deletions internal/shell/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package shell
import (
"bytes"
_ "embed"
"fmt"

constval "github.com/Equationzhao/g/internal/const"
)

//go:embed g.ps1
Expand All @@ -20,11 +23,19 @@ var FISHContent []byte
//go:embed g.nu
var NUContent []byte

func Init() {
// replace os newline with unix newline
PSContent = bytes.ReplaceAll(PSContent, []byte("\r\n"), []byte("\n"))
BASHContent = bytes.ReplaceAll(BASHContent, []byte("\r\n"), []byte("\n"))
ZSHContent = bytes.ReplaceAll(ZSHContent, []byte("\r\n"), []byte("\n"))
FISHContent = bytes.ReplaceAll(FISHContent, []byte("\r\n"), []byte("\n"))
NUContent = bytes.ReplaceAll(NUContent, []byte("\r\n"), []byte("\n"))
func Init(shell string) (string, error) {
switch shell {
case "zsh":
return string(bytes.ReplaceAll(ZSHContent, []byte("\r\n"), []byte("\n"))), nil
case "bash":
return string(bytes.ReplaceAll(BASHContent, []byte("\r\n"), []byte("\n"))), nil
case "fish":
return string(bytes.ReplaceAll(FISHContent, []byte("\r\n"), []byte("\n"))), nil
case "powershell", "pwsh":
return string(bytes.ReplaceAll(PSContent, []byte("\r\n"), []byte("\n"))), nil
case "nushell", "nu":
return string(bytes.ReplaceAll(FISHContent, []byte("\r\n"), []byte("\n"))), nil
// replace os newline with unix newline
}
return "", fmt.Errorf("unsupported shell: %s \n %s[zsh|bash|fish|powershell|nushell]", shell, constval.Success)
}

0 comments on commit aa7c2ae

Please sign in to comment.