feat: replace debug information with a new layout

Similar as Starship timings command
This commit is contained in:
lnu 2020-12-27 17:53:58 +01:00 committed by Jan De Dobbeleer
parent eefc689ec9
commit 0fcf7cc8be
6 changed files with 135 additions and 34 deletions

43
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,43 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": ["-config=${workspaceRoot}/themes/jandedobbeleer.omp.json"]
},
{
"name": "Launch tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceRoot}/src",
"args": ["-test.v"]
},
{
"name": "Print debug",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": [
"--debug",
"-config=${workspaceRoot}/themes/jandedobbeleer.omp.json"
]
},
{
"name": "Print init pwsh",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": ["--print-init","-shell=pwsh","-config=${workspaceRoot}/themes/jandedobbeleer.omp.json"]
},
]
}

View file

@ -5,7 +5,6 @@
$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 {
@ -29,6 +28,7 @@ function Set-PoshContext {}
function Set-GitStatus { function Set-GitStatus {
if (Get-Command -Name "Get-GitStatus" -ErrorAction SilentlyContinue) { if (Get-Command -Name "Get-GitStatus" -ErrorAction SilentlyContinue) {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Variable used later(not in this scope)')]
$Global:GitStatus = Get-GitStatus $Global:GitStatus = Get-GitStatus
} }
} }
@ -37,14 +37,9 @@ 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.omp.json") { if (Test-Path "$PSScriptRoot/themes/$Theme.omp.json") {
$global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.omp.json" $global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.omp.json"
} }
@ -81,9 +76,8 @@ 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 = "--debug=""$showDebug"" --config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime" $startInfo.Arguments = "--config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime"
$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

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"sync" "sync"
"time"
) )
type engine struct { type engine struct {
@ -17,6 +18,17 @@ type engine struct {
rprompt string rprompt string
} }
type SegmentTiming struct {
name string
nameLength int
enabled bool
stringValue string
enabledDuration time.Duration
stringDuration time.Duration
background string
foreground string
}
func (e *engine) getPowerlineColor(foreground bool) string { func (e *engine) getPowerlineColor(foreground bool) string {
if e.previousActiveSegment == nil { if e.previousActiveSegment == nil {
return Transparent return Transparent
@ -74,9 +86,6 @@ func (e *engine) renderText(text string) {
prefix := e.activeSegment.getValue(Prefix, defaultValue) prefix := e.activeSegment.getValue(Prefix, defaultValue)
postfix := e.activeSegment.getValue(Postfix, defaultValue) postfix := e.activeSegment.getValue(Postfix, defaultValue)
e.color.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix)) e.color.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix))
if *e.env.getArgs().Debug {
e.color.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) {
@ -117,11 +126,10 @@ 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()
s.setStringValue(e.env, cwd, debug) s.setStringValue(e.env, cwd)
}(segment) }(segment)
} }
} }
@ -159,6 +167,71 @@ func (e *engine) render() {
e.write() e.write()
} }
// debug will lool through your config file and output the timings for each segments
func (e *engine) debug() {
var segmentTimings []SegmentTiming
nameMaxLength := 0
e.renderer.print("\nHere are the timings of segments in your prompt:\n")
// loop each segments of each blocks
for _, block := range e.settings.Blocks {
for _, segment := range block.Segments {
err := segment.mapSegmentWithWriter(e.env)
if err != nil || segment.shouldIgnoreFolder(e.env.getcwd()) {
return
}
var segmentTiming SegmentTiming
segmentTiming.name = string(segment.Type)
segmentTiming.nameLength = len(segmentTiming.name)
if segmentTiming.nameLength > nameMaxLength {
nameMaxLength = segmentTiming.nameLength
}
segmentTiming.background = segment.Background
segmentTiming.foreground = segment.Foreground
// enabled timing
start := time.Now()
segmentTiming.enabled = segment.enabled()
segmentTiming.enabledDuration = time.Since(start)
// string timing
if segmentTiming.enabled {
start = time.Now()
segmentTiming.stringValue = segment.string()
segmentTiming.stringDuration = time.Since(start)
// not pretty rendering could be refactored for a better separation of concern
e.previousActiveSegment = nil
e.activeSegment = segment
e.activeSegment.Background = segment.props.background
e.activeSegment.Foreground = segment.props.foreground
e.renderSegmentText(segmentTiming.stringValue)
segmentTiming.stringValue = e.color.string()
e.color.buffer.Reset()
}
segmentTimings = append(segmentTimings, segmentTiming)
}
}
// 7 => (false)
nameMaxLength += 7
for _, segment := range segmentTimings {
duration := segment.enabledDuration.Milliseconds()
if segment.enabled {
duration += segment.stringDuration.Milliseconds()
}
e.renderer.print(fmt.Sprintf("%-*s - %3d ms - %s\n", nameMaxLength, fmt.Sprintf("%s(%t)", segment.name, segment.enabled),
duration, segment.stringValue))
}
fmt.Print(e.renderer.string())
}
func (e *engine) write() { func (e *engine) write() {
switch e.env.getShellName() { switch e.env.getShellName() {
case zsh: case zsh:

View file

@ -1,6 +1,5 @@
$global:PoshSettings = New-Object -TypeName PSObject -Property @{ $global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = ""; Theme = "";
ShowDebug = $false
} }
if (Test-Path "::CONFIG::") { if (Test-Path "::CONFIG::") {
@ -10,6 +9,7 @@ function Set-PoshContext {}
function Set-GitStatus { function Set-GitStatus {
if (Get-Command -Name "Get-GitStatus" -ErrorAction SilentlyContinue) { if (Get-Command -Name "Get-GitStatus" -ErrorAction SilentlyContinue) {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Variable used later(not in this scope)')]
$Global:GitStatus = Get-GitStatus $Global:GitStatus = Get-GitStatus
} }
} }
@ -40,9 +40,8 @@ function Set-GitStatus {
$startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "::OMP::" $startInfo.FileName = "::OMP::"
$config = $global:PoshSettings.Theme $config = $global:PoshSettings.Theme
$showDebug = $global:PoshSettings.ShowDebug
$cleanPWD = $PWD.ProviderPath.TrimEnd("\") $cleanPWD = $PWD.ProviderPath.TrimEnd("\")
$startInfo.Arguments = "--debug=""$showDebug"" --config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime" $startInfo.Arguments = "--config=""$config"" --error=$errorCode --pwd=""$cleanPWD"" --execution-time=$executionTime"
$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

@ -127,8 +127,10 @@ func main() {
fmt.Println(Version) fmt.Println(Version)
return return
} }
formats := &ansiFormats{} formats := &ansiFormats{}
formats.init(env.getShellName()) formats.init(env.getShellName())
renderer := &AnsiRenderer{ renderer := &AnsiRenderer{
buffer: new(bytes.Buffer), buffer: new(bytes.Buffer),
formats: formats, formats: formats,
@ -149,7 +151,12 @@ func main() {
renderer: renderer, renderer: renderer,
consoleTitle: title, consoleTitle: title,
} }
engine.render()
if *args.Debug {
engine.debug()
} else {
engine.render()
}
} }
func initShell(shell, config string) string { func initShell(shell, config string) string {

View file

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"regexp" "regexp"
"time"
) )
// Segment represent a single segment and it's configuration // Segment represent a single segment and it's configuration
@ -22,7 +21,6 @@ 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
@ -164,24 +162,11 @@ 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) { func (segment *Segment) setStringValue(env environmentInfo, cwd string) {
err := segment.mapSegmentWithWriter(env) err := segment.mapSegmentWithWriter(env)
if err != nil || segment.shouldIgnoreFolder(cwd) { if err != nil || segment.shouldIgnoreFolder(cwd) {
return 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() { if segment.enabled() {
segment.stringValue = segment.string() segment.stringValue = segment.string()
} }