JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
17.01k stars 2.37k forks source link

Time of command execution #102

Closed MJECloud closed 3 years ago

MJECloud commented 3 years ago

Prerequisites

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

JanDeDobbeleer commented 3 years ago

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?

MJECloud commented 3 years ago

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")
                }
            }
        }
    }
JanDeDobbeleer commented 3 years ago

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

JanDeDobbeleer commented 3 years ago

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"
  }
}
MJECloud commented 3 years ago

Thank you! I'll test it 😁

MJECloud commented 3 years ago

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

image

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.