mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-29 12:57:50 +08:00
refactor(logging): migrate from CCLog to structured logger
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { Command, createCli } from "@/lib/ccCLI";
|
import { Command, createCli } from "@/lib/ccCLI";
|
||||||
import { Ok } from "@/lib/thirdparty/ts-result-es";
|
import { Ok } from "@/lib/thirdparty/ts-result-es";
|
||||||
import { CCLog } from "@/lib/ccLog";
|
|
||||||
import {
|
import {
|
||||||
AccessConfig,
|
AccessConfig,
|
||||||
UserGroupConfig,
|
UserGroupConfig,
|
||||||
@@ -8,12 +7,13 @@ import {
|
|||||||
saveConfig,
|
saveConfig,
|
||||||
} from "./config";
|
} from "./config";
|
||||||
import { parseBoolean } from "@/lib/common";
|
import { parseBoolean } from "@/lib/common";
|
||||||
|
import { Logger } from "@/lib/ccStructLog";
|
||||||
|
|
||||||
// 1. Define AppContext
|
// 1. Define AppContext
|
||||||
export interface AppContext {
|
export interface AppContext {
|
||||||
configFilepath: string;
|
configFilepath: string;
|
||||||
reloadConfig: () => void;
|
reloadConfig: () => void;
|
||||||
logger: CCLog;
|
logger: Logger;
|
||||||
print: (
|
print: (
|
||||||
message: string | MinecraftTextComponent | MinecraftTextComponent[],
|
message: string | MinecraftTextComponent | MinecraftTextComponent[],
|
||||||
) => void;
|
) => void;
|
||||||
@@ -48,7 +48,9 @@ const addCommand: Command<AppContext> = {
|
|||||||
config.adminGroupConfig.groupUsers.push(playerName);
|
config.adminGroupConfig.groupUsers.push(playerName);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const group = config.usersGroups.find((g) => g.groupName === groupName);
|
const group = config.usersGroups.find(
|
||||||
|
(g) => g.groupName === groupName,
|
||||||
|
);
|
||||||
if (!group) {
|
if (!group) {
|
||||||
const groupNames = getGroupNames(config);
|
const groupNames = getGroupNames(config);
|
||||||
context.print({
|
context.print({
|
||||||
@@ -107,7 +109,9 @@ const delCommand: Command<AppContext> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (group.groupUsers !== undefined) {
|
if (group.groupUsers !== undefined) {
|
||||||
group.groupUsers = group.groupUsers.filter((user) => user !== playerName);
|
group.groupUsers = group.groupUsers.filter(
|
||||||
|
(user) => user !== playerName,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
saveConfig(config, context.configFilepath);
|
saveConfig(config, context.configFilepath);
|
||||||
@@ -238,7 +242,10 @@ const configCommand: Command<AppContext> = {
|
|||||||
{ name: "value", description: "要设置的值", required: true },
|
{ name: "value", description: "要设置的值", required: true },
|
||||||
],
|
],
|
||||||
action: ({ args, context }) => {
|
action: ({ args, context }) => {
|
||||||
const [option, valueStr] = [args.option as string, args.value as string];
|
const [option, valueStr] = [
|
||||||
|
args.option as string,
|
||||||
|
args.value as string,
|
||||||
|
];
|
||||||
const config = loadConfig(context.configFilepath)!;
|
const config = loadConfig(context.configFilepath)!;
|
||||||
|
|
||||||
// Check if it's a group property (contains a dot)
|
// Check if it's a group property (contains a dot)
|
||||||
@@ -251,7 +258,9 @@ const configCommand: Command<AppContext> = {
|
|||||||
if (groupName === "admin") {
|
if (groupName === "admin") {
|
||||||
groupConfig = config.adminGroupConfig;
|
groupConfig = config.adminGroupConfig;
|
||||||
} else {
|
} else {
|
||||||
groupConfig = config.usersGroups.find((g) => g.groupName === groupName);
|
groupConfig = config.usersGroups.find(
|
||||||
|
(g) => g.groupName === groupName,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!groupConfig) {
|
if (!groupConfig) {
|
||||||
@@ -321,7 +330,9 @@ const configCommand: Command<AppContext> = {
|
|||||||
const value = parseInt(valueStr);
|
const value = parseInt(valueStr);
|
||||||
|
|
||||||
if (isNaN(value)) {
|
if (isNaN(value)) {
|
||||||
context.print({ text: `无效的值: ${valueStr}. 必须是一个数字。` });
|
context.print({
|
||||||
|
text: `无效的值: ${valueStr}. 必须是一个数字。`,
|
||||||
|
});
|
||||||
return Ok.EMPTY;
|
return Ok.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,10 +11,12 @@ import {
|
|||||||
ConsoleStream,
|
ConsoleStream,
|
||||||
DAY,
|
DAY,
|
||||||
FileStream,
|
FileStream,
|
||||||
Logger,
|
getStructLogger,
|
||||||
|
LoggerOptions,
|
||||||
LogLevel,
|
LogLevel,
|
||||||
MB,
|
MB,
|
||||||
processor,
|
processor,
|
||||||
|
setStructLoggerConfig,
|
||||||
textRenderer,
|
textRenderer,
|
||||||
} from "@/lib/ccStructLog";
|
} from "@/lib/ccStructLog";
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ const args = [...$vararg];
|
|||||||
|
|
||||||
// Init Log
|
// Init Log
|
||||||
let isOnConsoleStream = true;
|
let isOnConsoleStream = true;
|
||||||
const logger = new Logger({
|
const loggerConfig: LoggerOptions = {
|
||||||
processors: [
|
processors: [
|
||||||
processor.filterByLevel(LogLevel.Info),
|
processor.filterByLevel(LogLevel.Info),
|
||||||
processor.addTimestamp(),
|
processor.addTimestamp(),
|
||||||
@@ -40,7 +42,9 @@ const logger = new Logger({
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
});
|
};
|
||||||
|
setStructLoggerConfig(loggerConfig);
|
||||||
|
const logger = getStructLogger();
|
||||||
|
|
||||||
// Load Config
|
// Load Config
|
||||||
const configFilepath = `${shell.dir()}/access.config.json`;
|
const configFilepath = `${shell.dir()}/access.config.json`;
|
||||||
@@ -79,6 +83,11 @@ function reloadConfig() {
|
|||||||
gWatchPlayersInfo = [];
|
gWatchPlayersInfo = [];
|
||||||
releaser.release();
|
releaser.release();
|
||||||
logger.info("Reload config successfully!");
|
logger.info("Reload config successfully!");
|
||||||
|
const tutorial: string[] = [];
|
||||||
|
tutorial.push("Access Control System started.");
|
||||||
|
tutorial.push("\tPress 'c' to open configuration TUI.");
|
||||||
|
tutorial.push("\tPress 'r' to reload configuration.");
|
||||||
|
print(tutorial.join("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function safeParseTextComponent(
|
function safeParseTextComponent(
|
||||||
|
|||||||
@@ -99,25 +99,31 @@ const AccessControlTUI = () => {
|
|||||||
|
|
||||||
// Validate numbers
|
// Validate numbers
|
||||||
if (
|
if (
|
||||||
validateNumber(currentConfig.detectInterval?.toString() ?? "") === null
|
validateNumber(
|
||||||
|
currentConfig.detectInterval?.toString() ?? "",
|
||||||
|
) === null
|
||||||
) {
|
) {
|
||||||
showError("Invalid Detect Interval: must be a number");
|
showError("Invalid Detect Interval: must be a number");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
validateNumber(currentConfig.watchInterval?.toString() ?? "") === null
|
validateNumber(
|
||||||
|
currentConfig.watchInterval?.toString() ?? "",
|
||||||
|
) === null
|
||||||
) {
|
) {
|
||||||
showError("Invalid Watch Interval: must be a number");
|
showError("Invalid Watch Interval: must be a number");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
validateNumber(currentConfig.noticeTimes?.toString() ?? "") === null
|
validateNumber(currentConfig.noticeTimes?.toString() ?? "") ===
|
||||||
|
null
|
||||||
) {
|
) {
|
||||||
showError("Invalid Notice Times: must be a number");
|
showError("Invalid Notice Times: must be a number");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
validateNumber(currentConfig.detectRange?.toString() ?? "") === null
|
validateNumber(currentConfig.detectRange?.toString() ?? "") ===
|
||||||
|
null
|
||||||
) {
|
) {
|
||||||
showError("Invalid Detect Range: must be a number");
|
showError("Invalid Detect Range: must be a number");
|
||||||
return;
|
return;
|
||||||
@@ -153,7 +159,9 @@ const AccessControlTUI = () => {
|
|||||||
|
|
||||||
for (const toastConfig of toastConfigs) {
|
for (const toastConfig of toastConfigs) {
|
||||||
if (toastConfig.value != undefined) {
|
if (toastConfig.value != undefined) {
|
||||||
const serialized = textutils.serialiseJSON(toastConfig.value);
|
const serialized = textutils.serialiseJSON(
|
||||||
|
toastConfig.value,
|
||||||
|
);
|
||||||
if (!validateTextComponent(serialized)) {
|
if (!validateTextComponent(serialized)) {
|
||||||
showError(
|
showError(
|
||||||
`Invalid ${toastConfig.name}: must be valid MinecraftTextComponent JSON`,
|
`Invalid ${toastConfig.name}: must be valid MinecraftTextComponent JSON`,
|
||||||
@@ -210,7 +218,9 @@ const AccessControlTUI = () => {
|
|||||||
const currentAdmin = config().adminGroupConfig;
|
const currentAdmin = config().adminGroupConfig;
|
||||||
setConfig("adminGroupConfig", {
|
setConfig("adminGroupConfig", {
|
||||||
...currentAdmin,
|
...currentAdmin,
|
||||||
groupUsers: currentAdmin.groupUsers.filter((user) => user !== userName),
|
groupUsers: currentAdmin.groupUsers.filter(
|
||||||
|
(user) => user !== userName,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Regular group
|
// Regular group
|
||||||
@@ -268,7 +278,10 @@ const AccessControlTUI = () => {
|
|||||||
onFocusChanged: () => {
|
onFocusChanged: () => {
|
||||||
const num = validateNumber(getDetectInterval());
|
const num = validateNumber(getDetectInterval());
|
||||||
if (num !== null) setConfig("detectInterval", num);
|
if (num !== null) setConfig("detectInterval", num);
|
||||||
else setDetectInterval(config().detectInterval.toString());
|
else
|
||||||
|
setDetectInterval(
|
||||||
|
config().detectInterval.toString(),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
@@ -282,7 +295,8 @@ const AccessControlTUI = () => {
|
|||||||
onFocusChanged: () => {
|
onFocusChanged: () => {
|
||||||
const num = validateNumber(getWatchInterval());
|
const num = validateNumber(getWatchInterval());
|
||||||
if (num !== null) setConfig("watchInterval", num);
|
if (num !== null) setConfig("watchInterval", num);
|
||||||
else setWatchInterval(config().watchInterval.toString());
|
else
|
||||||
|
setWatchInterval(config().watchInterval.toString());
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
@@ -347,11 +361,15 @@ const AccessControlTUI = () => {
|
|||||||
div(
|
div(
|
||||||
{ class: "flex flex-col" },
|
{ class: "flex flex-col" },
|
||||||
label({}, "Groups:"),
|
label({}, "Groups:"),
|
||||||
For({ each: () => groups, class: "flex flex-col" }, (group, index) =>
|
For(
|
||||||
|
{ each: () => groups, class: "flex flex-col" },
|
||||||
|
(group, index) =>
|
||||||
button(
|
button(
|
||||||
{
|
{
|
||||||
class:
|
class:
|
||||||
selectedGroupIndex() === index() ? "bg-blue text-white" : "",
|
selectedGroupIndex() === index()
|
||||||
|
? "bg-blue text-white"
|
||||||
|
: "",
|
||||||
onClick: () => setSelectedGroupIndex(index()),
|
onClick: () => setSelectedGroupIndex(index()),
|
||||||
},
|
},
|
||||||
group.groupName,
|
group.groupName,
|
||||||
@@ -489,7 +507,10 @@ const AccessControlTUI = () => {
|
|||||||
* Toast Configuration Tab Factory
|
* Toast Configuration Tab Factory
|
||||||
*/
|
*/
|
||||||
const createToastTab = (
|
const createToastTab = (
|
||||||
toastType: "welcomeToastConfig" | "warnToastConfig" | "noticeToastConfig",
|
toastType:
|
||||||
|
| "welcomeToastConfig"
|
||||||
|
| "warnToastConfig"
|
||||||
|
| "noticeToastConfig",
|
||||||
) => {
|
) => {
|
||||||
return () => {
|
return () => {
|
||||||
const toastConfig = config()[toastType];
|
const toastConfig = config()[toastType];
|
||||||
@@ -533,7 +554,9 @@ const AccessControlTUI = () => {
|
|||||||
} catch {
|
} catch {
|
||||||
setTempToastConfig({
|
setTempToastConfig({
|
||||||
...getTempToastConfig(),
|
...getTempToastConfig(),
|
||||||
title: textutils.serialiseJSON(currentToastConfig.title),
|
title: textutils.serialiseJSON(
|
||||||
|
currentToastConfig.title,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -545,7 +568,10 @@ const AccessControlTUI = () => {
|
|||||||
type: "text",
|
type: "text",
|
||||||
value: () => getTempToastConfig().msg,
|
value: () => getTempToastConfig().msg,
|
||||||
onInput: (value) =>
|
onInput: (value) =>
|
||||||
setTempToastConfig({ ...getTempToastConfig(), msg: value }),
|
setTempToastConfig({
|
||||||
|
...getTempToastConfig(),
|
||||||
|
msg: value,
|
||||||
|
}),
|
||||||
onFocusChanged: () => {
|
onFocusChanged: () => {
|
||||||
const currentToastConfig = config()[toastType];
|
const currentToastConfig = config()[toastType];
|
||||||
|
|
||||||
@@ -566,7 +592,9 @@ const AccessControlTUI = () => {
|
|||||||
} catch {
|
} catch {
|
||||||
setTempToastConfig({
|
setTempToastConfig({
|
||||||
...getTempToastConfig(),
|
...getTempToastConfig(),
|
||||||
msg: textutils.serialiseJSON(currentToastConfig.msg),
|
msg: textutils.serialiseJSON(
|
||||||
|
currentToastConfig.msg,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
// Invalid JSON, ignore
|
// Invalid JSON, ignore
|
||||||
}
|
}
|
||||||
@@ -579,13 +607,19 @@ const AccessControlTUI = () => {
|
|||||||
input({
|
input({
|
||||||
type: "text",
|
type: "text",
|
||||||
value: () => {
|
value: () => {
|
||||||
const str = textutils.serialiseJSON(getTempToastConfig().prefix, {
|
const str = textutils.serialiseJSON(
|
||||||
|
getTempToastConfig().prefix,
|
||||||
|
{
|
||||||
unicode_strings: true,
|
unicode_strings: true,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
return str.substring(1, str.length - 1);
|
return str.substring(1, str.length - 1);
|
||||||
},
|
},
|
||||||
onInput: (value) =>
|
onInput: (value) =>
|
||||||
setTempToastConfig({ ...getTempToastConfig(), prefix: value }),
|
setTempToastConfig({
|
||||||
|
...getTempToastConfig(),
|
||||||
|
prefix: value,
|
||||||
|
}),
|
||||||
onFocusChanged: () => {
|
onFocusChanged: () => {
|
||||||
const currentToastConfig = config()[toastType];
|
const currentToastConfig = config()[toastType];
|
||||||
setConfig(toastType, {
|
setConfig(toastType, {
|
||||||
@@ -603,7 +637,10 @@ const AccessControlTUI = () => {
|
|||||||
type: "text",
|
type: "text",
|
||||||
value: () => getTempToastConfig().brackets,
|
value: () => getTempToastConfig().brackets,
|
||||||
onInput: (value) =>
|
onInput: (value) =>
|
||||||
setTempToastConfig({ ...getTempToastConfig(), brackets: value }),
|
setTempToastConfig({
|
||||||
|
...getTempToastConfig(),
|
||||||
|
brackets: value,
|
||||||
|
}),
|
||||||
onFocusChanged: () => {
|
onFocusChanged: () => {
|
||||||
const currentToastConfig = config()[toastType];
|
const currentToastConfig = config()[toastType];
|
||||||
setConfig(toastType, {
|
setConfig(toastType, {
|
||||||
@@ -678,7 +715,10 @@ const AccessControlTUI = () => {
|
|||||||
{ when: () => currentTab() === TABS.WELCOME_TOAST },
|
{ when: () => currentTab() === TABS.WELCOME_TOAST },
|
||||||
WelcomeToastTab(),
|
WelcomeToastTab(),
|
||||||
),
|
),
|
||||||
Match({ when: () => currentTab() === TABS.WARN_TOAST }, WarnToastTab()),
|
Match(
|
||||||
|
{ when: () => currentTab() === TABS.WARN_TOAST },
|
||||||
|
WarnToastTab(),
|
||||||
|
),
|
||||||
Match(
|
Match(
|
||||||
{ when: () => currentTab() === TABS.NOTICE_TOAST },
|
{ when: () => currentTab() === TABS.NOTICE_TOAST },
|
||||||
NoticeToastTab(),
|
NoticeToastTab(),
|
||||||
@@ -703,7 +743,10 @@ const AccessControlTUI = () => {
|
|||||||
For({ each: () => tabNames }, (tabName, index) =>
|
For({ each: () => tabNames }, (tabName, index) =>
|
||||||
button(
|
button(
|
||||||
{
|
{
|
||||||
class: currentTab() === index() ? "bg-blue text-white" : "",
|
class:
|
||||||
|
currentTab() === index()
|
||||||
|
? "bg-blue text-white"
|
||||||
|
: "",
|
||||||
onClick: () => setCurrentTab(index() as TabIndex),
|
onClick: () => setCurrentTab(index() as TabIndex),
|
||||||
},
|
},
|
||||||
tabName,
|
tabName,
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import {
|
|||||||
import { Queue } from "@/lib/datatype/Queue";
|
import { Queue } from "@/lib/datatype/Queue";
|
||||||
import {
|
import {
|
||||||
ConsoleStream,
|
ConsoleStream,
|
||||||
|
DAY,
|
||||||
|
FileStream,
|
||||||
Logger,
|
Logger,
|
||||||
LogLevel,
|
LogLevel,
|
||||||
processor,
|
processor,
|
||||||
@@ -18,7 +20,17 @@ const logger = new Logger({
|
|||||||
processor.addTimestamp(),
|
processor.addTimestamp(),
|
||||||
],
|
],
|
||||||
renderer: textRenderer,
|
renderer: textRenderer,
|
||||||
streams: [new ConsoleStream()],
|
streams: [
|
||||||
|
new ConsoleStream(),
|
||||||
|
new FileStream({
|
||||||
|
filePath: "autocraft.log",
|
||||||
|
rotationInterval: DAY,
|
||||||
|
autoCleanup: {
|
||||||
|
enabled: true,
|
||||||
|
maxFiles: 3,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const peripheralsNames = {
|
const peripheralsNames = {
|
||||||
@@ -47,7 +59,8 @@ enum State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
while (true) {
|
let isFinishedInitPeripheral = false;
|
||||||
|
while (!isFinishedInitPeripheral) {
|
||||||
try {
|
try {
|
||||||
packsInventory = peripheral.wrap(
|
packsInventory = peripheral.wrap(
|
||||||
peripheralsNames.packsInventory,
|
peripheralsNames.packsInventory,
|
||||||
@@ -67,7 +80,7 @@ function main() {
|
|||||||
turtleLocalName = wiredModem.getNameLocal();
|
turtleLocalName = wiredModem.getNameLocal();
|
||||||
|
|
||||||
logger.info("Peripheral initialization complete...");
|
logger.info("Peripheral initialization complete...");
|
||||||
break;
|
isFinishedInitPeripheral = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Peripheral initialization failed for ${String(error)}, try again...`,
|
`Peripheral initialization failed for ${String(error)}, try again...`,
|
||||||
|
|||||||
@@ -398,11 +398,17 @@ export class UIObject {
|
|||||||
|
|
||||||
const newScrollX = Math.max(
|
const newScrollX = Math.max(
|
||||||
0,
|
0,
|
||||||
Math.min(this.scrollProps.maxScrollX, this.scrollProps.scrollX + deltaX),
|
Math.min(
|
||||||
|
this.scrollProps.maxScrollX,
|
||||||
|
this.scrollProps.scrollX + deltaX,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
const newScrollY = Math.max(
|
const newScrollY = Math.max(
|
||||||
0,
|
0,
|
||||||
Math.min(this.scrollProps.maxScrollY, this.scrollProps.scrollY + deltaY),
|
Math.min(
|
||||||
|
this.scrollProps.maxScrollY,
|
||||||
|
this.scrollProps.scrollY + deltaY,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
this.scrollProps.scrollX = newScrollX;
|
this.scrollProps.scrollX = newScrollX;
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
import { UIObject } from "./UIObject";
|
import { UIObject } from "./UIObject";
|
||||||
import { calculateLayout } from "./layout";
|
import { calculateLayout } from "./layout";
|
||||||
import { render as renderTree, clearScreen } from "./renderer";
|
import { render as renderTree, clearScreen } from "./renderer";
|
||||||
import { CCLog, DAY, LogLevel } from "../ccLog";
|
|
||||||
import { setLogger } from "./context";
|
|
||||||
import { InputProps } from "./components";
|
import { InputProps } from "./components";
|
||||||
import { Setter } from "./reactivity";
|
import { Setter } from "./reactivity";
|
||||||
|
import { getStructLogger, Logger } from "@/lib/ccStructLog";
|
||||||
|
import { setLogger } from "./context";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main application class
|
* Main application class
|
||||||
@@ -21,7 +21,7 @@ export class Application {
|
|||||||
private focusedNode?: UIObject;
|
private focusedNode?: UIObject;
|
||||||
private termWidth: number;
|
private termWidth: number;
|
||||||
private termHeight: number;
|
private termHeight: number;
|
||||||
private logger: CCLog;
|
private logger: Logger;
|
||||||
private cursorBlinkState = false;
|
private cursorBlinkState = false;
|
||||||
private lastBlinkTime = 0;
|
private lastBlinkTime = 0;
|
||||||
private readonly BLINK_INTERVAL = 0.5; // seconds
|
private readonly BLINK_INTERVAL = 0.5; // seconds
|
||||||
@@ -30,11 +30,7 @@ export class Application {
|
|||||||
const [width, height] = term.getSize();
|
const [width, height] = term.getSize();
|
||||||
this.termWidth = width;
|
this.termWidth = width;
|
||||||
this.termHeight = height;
|
this.termHeight = height;
|
||||||
this.logger = new CCLog("tui_debug.log", {
|
this.logger = getStructLogger("ccTUI");
|
||||||
printTerminal: false,
|
|
||||||
logInterval: DAY,
|
|
||||||
outputMinLevel: LogLevel.Info,
|
|
||||||
});
|
|
||||||
setLogger(this.logger);
|
setLogger(this.logger);
|
||||||
this.logger.debug("Application constructed.");
|
this.logger.debug("Application constructed.");
|
||||||
}
|
}
|
||||||
@@ -99,7 +95,6 @@ export class Application {
|
|||||||
this.root.unmount();
|
this.root.unmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.close();
|
|
||||||
clearScreen();
|
clearScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,8 +219,10 @@ export class Application {
|
|||||||
| undefined;
|
| undefined;
|
||||||
if (type === "checkbox") {
|
if (type === "checkbox") {
|
||||||
// Toggle checkbox
|
// Toggle checkbox
|
||||||
const onChangeProp = (this.focusedNode.props as InputProps).onChange;
|
const onChangeProp = (this.focusedNode.props as InputProps)
|
||||||
const checkedProp = (this.focusedNode.props as InputProps).checked;
|
.onChange;
|
||||||
|
const checkedProp = (this.focusedNode.props as InputProps)
|
||||||
|
.checked;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof onChangeProp === "function" &&
|
typeof onChangeProp === "function" &&
|
||||||
@@ -260,7 +257,10 @@ export class Application {
|
|||||||
const valueProp = (this.focusedNode.props as InputProps).value;
|
const valueProp = (this.focusedNode.props as InputProps).value;
|
||||||
const onInputProp = (this.focusedNode.props as InputProps).onInput;
|
const onInputProp = (this.focusedNode.props as InputProps).onInput;
|
||||||
|
|
||||||
if (typeof valueProp !== "function" || typeof onInputProp !== "function") {
|
if (
|
||||||
|
typeof valueProp !== "function" ||
|
||||||
|
typeof onInputProp !== "function"
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +273,10 @@ export class Application {
|
|||||||
this.needsRender = true;
|
this.needsRender = true;
|
||||||
} else if (key === keys.right) {
|
} else if (key === keys.right) {
|
||||||
// Move cursor right
|
// Move cursor right
|
||||||
this.focusedNode.cursorPos = math.min(currentValue.length, cursorPos + 1);
|
this.focusedNode.cursorPos = math.min(
|
||||||
|
currentValue.length,
|
||||||
|
cursorPos + 1,
|
||||||
|
);
|
||||||
this.needsRender = true;
|
this.needsRender = true;
|
||||||
} else if (key === keys.backspace) {
|
} else if (key === keys.backspace) {
|
||||||
// Delete character before cursor
|
// Delete character before cursor
|
||||||
@@ -301,11 +304,15 @@ export class Application {
|
|||||||
* Handle character input events
|
* Handle character input events
|
||||||
*/
|
*/
|
||||||
private handleCharEvent(char: string): void {
|
private handleCharEvent(char: string): void {
|
||||||
if (this.focusedNode !== undefined && this.focusedNode.type === "input") {
|
if (
|
||||||
|
this.focusedNode !== undefined &&
|
||||||
|
this.focusedNode.type === "input"
|
||||||
|
) {
|
||||||
const type = (this.focusedNode.props as InputProps).type;
|
const type = (this.focusedNode.props as InputProps).type;
|
||||||
if (type !== "checkbox") {
|
if (type !== "checkbox") {
|
||||||
// Insert character at cursor position
|
// Insert character at cursor position
|
||||||
const onInputProp = (this.focusedNode.props as InputProps).onInput;
|
const onInputProp = (this.focusedNode.props as InputProps)
|
||||||
|
.onInput;
|
||||||
const valueProp = (this.focusedNode.props as InputProps).value;
|
const valueProp = (this.focusedNode.props as InputProps).value;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -338,7 +345,10 @@ export class Application {
|
|||||||
|
|
||||||
if (clicked !== undefined) {
|
if (clicked !== undefined) {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
string.format("handleMouseClick: Found node of type %s.", clicked.type),
|
string.format(
|
||||||
|
"handleMouseClick: Found node of type %s.",
|
||||||
|
clicked.type,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
// Set focus
|
// Set focus
|
||||||
if (
|
if (
|
||||||
@@ -351,7 +361,8 @@ export class Application {
|
|||||||
}
|
}
|
||||||
this.focusedNode = clicked;
|
this.focusedNode = clicked;
|
||||||
if (typeof clicked.props.onFocusChanged === "function") {
|
if (typeof clicked.props.onFocusChanged === "function") {
|
||||||
const onFocusChanged = clicked.props.onFocusChanged as Setter<boolean>;
|
const onFocusChanged = clicked.props
|
||||||
|
.onFocusChanged as Setter<boolean>;
|
||||||
onFocusChanged(true);
|
onFocusChanged(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,11 +386,15 @@ export class Application {
|
|||||||
"handleMouseClick: onClick handler found, executing.",
|
"handleMouseClick: onClick handler found, executing.",
|
||||||
);
|
);
|
||||||
(onClick as () => void)();
|
(onClick as () => void)();
|
||||||
this.logger.debug("handleMouseClick: onClick handler finished.");
|
this.logger.debug(
|
||||||
|
"handleMouseClick: onClick handler finished.",
|
||||||
|
);
|
||||||
this.needsRender = true;
|
this.needsRender = true;
|
||||||
}
|
}
|
||||||
} else if (clicked.type === "input") {
|
} else if (clicked.type === "input") {
|
||||||
const type = (clicked.props as InputProps).type as string | undefined;
|
const type = (clicked.props as InputProps).type as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
if (type === "checkbox") {
|
if (type === "checkbox") {
|
||||||
const onChangeProp = (clicked.props as InputProps).onChange;
|
const onChangeProp = (clicked.props as InputProps).onChange;
|
||||||
const checkedProp = (clicked.props as InputProps).checked;
|
const checkedProp = (clicked.props as InputProps).checked;
|
||||||
@@ -397,7 +412,9 @@ export class Application {
|
|||||||
|
|
||||||
this.needsRender = true;
|
this.needsRender = true;
|
||||||
} else {
|
} else {
|
||||||
this.logger.debug("handleMouseClick: No node found at click position.");
|
this.logger.debug(
|
||||||
|
"handleMouseClick: No node found at click position.",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,7 +449,9 @@ export class Application {
|
|||||||
);
|
);
|
||||||
// Only return interactive elements
|
// Only return interactive elements
|
||||||
if (node.type === "button" || node.type === "input") {
|
if (node.type === "button" || node.type === "input") {
|
||||||
this.logger.debug("findNodeAt: Node is interactive, returning.");
|
this.logger.debug(
|
||||||
|
"findNodeAt: Node is interactive, returning.",
|
||||||
|
);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -502,7 +521,9 @@ export class Application {
|
|||||||
);
|
);
|
||||||
// Only return scrollable elements
|
// Only return scrollable elements
|
||||||
if (node.type === "scroll-container") {
|
if (node.type === "scroll-container") {
|
||||||
this.logger.debug("findNodeAt: Node is scrollable, returning.");
|
this.logger.debug(
|
||||||
|
"findNodeAt: Node is scrollable, returning.",
|
||||||
|
);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,12 +167,15 @@ export function label(
|
|||||||
const sentences = createMemo(() => {
|
const sentences = createMemo(() => {
|
||||||
const words = splitByWhitespace(text());
|
const words = splitByWhitespace(text());
|
||||||
const ret = concatSentence(words, 40);
|
const ret = concatSentence(words, 40);
|
||||||
context.logger?.debug(`label words changed : [ ${ret.join(",")} ]`);
|
context.logger?.debug(
|
||||||
|
`label words changed : [ ${ret.join(",")} ]`,
|
||||||
|
);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
const forNode = For({ class: `flex flex-col`, each: sentences }, (word) =>
|
const forNode = For(
|
||||||
label({ class: p.class }, word),
|
{ class: `flex flex-col`, each: sentences },
|
||||||
|
(word) => label({ class: p.class }, word),
|
||||||
);
|
);
|
||||||
|
|
||||||
return forNode;
|
return forNode;
|
||||||
|
|||||||
@@ -4,13 +4,13 @@
|
|||||||
* to all components without prop drilling.
|
* to all components without prop drilling.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { CCLog } from "../ccLog";
|
import { Logger } from "@/lib/ccStructLog";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The global context object for the TUI application.
|
* The global context object for the TUI application.
|
||||||
* This will be set by the Application instance on creation.
|
* This will be set by the Application instance on creation.
|
||||||
*/
|
*/
|
||||||
export const context: { logger: CCLog | undefined } = {
|
export const context: { logger: Logger | undefined } = {
|
||||||
logger: undefined,
|
logger: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -18,6 +18,6 @@ export const context: { logger: CCLog | undefined } = {
|
|||||||
* Sets the global logger instance.
|
* Sets the global logger instance.
|
||||||
* @param l The logger instance.
|
* @param l The logger instance.
|
||||||
*/
|
*/
|
||||||
export function setLogger(l: CCLog): void {
|
export function setLogger(l: Logger): void {
|
||||||
context.logger = l;
|
context.logger = l;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,10 @@ function measureNode(
|
|||||||
if (node.styleProps.width === "screen") {
|
if (node.styleProps.width === "screen") {
|
||||||
const termSize = getTerminalSize();
|
const termSize = getTerminalSize();
|
||||||
measuredWidth = termSize.width;
|
measuredWidth = termSize.width;
|
||||||
} else if (node.styleProps.width === "full" && parentWidth !== undefined) {
|
} else if (
|
||||||
|
node.styleProps.width === "full" &&
|
||||||
|
parentWidth !== undefined
|
||||||
|
) {
|
||||||
measuredWidth = parentWidth;
|
measuredWidth = parentWidth;
|
||||||
} else if (typeof node.styleProps.width === "number") {
|
} else if (typeof node.styleProps.width === "number") {
|
||||||
measuredWidth = node.styleProps.width;
|
measuredWidth = node.styleProps.width;
|
||||||
@@ -297,7 +300,13 @@ export function calculateLayout(
|
|||||||
const childY = startY + scrollOffsetY;
|
const childY = startY + scrollOffsetY;
|
||||||
|
|
||||||
// Recursively calculate layout for child with its natural size
|
// Recursively calculate layout for child with its natural size
|
||||||
calculateLayout(child, childSize.width, childSize.height, childX, childY);
|
calculateLayout(
|
||||||
|
child,
|
||||||
|
childSize.width,
|
||||||
|
childSize.height,
|
||||||
|
childX,
|
||||||
|
childY,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -368,7 +377,8 @@ export function calculateLayout(
|
|||||||
|
|
||||||
// Cross axis (vertical) alignment
|
// Cross axis (vertical) alignment
|
||||||
if (align === "center") {
|
if (align === "center") {
|
||||||
childY = startY + math.floor((availableHeight - measure.height) / 2);
|
childY =
|
||||||
|
startY + math.floor((availableHeight - measure.height) / 2);
|
||||||
} else if (align === "end") {
|
} else if (align === "end") {
|
||||||
childY = startY + (availableHeight - measure.height);
|
childY = startY + (availableHeight - measure.height);
|
||||||
} else {
|
} else {
|
||||||
@@ -385,7 +395,8 @@ export function calculateLayout(
|
|||||||
|
|
||||||
// Cross axis (horizontal) alignment
|
// Cross axis (horizontal) alignment
|
||||||
if (align === "center") {
|
if (align === "center") {
|
||||||
childX = startX + math.floor((availableWidth - measure.width) / 2);
|
childX =
|
||||||
|
startX + math.floor((availableWidth - measure.width) / 2);
|
||||||
} else if (align === "end") {
|
} else if (align === "end") {
|
||||||
childX = startX + (availableWidth - measure.width);
|
childX = startX + (availableWidth - measure.width);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -75,10 +75,10 @@ export function createSignal<T>(initialValue: T): Signal<T> {
|
|||||||
// Notify all subscribed listeners
|
// Notify all subscribed listeners
|
||||||
if (batchDepth > 0) {
|
if (batchDepth > 0) {
|
||||||
// In batch mode, collect effects to run later
|
// In batch mode, collect effects to run later
|
||||||
listeners.forEach(listener => pendingEffects.add(listener));
|
listeners.forEach((listener) => pendingEffects.add(listener));
|
||||||
} else {
|
} else {
|
||||||
// Run effects immediately
|
// Run effects immediately
|
||||||
listeners.forEach(listener => {
|
listeners.forEach((listener) => {
|
||||||
try {
|
try {
|
||||||
listener();
|
listener();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -152,7 +152,7 @@ export function batch(fn: () => void): void {
|
|||||||
const effects = Array.from(pendingEffects);
|
const effects = Array.from(pendingEffects);
|
||||||
pendingEffects.clear();
|
pendingEffects.clear();
|
||||||
|
|
||||||
effects.forEach(effect => {
|
effects.forEach((effect) => {
|
||||||
try {
|
try {
|
||||||
effect();
|
effect();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ function getTextContent(node: UIObject): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For nodes with text children, get their content
|
// For nodes with text children, get their content
|
||||||
if (node.children.length > 0 && node.children[0].textContent !== undefined) {
|
if (
|
||||||
|
node.children.length > 0 &&
|
||||||
|
node.children[0].textContent !== undefined
|
||||||
|
) {
|
||||||
const child = node.children[0];
|
const child = node.children[0];
|
||||||
if (typeof child.textContent === "function") {
|
if (typeof child.textContent === "function") {
|
||||||
return child.textContent();
|
return child.textContent();
|
||||||
@@ -39,7 +42,11 @@ function isPositionVisible(
|
|||||||
): boolean {
|
): boolean {
|
||||||
let current = node.parent;
|
let current = node.parent;
|
||||||
while (current) {
|
while (current) {
|
||||||
if (isScrollContainer(current) && current.layout && current.scrollProps) {
|
if (
|
||||||
|
isScrollContainer(current) &&
|
||||||
|
current.layout &&
|
||||||
|
current.scrollProps
|
||||||
|
) {
|
||||||
const { x: containerX, y: containerY } = current.layout;
|
const { x: containerX, y: containerY } = current.layout;
|
||||||
const { viewportWidth, viewportHeight } = current.scrollProps;
|
const { viewportWidth, viewportHeight } = current.scrollProps;
|
||||||
|
|
||||||
@@ -189,7 +196,9 @@ function drawNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "input": {
|
case "input": {
|
||||||
const type = (node.props as InputProps).type as string | undefined;
|
const type = (node.props as InputProps).type as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
|
|
||||||
if (type === "checkbox") {
|
if (type === "checkbox") {
|
||||||
// Draw checkbox
|
// Draw checkbox
|
||||||
@@ -224,7 +233,11 @@ function drawNode(
|
|||||||
const focusedBgColor = bgColor ?? colors.white;
|
const focusedBgColor = bgColor ?? colors.white;
|
||||||
const unfocusedBgColor = bgColor ?? colors.black;
|
const unfocusedBgColor = bgColor ?? colors.black;
|
||||||
|
|
||||||
if (displayText === "" && placeholder !== undefined && !focused) {
|
if (
|
||||||
|
displayText === "" &&
|
||||||
|
placeholder !== undefined &&
|
||||||
|
!focused
|
||||||
|
) {
|
||||||
displayText = placeholder;
|
displayText = placeholder;
|
||||||
showPlaceholder = true;
|
showPlaceholder = true;
|
||||||
currentTextColor = currentTextColor ?? colors.gray;
|
currentTextColor = currentTextColor ?? colors.gray;
|
||||||
@@ -235,7 +248,9 @@ function drawNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set background and clear the input area, creating a 1-character padding on the left
|
// Set background and clear the input area, creating a 1-character padding on the left
|
||||||
term.setBackgroundColor(focused ? focusedBgColor : unfocusedBgColor);
|
term.setBackgroundColor(
|
||||||
|
focused ? focusedBgColor : unfocusedBgColor,
|
||||||
|
);
|
||||||
term.setCursorPos(x, y);
|
term.setCursorPos(x, y);
|
||||||
term.write(" ".repeat(width));
|
term.write(" ".repeat(width));
|
||||||
|
|
||||||
@@ -247,7 +262,9 @@ function drawNode(
|
|||||||
|
|
||||||
// Move text if it's too long for the padded area
|
// Move text if it's too long for the padded area
|
||||||
const startDisPos =
|
const startDisPos =
|
||||||
cursorPos >= renderWidth ? cursorPos - renderWidth + 1 : 0;
|
cursorPos >= renderWidth
|
||||||
|
? cursorPos - renderWidth + 1
|
||||||
|
: 0;
|
||||||
const stopDisPos = startDisPos + renderWidth;
|
const stopDisPos = startDisPos + renderWidth;
|
||||||
|
|
||||||
if (focused && !showPlaceholder && cursorBlinkState) {
|
if (focused && !showPlaceholder && cursorBlinkState) {
|
||||||
@@ -271,7 +288,10 @@ function drawNode(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Draw cursor at the end of the text if applicable
|
// Draw cursor at the end of the text if applicable
|
||||||
if (cursorPos === textToRender.length && cursorPos < renderWidth) {
|
if (
|
||||||
|
cursorPos === textToRender.length &&
|
||||||
|
cursorPos < renderWidth
|
||||||
|
) {
|
||||||
term.setBackgroundColor(currentTextColor);
|
term.setBackgroundColor(currentTextColor);
|
||||||
term.setTextColor(focusedBgColor);
|
term.setTextColor(focusedBgColor);
|
||||||
term.write(" ");
|
term.write(" ");
|
||||||
@@ -281,7 +301,9 @@ function drawNode(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Not focused or no cursor, just write the text
|
// Not focused or no cursor, just write the text
|
||||||
term.write(textToRender.substring(startDisPos, stopDisPos));
|
term.write(
|
||||||
|
textToRender.substring(startDisPos, stopDisPos),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -45,7 +45,9 @@ export interface SetStoreFunction<T> {
|
|||||||
* setTodos([{ title: "First", done: false }]);
|
* setTodos([{ title: "First", done: false }]);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function createStore<T extends object>(initialValue: T): [Accessor<T>, SetStoreFunction<T>] {
|
export function createStore<T extends object>(
|
||||||
|
initialValue: T,
|
||||||
|
): [Accessor<T>, SetStoreFunction<T>] {
|
||||||
// Use a signal to track the entire state
|
// Use a signal to track the entire state
|
||||||
const [get, set] = createSignal(initialValue);
|
const [get, set] = createSignal(initialValue);
|
||||||
|
|
||||||
@@ -88,8 +90,11 @@ export function createStore<T extends object>(initialValue: T): [Accessor<T>, Se
|
|||||||
|
|
||||||
if (Array.isArray(current)) {
|
if (Array.isArray(current)) {
|
||||||
const newArray = [...current] as unknown[];
|
const newArray = [...current] as unknown[];
|
||||||
if (typeof newArray[index] === "object" && newArray[index] !== undefined) {
|
if (
|
||||||
newArray[index] = { ...(newArray[index]!), [key]: value };
|
typeof newArray[index] === "object" &&
|
||||||
|
newArray[index] !== undefined
|
||||||
|
) {
|
||||||
|
newArray[index] = { ...newArray[index]!, [key]: value };
|
||||||
}
|
}
|
||||||
set(newArray as T);
|
set(newArray as T);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user