@aicodewith/ccstatusline
Version:
AiCodeWith credits display plugin for Claude Code with customizable status line
190 lines (188 loc) • 4.31 kB
JavaScript
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// src/utils/config.ts
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { promisify } from "util";
var readFile2 = fs.promises?.readFile || promisify(fs.readFile);
var writeFile2 = fs.promises?.writeFile || promisify(fs.writeFile);
var mkdir2 = fs.promises?.mkdir || promisify(fs.mkdir);
var CONFIG_DIR = path.join(os.homedir(), ".config", "ccstatusline");
var SETTINGS_PATH = path.join(CONFIG_DIR, "settings.json");
var DEFAULT_SETTINGS = {
lines: [
// 第1行:Model | Session | Version
[
{
"id": "1",
"type": "model",
"color": "cyan"
},
{
"id": "2",
"type": "separator"
},
{
"id": "3",
"type": "session-clock",
"color": "green"
},
{
"id": "4",
"type": "separator"
},
{
"id": "5",
"type": "version",
"color": "gray"
}
],
// 第2行:Context相关信息
[
{
"id": "6",
"type": "context-length",
"color": "yellow"
},
{
"id": "7",
"type": "separator"
},
{
"id": "8",
"type": "context-percentage",
"color": "yellow"
},
{
"id": "9",
"type": "separator"
},
{
"id": "10",
"type": "context-percentage-usable",
"color": "yellow"
}
],
// 第3行:AiCodeWith积分信息
[
{
"id": "11",
"type": "aicodewith-month-remain",
"color": "magenta"
},
{
"id": "12",
"type": "separator"
},
{
"id": "13",
"type": "aicodewith-bonus",
"color": "cyan"
},
{
"id": "14",
"type": "separator"
},
{
"id": "15",
"type": "aicodewith-usage-percent",
"color": "green"
},
{
"id": "16",
"type": "separator"
},
{
"id": "17",
"type": "aicodewith-username",
"color": "blue"
},
{
"id": "18",
"type": "separator"
},
{
"id": "19",
"type": "aicodewith-plan",
"color": "yellow"
},
{
"id": "20",
"type": "separator"
},
{
"id": "21",
"type": "aicodewith-days",
"color": "red"
}
]
],
flexMode: "full-minus-40",
compactThreshold: 60
};
async function loadSettings() {
try {
if (!fs.existsSync(SETTINGS_PATH)) {
return DEFAULT_SETTINGS;
}
const content = await readFile2(SETTINGS_PATH, "utf-8");
let loaded;
try {
loaded = JSON.parse(content);
} catch (parseError) {
return DEFAULT_SETTINGS;
}
if (loaded.elements || loaded.layout) {
return migrateOldSettings(loaded);
}
if (loaded.items && !loaded.lines) {
loaded.lines = [loaded.items];
delete loaded.items;
}
if (loaded.lines) {
if (!Array.isArray(loaded.lines)) {
loaded.lines = [[]];
}
loaded.lines = loaded.lines.slice(0, 3);
}
return { ...DEFAULT_SETTINGS, ...loaded };
} catch (error) {
return DEFAULT_SETTINGS;
}
}
function migrateOldSettings(old) {
const items = [];
let id = 1;
if (old.elements?.model) {
items.push({ id: String(id++), type: "model", color: old.colors?.model });
}
if (items.length > 0 && old.elements?.gitBranch) {
items.push({ id: String(id++), type: "separator" });
}
if (old.elements?.gitBranch) {
items.push({ id: String(id++), type: "git-branch", color: old.colors?.gitBranch });
}
if (old.layout?.expandingSeparators) {
items.forEach((item) => {
if (item.type === "separator") {
item.type = "flex-separator";
}
});
}
return {
lines: [items],
// Put migrated items in first line
flexMode: DEFAULT_SETTINGS.flexMode,
compactThreshold: DEFAULT_SETTINGS.compactThreshold
};
}
async function saveSettings(settings) {
await mkdir2(CONFIG_DIR, { recursive: true });
await writeFile2(SETTINGS_PATH, JSON.stringify(settings, null, 2), "utf-8");
}
export {
DEFAULT_SETTINGS,
loadSettings,
saveSettings
};