feat: new ShowDebug parameter

calculate each segment timing
new parameter to show/hide segment debug information
set-poshprompt updated with the new showDebug parameter

Force disabled segment to be visible for debug purpose
This commit is contained in:
lnu 2020-11-15 08:47:00 +01:00 committed by Jan De Dobbeleer
parent 1f3c253e45
commit bea3258758
4 changed files with 47 additions and 7 deletions

View file

@ -67,6 +67,9 @@ func (e *engine) renderText(text string) {
prefix := e.activeSegment.getValue(Prefix, " ") prefix := e.activeSegment.getValue(Prefix, " ")
postfix := e.activeSegment.getValue(Postfix, " ") postfix := e.activeSegment.getValue(Postfix, " ")
e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix)) e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix))
if *e.env.getArgs().Debug {
e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("(%s:%s)", e.activeSegment.Type, e.activeSegment.timing))
}
} }
func (e *engine) renderSegmentText(text string) { func (e *engine) renderSegmentText(text string) {
@ -107,13 +110,11 @@ func (e *engine) setStringValues(segments []*Segment) {
wg.Add(len(segments)) wg.Add(len(segments))
defer wg.Wait() defer wg.Wait()
cwd := e.env.getcwd() cwd := e.env.getcwd()
debug := *e.env.getArgs().Debug
for _, segment := range segments { for _, segment := range segments {
go func(s *Segment) { go func(s *Segment) {
defer wg.Done() defer wg.Done()
err := s.mapSegmentWithWriter(e.env) s.setStringValue(e.env, cwd, debug)
if err == nil && !s.hasValue(IgnoreFolders, cwd) && s.enabled() {
s.stringValue = s.string()
}
}(segment) }(segment)
} }
} }

View file

@ -14,6 +14,7 @@ type args struct {
Config *string Config *string
Shell *string Shell *string
PWD *string PWD *string
Debug *bool
} }
func main() { func main() {
@ -42,6 +43,10 @@ func main() {
"pwd", "pwd",
"", "",
"the path you are working in"), "the path you are working in"),
Debug: flag.Bool(
"debug",
false,
"Print debug information"),
} }
flag.Parse() flag.Parse()
env := &environment{ env := &environment{

View file

@ -5,6 +5,7 @@
$global:PoshSettings = New-Object -TypeName PSObject -Property @{ $global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "$PSScriptRoot\themes\jandedobbeleer.json"; Theme = "$PSScriptRoot\themes\jandedobbeleer.json";
ShowDebug = $false
} }
function Get-PoshCommand { function Get-PoshCommand {
@ -36,9 +37,14 @@ function Set-PoshPrompt {
param( param(
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string] [string]
$Theme $Theme,
[Parameter(Mandatory = $false)]
[bool]
$ShowDebug = $false
) )
$global:PoshSettings.ShowDebug = $ShowDebug
if (Test-Path "$PSScriptRoot/themes/$Theme.json") { if (Test-Path "$PSScriptRoot/themes/$Theme.json") {
$global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.json" $global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.json"
} }
@ -68,8 +74,9 @@ function Set-PoshPrompt {
$startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = Get-PoshCommand $startInfo.FileName = Get-PoshCommand
$config = $global:PoshSettings.Theme $config = $global:PoshSettings.Theme
$showDebug = $global:PoshSettings.ShowDebug
$cleanPWD = $PWD.ProviderPath.TrimEnd("\") $cleanPWD = $PWD.ProviderPath.TrimEnd("\")
$startInfo.Arguments = "-config=""$config"" -error=$errorCode -pwd=""$cleanPWD""" $startInfo.Arguments = "-debug=""$showDebug"" -config=""$config"" -error=$errorCode -pwd=""$cleanPWD"""
$startInfo.Environment["TERM"] = "xterm-256color" $startInfo.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true $startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8 $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8

View file

@ -1,6 +1,9 @@
package main package main
import "errors" import (
"errors"
"time"
)
// Segment represent a single segment and it's configuration // Segment represent a single segment and it's configuration
type Segment struct { type Segment struct {
@ -17,6 +20,7 @@ type Segment struct {
writer SegmentWriter writer SegmentWriter
stringValue string stringValue string
active bool active bool
timing time.Duration
} }
// SegmentWriter is the interface used to define what and if to write to the prompt // SegmentWriter is the interface used to define what and if to write to the prompt
@ -149,3 +153,26 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
} }
return errors.New("unable to map writer") return errors.New("unable to map writer")
} }
func (segment *Segment) setStringValue(env environmentInfo, cwd string, debug bool) {
err := segment.mapSegmentWithWriter(env)
if err != nil || segment.hasValue(IgnoreFolders, cwd) {
return
}
// add timing only in debug
if debug {
start := time.Now()
defer (func() {
// force segment rendering to display the time it took
// to check if the segment is enabled or not
// depending on the segement, calling enabled()
// can be time consuming
segment.active = true
elapsed := time.Since(start)
segment.timing = elapsed
})()
}
if segment.enabled() {
segment.stringValue = segment.string()
}
}