Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions statusline.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,9 @@ function fmt_when($epoch) {
return ''
}

function truncate_pct($pct) {
if ([string]::IsNullOrEmpty($pct)) { return '' }
if ($pct -isnot [string]) { $pct = $pct.ToString() }; $idx = $pct.LastIndexOf('.')
if ($idx -ge 0) { return $pct.Substring(0, $idx) }
return $pct
}

function ctx_circle($pctraw) {
$pct = truncate_pct $pctraw
$pct = [string]$pctraw -replace '\..*'
if ([string]::IsNullOrEmpty($pct)) { return '' }
$n = 0
if ([long]::TryParse($pct, [ref]$n)) {
Expand All @@ -104,12 +98,12 @@ function ctx_circle($pctraw) {
}

function limit_pegged() {
$five = truncate_pct $script:five_pct
$five = [string]$script:five_pct -replace '\..*'
if (-not [string]::IsNullOrEmpty($five)) {
$n = 0; if (-not [long]::TryParse($five, [ref]$n)) { $n = 0 }
if ($n -ge 100) { return $true }
}
$week = truncate_pct $script:week_pct
$week = [string]$script:week_pct -replace '\..*'
if (-not [string]::IsNullOrEmpty($week)) {
$n = 0; if (-not [long]::TryParse($week, [ref]$n)) { $n = 0 }
if ($n -ge 100) { return $true }
Expand Down Expand Up @@ -333,7 +327,7 @@ function paint_sep() {
}

function tier_color($pctraw) {
$pct = truncate_pct $pctraw
$pct = [string]$pctraw -replace '\..*'
if ([string]::IsNullOrEmpty($pct)) { return '' }
$n = 0
if ([long]::TryParse($pct, [ref]$n)) {
Expand All @@ -347,7 +341,7 @@ function tier_color($pctraw) {

function cost_tier_color($usd) {
if ([string]::IsNullOrEmpty($usd)) { return '' }
$dollars = truncate_pct $usd
$dollars = [string]$usd -replace '\..*'
if ([string]::IsNullOrEmpty($dollars)) { $dollars = '0' }
$n = 0
if ([long]::TryParse($dollars, [ref]$n)) {
Expand All @@ -370,7 +364,7 @@ function span_sgr($sgr, $tier) {
}

function seg_rate($label, $pctraw, $reset_str) {
$pct = truncate_pct $pctraw
$pct = [string]$pctraw -replace '\..*'
$n = 0
if ([long]::TryParse($pct, [ref]$n) -and $n -ge 100) {
return egg $label $reset_str
Expand All @@ -391,7 +385,7 @@ function seg_rate($label, $pctraw, $reset_str) {
}

function seg_ctx($pctraw, $size) {
$pct = truncate_pct $pctraw
$pct = [string]$pctraw -replace '\..*'
$tier = tier_color $pct
$res = ""
$res += paint (span_sgr $script:CIRCLE_SGR $tier) (ctx_circle $pct)
Expand Down
16 changes: 0 additions & 16 deletions tests/ps-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ if ($null -eq (Get-Command fmt_size -ErrorAction SilentlyContinue)) {

# --- Test fmt helpers ---

$truncateTests = @(
@{in='42.9'; out='42'}
@{in='100'; out='100'}
@{in='0.5'; out='0'}
@{in='.5'; out=''}
@{in=''; out=''}
@{in=$null; out=''}
@{in='no_dot'; out='no_dot'}
@{in=42.9; out='42'}
)
foreach ($t in $truncateTests) {
$res = truncate_pct $t.in
if ($res -ne $t.out) { Fail "truncate_pct" "Expected '$($t.out)', got '$res' for $($t.in)" }
}
Pass "truncate_pct"

$costTests = @(
@{in=1.009058; out='$1.01'}
@{in=0; out='$0.00'}
Expand Down
Loading