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, " ")
postfix := e.activeSegment.getValue(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) {
@ -107,13 +110,11 @@ func (e *engine) setStringValues(segments []*Segment) {
wg.Add(len(segments))
defer wg.Wait()
cwd := e.env.getcwd()
debug := *e.env.getArgs().Debug
for _, segment := range segments {
go func(s *Segment) {
defer wg.Done()
err := s.mapSegmentWithWriter(e.env)
if err == nil && !s.hasValue(IgnoreFolders, cwd) && s.enabled() {
s.stringValue = s.string()
}
s.setStringValue(e.env, cwd, debug)
}(segment)
}
}

View file

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

View file

@ -5,6 +5,7 @@
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "$PSScriptRoot\themes\jandedobbeleer.json";
ShowDebug = $false
}
function Get-PoshCommand {
@ -36,9 +37,14 @@ function Set-PoshPrompt {
param(
[Parameter(Mandatory = $false)]
[string]
$Theme
$Theme,
[Parameter(Mandatory = $false)]
[bool]
$ShowDebug = $false
)
$global:PoshSettings.ShowDebug = $ShowDebug
if (Test-Path "$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.FileName = Get-PoshCommand
$config = $global:PoshSettings.Theme
$showDebug = $global:PoshSettings.ShowDebug
$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.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8

View file

@ -1,6 +1,9 @@
package main
import "errors"
import (
"errors"
"time"
)
// Segment represent a single segment and it's configuration
type Segment struct {
@ -17,6 +20,7 @@ type Segment struct {
writer SegmentWriter
stringValue string
active bool
timing time.Duration
}
// 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")
}
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()
}
}