diff --git a/.assets/config/pwsh_cfg/profile.ps1 b/.assets/config/pwsh_cfg/profile.ps1 index 95f897ff..1ae2e84e 100644 --- a/.assets/config/pwsh_cfg/profile.ps1 +++ b/.assets/config/pwsh_cfg/profile.ps1 @@ -70,13 +70,46 @@ try { [Environment]::SetEnvironmentVariable('VIRTUAL_ENV_DISABLE_PROMPT', $true) } catch { function Prompt { - $split = $($PWD.Path.Replace($HOME, '~').Replace('Microsoft.PowerShell.Core\FileSystem::', '') -replace '\\$').Split([IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries) + $execStatus = $? + # get execution time of the last command + if (Get-Command Format-Duration -CommandType Function -ErrorAction SilentlyContinue) { + $executionTime = (Get-History).Count -gt 0 ? (Format-Duration -TimeSpan (Get-History)[-1].Duration) : $null + } + # build current prompt path + $pathString = $PWD.Path.Replace($HOME, '~').Replace('Microsoft.PowerShell.Core\FileSystem::', '') -replace '\\$' + $split = $pathString.Split([IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries) $promptPath = if ($split.Count -gt 3) { [string]::Join('/', $split[0], '..', $split[-1]) } else { [string]::Join('/', $split) } - return "`e[1;32m{0}@{1}`e[0m: `e[1;34m$promptPath`e[0m> " -f $env:USER, ($env:HOSTNAME ?? $env:WSL_DISTRO_NAME) + # run elevated indicator + if ((id -u) -eq 0) { + [Console]::Write("`e[91m#`e[0m ") + } + # write last execution time + if ($executionTime) { + [Console]::Write("[`e[93m$executionTime`e[0m] ") + } + # write last execution status + [Console]::Write("$($PSStyle.Bold){0}`u{2192} ", $execStatus ? $PSStyle.Foreground.BrightGreen : $PSStyle.Foreground.BrightRed) + # write prompt path + [Console]::Write("`e[1;94m$promptPath`e[0m ") + # write git branch/status + if ($GitPromptSettings) { + # get git status + $gitStatus = @(git status -b --porcelain=v2 2>$null)[1..4] + if ($gitStatus) { + # get branch name and upstream status + $branch = $gitStatus[0].Split(' ')[2] + ($gitStatus[1] -match 'branch.upstream' ? $null : " `u{21E1}") + # format branch name color depending on working tree status + [Console]::Write( + "`e[38;2;232;204;151m({0}$branch`e[38;2;232;204;151m) ", + ($gitStatus | Select-String -Pattern '^(?!#)' -Quiet) ? "`e[38;2;255;146;72m" : "`e[38;2;212;170;252m" + ) + } + } + return '{0}{1} ' -f ($PSStyle.Reset, '>' * ($nestedPromptLevel + 1)) } } #endregion diff --git a/.assets/scripts/linux_setup.sh b/.assets/scripts/linux_setup.sh index c4b57a72..b3b9cfe0 100755 --- a/.assets/scripts/linux_setup.sh +++ b/.assets/scripts/linux_setup.sh @@ -141,8 +141,8 @@ for sc in ${scope_arr[@]}; do done # install powershell modules if [ -f /usr/bin/pwsh ]; then - cmd="Import-Module (Resolve-Path './modules/InstallUtils'); Invoke-GhRepoClone -OrgRepo 'szymonos/ps-modules'" - cloned=$(pwsh -nop -c $cmd) + cmnd="Import-Module (Resolve-Path './modules/InstallUtils'); Invoke-GhRepoClone -OrgRepo 'szymonos/ps-modules'" + cloned=$(pwsh -nop -c $cmnd) if [ $cloned -gt 0 ]; then printf "\e[96minstalling ps-modules...\e[0m\n" # install do-common module for all users diff --git a/modules/InstallUtils/Functions/git.ps1 b/modules/InstallUtils/Functions/git.ps1 index 4a82fe5d..094b76f8 100644 --- a/modules/InstallUtils/Functions/git.ps1 +++ b/modules/InstallUtils/Functions/git.ps1 @@ -77,24 +77,26 @@ function Invoke-GhRepoClone { Function for updating current git branch from remote. #> function Update-GitRepository { + [CmdletBinding()] + param () # perform check if the repository has remote $remote = git remote 2>$null if ($remote) { # fetch updates from remote - Write-Host "fetching $remote..." + Write-Verbose "fetching $remote..." git fetch --tags --prune --prune-tags --force $remote # check if current branch is behind remote $branch = git branch --show-current if ((git rev-parse HEAD) -ne (git rev-parse "$remote/$branch")) { - Write-Host "$branch branch is behind the $remote, performing hard reset" + Write-Verbose "$branch branch is behind the $remote, performing hard reset" git reset --hard "$remote/$branch" return 2 } else { - Write-Host "$branch branch is up to date" + Write-Verbose "$branch branch is up to date" return 1 } } else { - Write-Host 'Not a git repository.' + Write-Warning 'Not a git repository.' return 0 } } diff --git a/modules/InstallUtils/InstallUtils.psd1 b/modules/InstallUtils/InstallUtils.psd1 index ceecb9f3..b76f5218 100644 --- a/modules/InstallUtils/InstallUtils.psd1 +++ b/modules/InstallUtils/InstallUtils.psd1 @@ -12,7 +12,7 @@ RootModule = 'InstallUtils.psm1' # Version number of this module. - ModuleVersion = '0.3.0' + ModuleVersion = '0.3.1' # Supported PSEditions CompatiblePSEditions = @('Core', 'Desk') diff --git a/wsl/wsl_setup.ps1 b/wsl/wsl_setup.ps1 index 44ac5334..2cec400e 100644 --- a/wsl/wsl_setup.ps1 +++ b/wsl/wsl_setup.ps1 @@ -279,7 +279,7 @@ process { zsh { $scopes.Add('shell') | Out-Null } } # determine 'oh_my_posh' scope - if ($chk.omp -or $OmpTheme) { + if ($lx.Version -eq 2 -and ($chk.omp -or $OmpTheme)) { @('oh_my_posh', 'shell').ForEach({ $scopes.Add($_) | Out-Null }) } # remove scopes unavailable in WSL1 @@ -287,6 +287,7 @@ process { $scopes.Remove('distrobox') | Out-Null $scopes.Remove('docker') | Out-Null $scopes.Remove('k8s_ext') | Out-Null + $scopes.Remove('oh_my_posh') | Out-Null } # display distro name and installed scopes Write-Host "`n`e[95;1m${Distro}$($scopes.Count ? " :`e[0;90m $($scopes -join ', ')`e[0m" : "`e[0m")"