mirror of
https://github.com/charmbracelet/crush.git
synced 2026-10-28 08:35:13 +08:00
fix(ui): color the context readout instead of flagging it with a "W"
This commit is contained in:
5 files changed
+28
-3
No files matched your search
@@ -114,6 +114,10 @@ func ModelInfo(t *styles.Styles, modelName, providerName, reasoningInfo string,
|
||||
)
|
||||
}
|
||||
|
||||
// contextWarnThreshold is the percentage of the context window past which
|
||||
// the usage readout turns the warning colour.
|
||||
const contextWarnThreshold = 80
|
||||
|
||||
// formatTokensAndCost formats token usage and cost with appropriate units
|
||||
// (K/M) and percentage of context window.
|
||||
func formatTokensAndCost(t *styles.Styles, tokens, contextWindow int64, cost float64, estimated bool) string {
|
||||
@@ -147,10 +151,12 @@ func formatTokensAndCost(t *styles.Styles, tokens, contextWindow int64, cost flo
|
||||
percentageText = "~" + percentageText
|
||||
}
|
||||
formattedPercentage := t.ModelInfo.TokenPercentage.Render(percentageText)
|
||||
formattedTokens = fmt.Sprintf("%s %s", formattedPercentage, formattedTokens)
|
||||
if percentage > 80 {
|
||||
formattedTokens = fmt.Sprintf("%s %s", styles.LSPWarningIcon, formattedTokens)
|
||||
// A nearly full context window is worth noticing, so the percentage
|
||||
// itself carries the warning colour rather than a separate marker.
|
||||
if percentage > contextWarnThreshold {
|
||||
formattedPercentage = t.ModelInfo.TokenPercentageWarn.Render(percentageText)
|
||||
}
|
||||
formattedTokens = fmt.Sprintf("%s %s", formattedPercentage, formattedTokens)
|
||||
|
||||
return fmt.Sprintf("%s %s", formattedTokens, formattedCost)
|
||||
}
|
||||
|
||||
@@ -33,3 +33,17 @@ func TestFormatTokensAndCostOmitsEstimatedPrefix(t *testing.T) {
|
||||
require.Contains(t, actual, "12%")
|
||||
require.NotContains(t, actual, "~12%")
|
||||
}
|
||||
|
||||
func TestFormatTokensAndCostWarnsWhenContextNearlyFull(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sty := styles.CharmtonePantera()
|
||||
|
||||
full := formatTokensAndCost(&sty, 850, 1000, 0, false)
|
||||
roomy := formatTokensAndCost(&sty, 120, 1000, 0, false)
|
||||
|
||||
// The warning is colour only, so the text itself gains nothing.
|
||||
require.Equal(t, "85% (850) $0.00", ansi.Strip(full))
|
||||
require.Contains(t, full, sty.ModelInfo.TokenPercentageWarn.Render("85%"))
|
||||
require.Contains(t, roomy, sty.ModelInfo.TokenPercentage.Render("12%"))
|
||||
}
|
||||
@@ -978,6 +978,7 @@ func quickStyle(o quickStyleOpts) Styles {
|
||||
s.ModelInfo.Reasoning = lipgloss.NewStyle().Foreground(o.fgMostSubtle).PaddingLeft(2)
|
||||
s.ModelInfo.TokenCount = lipgloss.NewStyle().Foreground(o.fgMostSubtle)
|
||||
s.ModelInfo.TokenPercentage = lipgloss.NewStyle().Foreground(o.fgMoreSubtle)
|
||||
s.ModelInfo.TokenPercentageWarn = lipgloss.NewStyle().Foreground(o.attention)
|
||||
s.ModelInfo.EstimatedUsagePrefix = s.ModelInfo.TokenPercentage
|
||||
s.ModelInfo.Cost = lipgloss.NewStyle().Foreground(o.fgMoreSubtle)
|
||||
s.ModelInfo.HypercreditIcon = lipgloss.NewStyle().Foreground(o.secondary)
|
||||
|
||||
@@ -279,6 +279,7 @@ type Styles struct {
|
||||
Reasoning lipgloss.Style // Reasoning effort text
|
||||
TokenCount lipgloss.Style // "(42K)" token count
|
||||
TokenPercentage lipgloss.Style // "42%" percent of context window
|
||||
TokenPercentageWarn lipgloss.Style // "85%" when the window is nearly full
|
||||
EstimatedUsagePrefix lipgloss.Style // "~" prefix for estimated usage
|
||||
Cost lipgloss.Style // "$0.42" cost readout
|
||||
HypercreditIcon lipgloss.Style // Hypercredit icon (◆)
|
||||
|
||||
@@ -158,6 +158,9 @@ func charmtoneOverrides(s Styles) Styles {
|
||||
s.Messages.SubduedHypercreditIcon = s.Messages.SubduedHypercreditIcon.
|
||||
Foreground(charmtone.Violet)
|
||||
|
||||
s.ModelInfo.TokenPercentageWarn = s.ModelInfo.TokenPercentageWarn.
|
||||
Foreground(charmtone.Uni)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user