Closed MJECloud closed 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?
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! 😊
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.
Prerequisites
CONTRIBUTING
guideDescription
Hi,
is it possible to display the time the last command took? In v2 i wrote a function in the .psm1.
Cheers, Maurice