mirror of
https://github.com/tiennm99/ccstatusline.git
synced 2026-07-20 06:18:43 +00:00
feat: add remaining mode to context % widgets (#83)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { RenderContext } from '../types/RenderContext';
|
||||
import type { Settings } from '../types/Settings';
|
||||
import type {
|
||||
CustomKeybind,
|
||||
Widget,
|
||||
WidgetEditorDisplay,
|
||||
WidgetItem
|
||||
@@ -8,22 +9,56 @@ import type {
|
||||
|
||||
export class ContextPercentageWidget implements Widget {
|
||||
getDefaultColor(): string { return 'blue'; }
|
||||
getDescription(): string { return 'Shows percentage of context window used (of 200k tokens)'; }
|
||||
getDescription(): string { return 'Shows percentage of context window used or remaining (of 200k tokens)'; }
|
||||
getDisplayName(): string { return 'Context %'; }
|
||||
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
|
||||
return { displayText: this.getDisplayName() };
|
||||
const isInverse = item.metadata?.inverse === 'true';
|
||||
const modifiers: string[] = [];
|
||||
|
||||
if (isInverse) {
|
||||
modifiers.push('remaining');
|
||||
}
|
||||
|
||||
return {
|
||||
displayText: this.getDisplayName(),
|
||||
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
|
||||
};
|
||||
}
|
||||
|
||||
handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
|
||||
if (action === 'toggle-inverse') {
|
||||
const currentState = item.metadata?.inverse === 'true';
|
||||
return {
|
||||
...item,
|
||||
metadata: {
|
||||
...item.metadata,
|
||||
inverse: (!currentState).toString()
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
render(item: WidgetItem, context: RenderContext, settings: Settings): string | null {
|
||||
const isInverse = item.metadata?.inverse === 'true';
|
||||
|
||||
if (context.isPreview) {
|
||||
return item.rawValue ? '9.3%' : 'Ctx: 9.3%';
|
||||
const previewValue = isInverse ? '90.7%' : '9.3%';
|
||||
return item.rawValue ? previewValue : `Ctx: ${previewValue}`;
|
||||
} else if (context.tokenMetrics) {
|
||||
const percentage = Math.min(100, (context.tokenMetrics.contextLength / 200000) * 100);
|
||||
return item.rawValue ? `${percentage.toFixed(1)}%` : `Ctx: ${percentage.toFixed(1)}%`;
|
||||
const usedPercentage = Math.min(100, (context.tokenMetrics.contextLength / 200000) * 100);
|
||||
const displayPercentage = isInverse ? (100 - usedPercentage) : usedPercentage;
|
||||
return item.rawValue ? `${displayPercentage.toFixed(1)}%` : `Ctx: ${displayPercentage.toFixed(1)}%`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getCustomKeybinds(): CustomKeybind[] {
|
||||
return [
|
||||
{ key: 'l', label: '(l)eft/remaining', action: 'toggle-inverse' }
|
||||
];
|
||||
}
|
||||
|
||||
supportsRawValue(): boolean { return true; }
|
||||
supportsColors(item: WidgetItem): boolean { return true; }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { RenderContext } from '../types/RenderContext';
|
||||
import type { Settings } from '../types/Settings';
|
||||
import type {
|
||||
CustomKeybind,
|
||||
Widget,
|
||||
WidgetEditorDisplay,
|
||||
WidgetItem
|
||||
@@ -8,22 +9,56 @@ import type {
|
||||
|
||||
export class ContextPercentageUsableWidget implements Widget {
|
||||
getDefaultColor(): string { return 'green'; }
|
||||
getDescription(): string { return 'Shows percentage of usable context window used (of 160k tokens before auto-compact)'; }
|
||||
getDescription(): string { return 'Shows percentage of usable context window used or remaining (of 160k tokens before auto-compact)'; }
|
||||
getDisplayName(): string { return 'Context % (usable)'; }
|
||||
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
|
||||
return { displayText: this.getDisplayName() };
|
||||
const isInverse = item.metadata?.inverse === 'true';
|
||||
const modifiers: string[] = [];
|
||||
|
||||
if (isInverse) {
|
||||
modifiers.push('remaining');
|
||||
}
|
||||
|
||||
return {
|
||||
displayText: this.getDisplayName(),
|
||||
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
|
||||
};
|
||||
}
|
||||
|
||||
handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
|
||||
if (action === 'toggle-inverse') {
|
||||
const currentState = item.metadata?.inverse === 'true';
|
||||
return {
|
||||
...item,
|
||||
metadata: {
|
||||
...item.metadata,
|
||||
inverse: (!currentState).toString()
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
render(item: WidgetItem, context: RenderContext, settings: Settings): string | null {
|
||||
const isInverse = item.metadata?.inverse === 'true';
|
||||
|
||||
if (context.isPreview) {
|
||||
return item.rawValue ? '11.6%' : 'Ctx(u): 11.6%';
|
||||
const previewValue = isInverse ? '88.4%' : '11.6%';
|
||||
return item.rawValue ? previewValue : `Ctx(u): ${previewValue}`;
|
||||
} else if (context.tokenMetrics) {
|
||||
const percentage = Math.min(100, (context.tokenMetrics.contextLength / 160000) * 100);
|
||||
return item.rawValue ? `${percentage.toFixed(1)}%` : `Ctx(u): ${percentage.toFixed(1)}%`;
|
||||
const usedPercentage = Math.min(100, (context.tokenMetrics.contextLength / 160000) * 100);
|
||||
const displayPercentage = isInverse ? (100 - usedPercentage) : usedPercentage;
|
||||
return item.rawValue ? `${displayPercentage.toFixed(1)}%` : `Ctx(u): ${displayPercentage.toFixed(1)}%`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getCustomKeybinds(): CustomKeybind[] {
|
||||
return [
|
||||
{ key: 'l', label: '(l)eft/remaining', action: 'toggle-inverse' }
|
||||
];
|
||||
}
|
||||
|
||||
supportsRawValue(): boolean { return true; }
|
||||
supportsColors(item: WidgetItem): boolean { return true; }
|
||||
}
|
||||
Reference in New Issue
Block a user