feat: initialize pwsh starship style

This commit is contained in:
Jan De Dobbeleer 2020-12-22 19:31:20 +01:00 committed by Jan De Dobbeleer
parent 597a2a0d96
commit a1b2b8d67b
8 changed files with 152 additions and 2 deletions

View file

@ -26,6 +26,10 @@ jobs:
go-version: 1.15.x
- name: Checkout code
uses: actions/checkout@v2
- name: Bundle init scripts
run: |
go get -u github.com/kevinburke/go-bindata/...
go-bindata -o init.go init/
- name: Golang CI
uses: golangci/golangci-lint-action@v2
with:

View file

@ -66,6 +66,10 @@ jobs:
go-version: 1.15.x
- name: Checkout code
uses: actions/checkout@v2
- name: Bundle init scripts
run: |
go get -u github.com/kevinburke/go-bindata/...
go-bindata -o init.go init/
- name: Build
id: build
run: go build -o ${{ matrix.ARTIFACT }} -ldflags="-X 'main.Version=${{ needs.release.outputs.version }}'"

2
.gitignore vendored
View file

@ -9,6 +9,8 @@
*.dll
*.so
*.dylib
# Initialization scripts generated by https://github.com/kevinburke/go-bindata
init.go
# Test binary, built with `go test -c`
*.test

View file

@ -49,6 +49,7 @@ a modern and more efficient tool was needed to suit my personal needs.
* [Robby Russel][oh-my-zsh] for creating oh-my-zsh, without him this would probably not be here
* [Janne Mareike Koschinski][justjanne] for providing information on how to get certain information
using Go (and the amazing [README][powerline-go])
* [Starship][starship] for creating an amazing way to initialize the prompt
[release-status]: https://github.com/jandedobbeleer/oh-my-posh3/workflows/Release/badge.svg
[psgallery-badge]: https://img.shields.io/powershellgallery/dt/oh-my-posh.svg
@ -69,3 +70,4 @@ using Go (and the amazing [README][powerline-go])
[oh-my-zsh]: https://github.com/robbyrussell/oh-my-zsh
[justjanne]: https://github.com/justjanne
[powerline-go]: https://github.com/justjanne/powerline-go
[starship]: https://github.com/starship/starship/blob/master/src/init/mod.rs

View file

@ -70,6 +70,15 @@ New: &new{},
Even with unit tests, it's a good idea to build and validate the changes.
First, we need to package the init scripts:
```shell
go get -u github.com/kevinburke/go-bindata/...
go-bindata -o init.go init/
```
Next, build the app and validate the changes:
```shell
go build -o $GOPATH/bin/oh-my-posh
```

66
init/pwsh.ps1 Normal file
View file

@ -0,0 +1,66 @@
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "";
ShowDebug = $false
}
if (Test-Path "::CONFIG::") {
$global:PoshSettings.Theme = Resolve-Path -Path "::CONFIG::"
}
function Set-PoshContext {}
function Set-GitStatus {
if (Get-Command -Name "Get-GitStatus" -ErrorAction SilentlyContinue) {
$Global:GitStatus = Get-GitStatus
}
}
[ScriptBlock]$Prompt = {
#store if the last command was successfull
$lastCommandSuccess = $?
#store the last exit code for restore
$realLASTEXITCODE = $global:LASTEXITCODE
$errorCode = 0
Set-PoshContext
if ($lastCommandSuccess -eq $false) {
#native app exit code
if ($realLASTEXITCODE -is [int] -and $realLASTEXITCODE -gt 0) {
$errorCode = $realLASTEXITCODE
}
else {
$errorCode = 1
}
}
$executionTime = -1
$history = Get-History -ErrorAction Ignore -Count 1
if ($null -ne $history -and $null -ne $history.EndExecutionTime -and $null -ne $history.StartExecutionTime) {
$executionTime = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
}
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "::OMP::"
$config = $global:PoshSettings.Theme
$showDebug = $global:PoshSettings.ShowDebug
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
$startInfo.Arguments = "--debug=""$showDebug"" --config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime"
$startInfo.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
if ($PWD.Provider.Name -eq 'FileSystem') {
$startInfo.WorkingDirectory = $PWD.ProviderPath
}
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$standardOut = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
$standardOut
$global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false
Remove-Variable lastCommandSuccess -Confirm:$false
Set-GitStatus
}
Set-Item -Path Function:prompt -Value $Prompt -Force

63
main.go
View file

@ -5,12 +5,18 @@ import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"
)
// Version number of oh-my-posh
var Version = "development"
const (
noExe = "echo \"Unable to find Oh my Posh executable\""
)
type args struct {
ErrorCode *int
PrintConfig *bool
@ -23,6 +29,8 @@ type args struct {
ExecutionTime *float64
Millis *bool
Eval *bool
Init *bool
PrintInit *bool
}
func main() {
@ -71,6 +79,14 @@ func main() {
"eval",
false,
"Run in eval mode"),
Init: flag.Bool(
"init",
false,
"Initialize the shell"),
PrintInit: flag.Bool(
"print-init",
false,
"Print the shell initialization script"),
}
flag.Parse()
env := &environment{
@ -80,6 +96,16 @@ func main() {
fmt.Print(time.Now().UnixNano() / 1000000)
return
}
if *args.Init {
init := initShell(*args.Shell, *args.Config)
fmt.Print(init)
return
}
if *args.PrintInit {
init := printShellInit(*args.Shell, *args.Config)
fmt.Print(init)
return
}
settings := GetSettings(env)
if *args.PrintConfig {
theme, _ := json.MarshalIndent(settings, "", " ")
@ -114,3 +140,40 @@ func main() {
}
engine.render()
}
func initShell(shell, config string) string {
executable, err := os.Executable()
if err != nil {
return noExe
}
switch shell {
case pwsh:
return fmt.Sprintf("Invoke-Expression (@(&\"%s\" --print-init --shell pwsh --config %s) -join \"`n\")", executable, config)
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func printShellInit(shell, config string) string {
executable, err := os.Executable()
if err != nil {
return noExe
}
switch shell {
case pwsh:
return getShellInitScript(executable, config, "init/pwsh.ps1")
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func getShellInitScript(executable, config, script string) string {
data, err := Asset(script)
if err != nil {
return fmt.Sprintf("echo \"Unable to find initialization script %s\"", script)
}
init := string(data)
init = strings.ReplaceAll(init, "::OMP::", executable)
init = strings.ReplaceAll(init, "::CONFIG::", config)
return init
}

View file

@ -83,7 +83,7 @@ function Set-PoshPrompt {
$config = $global:PoshSettings.Theme
$showDebug = $global:PoshSettings.ShowDebug
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
$startInfo.Arguments = "-debug=""$showDebug"" -config=""$config"" -error=$errorCode -pwd=""$cleanPWD"" -execution-time=$executionTime"
$startInfo.Arguments = "--debug=""$showDebug"" --config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime"
$startInfo.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8