diff --git a/.gitignore b/.gitignore index 565462b..aad2bdc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tmp/ _site/ Gemfile.lock .goxc.local.json +*.exe diff --git a/README.md b/README.md index 8cd8e43..2a0b46e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ This will define the scmpuff shell functions as well as some handy shortcuts. [fish]: https://fishshell.com/ +For Powershell on Windows, add the following to your `$PROFILE` file: + + scmpuff init --shell=pwsh | Out-String | Invoke-Expression + ## Usage diff --git a/commands/inits/data/aliases.ps1 b/commands/inits/data/aliases.ps1 new file mode 100644 index 0000000..a8067f4 --- /dev/null +++ b/commands/inits/data/aliases.ps1 @@ -0,0 +1,6 @@ +function gs { scmpuff_status } +function ga { git add } +function gd { git diff } +function gl { git log } +function gco { git checkout } +function grs { git reset } diff --git a/commands/inits/data/git_wrapper.ps1 b/commands/inits/data/git_wrapper.ps1 new file mode 100644 index 0000000..d9c24a5 --- /dev/null +++ b/commands/inits/data/git_wrapper.ps1 @@ -0,0 +1,19 @@ +$SCMPUFF_GIT_CMD = Get-Command git | Select-Object -ExpandProperty Definition + +function git { + switch -regex -casesensitive($args[0]) { + "^(commit|blame|log|rebase|merge)$" { + & scmpuff exec -- $SCMPUFF_GIT_CMD $args + } + "^(checkout|diff|rm|reset)$" { + & scmpuff exec --relative -- $SCMPUFF_GIT_CMD $args + } + "^add$" { + & scmpuff exec -- $SCMPUFF_GIT_CMD $args + scmpuff_status + } + default { + & $SCMPUFF_GIT_CMD $args + } + } +} diff --git a/commands/inits/data/status_shortcuts.ps1 b/commands/inits/data/status_shortcuts.ps1 new file mode 100644 index 0000000..eae3826 --- /dev/null +++ b/commands/inits/data/status_shortcuts.ps1 @@ -0,0 +1,42 @@ +function scmpuff_status { + $scmpuff_env_char = "e" + + # Run scmpuff status command and capture the output + $cmd_output = & scmpuff status --filelist @args + + # if there was an error, exit prematurely, and pass along the exit code + $es = $LastExitCode + if ($es -ne 0) { + return $es + } + + # Fetch list of files (from first line of script output) + $files = ($cmd_output | Select-Object -First 1) -split '\s+' + + # Export numbered env variables for each file + scmpuff_clear_vars + $e = 1 + foreach ($file in $files) { + Set-Item "env:$($scmpuff_env_char + $e)" $file + $e++ + } + + # Print status (from line two onward) + $cmd_output | Select-Object -Skip 1 +} + +# Clear numbered env variables +function scmpuff_clear_vars { + # Define the environment variable character + $scmpuff_env_char = "e" + + # Iterate through environment variables and unset those starting with 'e' + for ($i = 1; $i -le 999; $i++) { + $env_var_i = $scmpuff_env_char + $i + if (Get-Item "env:$($env_var_i)" -ErrorAction Ignore) { + Remove-Item "env:$env_var_i" + } else { + break + } + } +} diff --git a/commands/inits/init.go b/commands/inits/init.go index 5aaca0f..7369d95 100644 --- a/commands/inits/init.go +++ b/commands/inits/init.go @@ -53,6 +53,10 @@ There are a number of flags to customize the shell integration. fmt.Println(fishCollection.Output(wrapGit, includeAliases)) os.Exit(0) + case "pwsh": + fmt.Println(pwshCollection.Output(wrapGit, includeAliases)) + os.Exit(0) + default: fmt.Fprintf(os.Stderr, "Unrecognized shell '%s'\n", shellType) os.Exit(1) @@ -88,7 +92,7 @@ There are a number of flags to customize the shell integration. InitCmd.Flags().StringVarP( &shellType, "shell", "s", "", - "Output shell type: sh | bash | zsh | fish", + "Output shell type: sh | bash | zsh | fish | pwsh", ) InitCmd.Flag("shell").NoOptDefVal = defaultShellType() @@ -101,7 +105,7 @@ func defaultShellType() string { if shellenv, ok := os.LookupEnv("SHELL"); ok { base := filepath.Base(shellenv) switch base { - case "sh", "bash", "zsh", "fish": + case "sh", "bash", "zsh", "fish", "pwsh": return base } } diff --git a/commands/inits/scripts.go b/commands/inits/scripts.go index 6787679..d50a59c 100644 --- a/commands/inits/scripts.go +++ b/commands/inits/scripts.go @@ -11,15 +11,24 @@ var scriptStatusShortcuts string //go:embed data/status_shortcuts.fish var scriptStatusShortcutsFish string +//go:embed data/status_shortcuts.ps1 +var scriptStatusShortcutsPwsh string + //go:embed data/aliases.sh var scriptAliases string +//go:embed data/aliases.ps1 +var scriptAliasesPwsh string + //go:embed data/git_wrapper.sh var scriptGitWrapper string //go:embed data/git_wrapper.fish var scriptGitWrapperFish string +//go:embed data/git_wrapper.ps1 +var scriptGitWrapperPwsh string + type scriptCollection struct { statusShortcuts string gitWrapper string @@ -38,6 +47,12 @@ var fishCollection = scriptCollection{ aliases: scriptAliases, } +var pwshCollection = scriptCollection{ + statusShortcuts: scriptStatusShortcutsPwsh, + gitWrapper: scriptGitWrapperPwsh, + aliases: scriptAliasesPwsh, +} + func (sc scriptCollection) Output(wrapGit, aliases bool) string { var b strings.Builder b.WriteString(sc.statusShortcuts) diff --git a/go.mod b/go.mod index c7cea41..bb5d0a7 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,9 @@ module github.com/mroth/scmpuff go 1.18 -require github.com/spf13/cobra v1.4.0 +require github.com/spf13/cobra v1.8.1 require ( - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 0dd8697..912390a 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= -github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=