Skip to content

Commit cc91979

Browse files
committed
feat(cli): add a shortcut key "y" to copy the Process Output command
1 parent 2eb0f3a commit cc91979

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

‎packages/cli/src/ui/views/ProcessStdoutView.tsx‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Box, Text } from "ink";
33
import { BASH_TIMEOUT_DECREMENT_MS, BASH_TIMEOUT_INCREMENT_MS } from "@vegamo/deepcode-core";
44
import type { BashTimeoutAdjustment } from "@vegamo/deepcode-core";
55
import { useTerminalInput } from "../hooks";
6+
import { writeClipboardTextAsync } from "../core/clipboard";
67

78
import {
89
wrapProcessOutputTitle,
@@ -41,6 +42,7 @@ export const ProcessStdoutView = React.memo(function ProcessStdoutView({
4142
const [scrollOffset, setScrollOffset] = useState(0);
4243
const [statusMessage, setStatusMessage] = useState("");
4344
const statusTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
45+
const copyRequestRef = useRef(0);
4446

4547
const panelHeight = Math.max(MIN_PANEL_HEIGHT, Math.min(screenHeight - 1, MAX_PANEL_HEIGHT));
4648
const titleLines = useMemo(
@@ -60,6 +62,10 @@ export const ProcessStdoutView = React.memo(function ProcessStdoutView({
6062
useEffect(() => {
6163
setScrollOffset(0);
6264
setStatusMessage("");
65+
return () => {
66+
copyRequestRef.current += 1;
67+
if (statusTimerRef.current) clearTimeout(statusTimerRef.current);
68+
};
6369
}, [selected?.id]);
6470

6571
useEffect(() => {
@@ -74,14 +80,6 @@ export const ProcessStdoutView = React.memo(function ProcessStdoutView({
7480
return () => clearInterval(interval);
7581
}, [processStdoutRef, selected?.id, selected?.running, selected?.pid]);
7682

77-
useEffect(() => {
78-
return () => {
79-
if (statusTimerRef.current) {
80-
clearTimeout(statusTimerRef.current);
81-
}
82-
};
83-
}, []);
84-
8583
const lines = useMemo(
8684
() => [...(pinTitle ? [] : titleLines), ...(selected ? wrapProcessOutputLines(stdoutText, screenWidth - 2) : [])],
8785
[pinTitle, titleLines, selected, stdoutText, screenWidth]
@@ -111,11 +109,27 @@ export const ProcessStdoutView = React.memo(function ProcessStdoutView({
111109
useTerminalInput(
112110
(input, key) => {
113111
if ((key.ctrl && (input === "o" || input === "O")) || key.escape) {
112+
copyRequestRef.current++;
114113
onDismiss();
115114
return;
116115
}
117116
if (key.leftArrow || key.rightArrow) {
118-
setSelectedId(moveProcessOutputSelection(items, selected?.id ?? null, key.leftArrow ? -1 : 1));
117+
const nextId = moveProcessOutputSelection(items, selected?.id ?? null, key.leftArrow ? -1 : 1);
118+
if (nextId !== selected?.id) copyRequestRef.current++;
119+
setSelectedId(nextId);
120+
return;
121+
}
122+
if (input === "y" && !key.ctrl && !key.meta && !key.paste) {
123+
if (!selected?.command) return;
124+
const request = ++copyRequestRef.current;
125+
const reportCopy = (success: boolean) => {
126+
if (request === copyRequestRef.current) {
127+
setTemporaryStatus(success ? "Copied to clipboard" : "Failed to copy");
128+
}
129+
};
130+
void writeClipboardTextAsync(selected.command)
131+
.then(reportCopy)
132+
.catch(() => reportCopy(false));
119133
return;
120134
}
121135
if (input === "+" && canAdjustTimeout && selected?.pid) {
@@ -164,7 +178,7 @@ export const ProcessStdoutView = React.memo(function ProcessStdoutView({
164178
<Text
165179
dimColor
166180
wrap="truncate-end"
167-
>{` (${canAdjustTimeout ? `timeout ${formatDuration(selected.timeoutMs!)} · +/- adjust · ` : ""}←→ history · Ctrl+O or Esc close · ↑↓ PgUp/PgDn scroll)`}</Text>
181+
>{` (${canAdjustTimeout ? `timeout ${formatDuration(selected.timeoutMs!)} · +/- adjust · ` : ""}←→ history · Ctrl+O or Esc close · ↑↓ PgUp/PgDn scroll · y copy command)`}</Text>
168182
</Box>
169183
<Box flexDirection="column" flexShrink={0} paddingX={1} overflow="hidden">
170184
{pinTitle && <Text wrap="hard">{titleLines.join("\n")}</Text>}

0 commit comments

Comments
 (0)