Skip to content

Commit

Permalink
style: 💄 Tail command improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Dec 3, 2024
1 parent 7f6e419 commit eaec2e4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 19 deletions.
100 changes: 83 additions & 17 deletions packages/cli/src/cmds/components/LogViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useCallback } from "react";
import { Box, Text, useInput, useApp } from "ink";
import chalk from "chalk";
import fs from "node:fs";
import { executeTests } from "../runTests";

interface LogViewerProps {
env: any;
logFilePath: string;
onExit: () => void;
onTest: () => Promise<void>;
onGrep: () => Promise<void>;
onNextLog?: () => void;
onPrevLog?: () => void;
zombieInfo?: {
Expand All @@ -23,7 +23,6 @@ export const LogViewer: React.FC<LogViewerProps> = ({
logFilePath,
onExit,
onTest,
onGrep,
onNextLog,
onPrevLog,
zombieInfo,
Expand All @@ -32,8 +31,50 @@ export const LogViewer: React.FC<LogViewerProps> = ({
const [tailing, setTailing] = useState(true);
const [currentReadPosition, setCurrentReadPosition] = useState(0);
const [isExecutingCommand, setIsExecutingCommand] = useState(false);
const [isGrepMode, setIsGrepMode] = useState(false);
const [grepInput, setGrepInput] = useState(process.env.MOON_GREP || "D01T01");
const [showCursor, setShowCursor] = useState(true);
const { exit } = useApp();

useEffect(() => {
const hideCursor = () => {
process.stdout.write('\x1B[?25l');
};

hideCursor();

const cursorCheck = setInterval(hideCursor, 100);

return () => {
clearInterval(cursorCheck);
process.stdout.write('\x1B[?25h');
};
}, []);

const handleGrepSubmit = useCallback(async () => {
process.env.MOON_RECYCLE = "true";
process.env.MOON_GREP = grepInput;
const opts: any = {
testNamePattern: grepInput,
silent: true,
subDirectory: process.env.MOON_SUBDIR,
};
opts.reporters = ["dot"];

setIsExecutingCommand(true);
removeWatcher();
try {
await executeTests(env, opts);
} finally {
setIsGrepMode(false);
setIsExecutingCommand(false);
if (tailing) {
setupWatcher();
readLog();
}
}
}, [grepInput, env, tailing]);

const resumePauseProse = [
`, ${chalk.bgWhite.black("[p]")} Pause tail`,
`, ${chalk.bgWhite.black("[r]")} Resume tail`,
Expand Down Expand Up @@ -62,6 +103,19 @@ export const LogViewer: React.FC<LogViewerProps> = ({
};

useInput((input, key) => {
if (isGrepMode) {
if (key.return) {
handleGrepSubmit();
} else if (key.escape) {
setIsGrepMode(false);
} else if (key.backspace || key.delete) {
setGrepInput(prev => prev.slice(0, -1));
} else if (input && !key.ctrl && !key.meta) {
setGrepInput(prev => prev + input);
}
return;
}

if (input === "q") {
fs.unwatchFile(logFilePath);
onExit();
Expand Down Expand Up @@ -92,15 +146,7 @@ export const LogViewer: React.FC<LogViewerProps> = ({
}

if (input === "g") {
setIsExecutingCommand(true);
removeWatcher();
onGrep().finally(() => {
setIsExecutingCommand(false);
if (tailing) {
setupWatcher();
readLog();
}
});
setIsGrepMode(true);
}

if (input === "," && onNextLog) {
Expand All @@ -116,6 +162,17 @@ export const LogViewer: React.FC<LogViewerProps> = ({
}
});

// Control blinking cursor for grep mode
useEffect(() => {
const timer = setInterval(() => {
setShowCursor(prev => !prev);
}, 500);

return () => {
clearInterval(timer);
};
}, [isGrepMode]);

const readLog = () => {
if (!tailing) return;

Expand Down Expand Up @@ -166,21 +223,30 @@ export const LogViewer: React.FC<LogViewerProps> = ({
}, [logFilePath, tailing]);

return (
<Box flexDirection="column" height="100%">
<Box flexGrow={1} flexDirection="column">
{logs.slice(-process.stdout.rows + 2).map((line, i) => (
<Box flexDirection="column" height={process.stdout.rows}>
<Box flexGrow={1} flexDirection="column" marginBottom={-1}>
{logs.slice(-process.stdout.rows + 1).map((line, i) => (
<Text key={i}>{line}</Text>
))}
</Box>
{!isExecutingCommand && (
<Box height={1}>
{!isExecutingCommand && !isGrepMode && (
<Box flexDirection="column" margin={0} padding={0}>
<Text dimColor>{"─".repeat(process.stdout.columns)}</Text>
<Text>
{bottomBarBase}
{resumePauseProse[tailing ? 0 : 1]}
{zombieContent}
</Text>
</Box>
)}
{!isExecutingCommand && isGrepMode && (
<Box flexDirection="column" margin={0} padding={0}>
<Text dimColor>{"─".repeat(process.stdout.columns)}</Text>
<Text>
Pattern to filter (ID/Title) [Enter to submit, Esc to cancel]: <Text color="green">{grepInput}</Text><Text color="green">{showCursor ? "▋" : " "}</Text>
</Text>
</Box>
)}
</Box>
);
};
25 changes: 24 additions & 1 deletion packages/cli/src/cmds/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@ import { hideBin } from "yargs/helpers";
import { fetchArtifact, deriveTestIds, generateConfig, type fetchArtifactArgs } from "../internal";
import { main } from "./main";
import { runNetworkCmd } from "./runNetwork";
import { testCmd, testRunArgs } from "./runTests";
import { testCmd } from "./runTests";
import { configSetup } from "../lib/configReader";
dotenv.config();

function handleCursor() {
const hideCursor = "\x1B[?25l";
const showCursor = "\x1B[?25h";

process.stdout.write(hideCursor);

process.on("exit", () => {
process.stdout.write(showCursor);
});

process.on("SIGINT", () => {
process.stdout.write(showCursor);
process.exit(0);
});

process.on("SIGTERM", () => {
process.stdout.write(showCursor);
process.exit(0);
});
}

handleCursor();

configSetup(process.argv);

export type RunCommandArgs = {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/runNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ const resolveTailChoice = async (env: Environment) => {
logFilePath={logFilePath}
onExit={() => resolve()}
onTest={async () => { await resolveTestChoice(env, true) }}
onGrep={async () => { await resolveGrepChoice(env, true) }}
// onGrep={async () => { await resolveGrepChoice(env, true) }}
onNextLog={
zombieNodes
? () => {
Expand Down

0 comments on commit eaec2e4

Please sign in to comment.