Oh-my-posh3: Time of command execution

Created on 26 Oct 2020  路  6Comments  路  Source: JanDeDobbeleer/oh-my-posh3

Prerequisites

  • [x] I have read and understand the CONTRIBUTING guide
  • [x] I looked for duplicate issues before submitting this one

Description

Hi,

is it possible to display the time the last command took?
In v2 i wrote a function in the .psm1.
image

Cheers,
Maurice

Most helpful comment

After building my own theme and testing i can confirm that it works! 馃槉

image

All 6 comments

Depends. Maybe if we abuse the Set-PoshContext function to add that to an env var we can read it might work. However, I might need to move it up being the first function that's being called possibly conflicting with the last exit code. How did you handle this in V2 code-wise?

Here is the code 馃槉

    $lastCmdTime = ""
    $lastCmd = Get-History -Count 1
    if ($null -ne $lastCmd) {

        if($PSVersionTable.PSVersion.Major -gt 5) {
            $cmdTime = $lastCmd.Duration
        }
        else {
            $cmdTime = ($Lastcmd.EndExecutionTime - $Lastcmd.StartExecutionTime)
        }
        $lastCmdTime = -join($cmdTime.Milliseconds,"ms")

        if ($cmdTime.TotalMilliseconds -ge 1000) {
            $lastCmdTime = -join($cmdTime.Seconds,"s")

            if ($cmdTime.TotalSeconds -ge 60) {
                $lastCmdTime = -join($cmdTime.Minutes,"m ",$cmdTime.Seconds, "s")

                if($cmdTime.TotalMinutes -ge 60) {
                    $lastCmdTime = -join($cmdTime.Hours,"h ",$cmdTime.Minutes, "m")
                }
            }
        }
    }

Top, I'll draft a POC tomorrow morning. I need to check a PR and can't stand open issues for too long 馃槀

Ok, so all you need is to extend your profile with the following and create a custom theme.

function Set-EnvVar {
    $lastCmdTime = ""
    $lastCmd = Get-History -Count 1
    if ($null -ne $lastCmd) {

        if($PSVersionTable.PSVersion.Major -gt 5) {
            $cmdTime = $lastCmd.Duration
        }
        else {
            $cmdTime = ($Lastcmd.EndExecutionTime - $Lastcmd.StartExecutionTime)
        }
        $lastCmdTime = -join($cmdTime.Milliseconds,"ms")

        if ($cmdTime.TotalMilliseconds -ge 1000) {
            $lastCmdTime = -join($cmdTime.Seconds,"s")

            if ($cmdTime.TotalSeconds -ge 60) {
                $lastCmdTime = -join($cmdTime.Minutes,"m ",$cmdTime.Seconds, "s")

                if($cmdTime.TotalMinutes -ge 60) {
                    $lastCmdTime = -join($cmdTime.Hours,"h ",$cmdTime.Minutes, "m")
                }
            }
        }
    }
    $env:POSH=$lastCmdTime
}

New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global

Set-PoshPrompt -Theme mytheme.json

To create a custom theme, you can use the Write-PoshTheme function which will output a json object you can copy-paste into a new file and extend with the environment variable segment.

{
  "type": "envvar",
  "style": "powerline",
  "powerline_symbol": "\uE0B0",
  "foreground": "#ffffff",
  "background": "#0077c2",
  "properties": {
    "var_name": "POSH"
  }
}

Thank you! I'll test it 馃榿

After building my own theme and testing i can confirm that it works! 馃槉

image

Was this page helpful?
0 / 5 - 0 ratings