更新用户界面组件,使用 Ink 的 stdin 检测替代原有的 TTY 检测,确保在不同环境下的输入支持。同时,修改启动脚本以确保 Bun 启动时正确处理 TTY 输入,提升用户交互体验。

This commit is contained in:
discountry
2025-09-24 22:58:39 +08:00
parent 0122bcc9ba
commit b2005f2cc4
5 changed files with 27 additions and 9 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ EOF
start_bot() { start_bot() {
# Bun auto-loads .env; no need to run dotenv explicitly # Bun auto-loads .env; no need to run dotenv explicitly
# Ensure Bun has a TTY for interactive menu input # Ensure Bun has a TTY for interactive menu input
exec bun run start < /dev/tty exec bun index.ts < /dev/tty > /dev/tty 2>&1
} }
main "$@" main "$@"
+8 -2
View File
@@ -1,5 +1,5 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { Box, Text, useInput } from "ink"; import { Box, Text, useInput, useStdin } from "ink";
import { TrendApp } from "./TrendApp"; import { TrendApp } from "./TrendApp";
import { MakerApp } from "./MakerApp"; import { MakerApp } from "./MakerApp";
import { OffsetMakerApp } from "./OffsetMakerApp"; import { OffsetMakerApp } from "./OffsetMakerApp";
@@ -32,9 +32,15 @@ const STRATEGIES: StrategyOption[] = [
}, },
]; ];
const inputSupported = Boolean(process.stdin && (process.stdin as any).isTTY); // Prefer Ink's stdin detection to avoid false negatives when spawned by shells
// that don't propagate process.stdin TTY flags correctly.
function useInputSupported() {
const { isRawModeSupported } = useStdin();
return Boolean(isRawModeSupported);
}
export function App() { export function App() {
const inputSupported = useInputSupported();
const [cursor, setCursor] = useState(0); const [cursor, setCursor] = useState(0);
const [selected, setSelected] = useState<StrategyOption | null>(null); const [selected, setSelected] = useState<StrategyOption | null>(null);
+6 -2
View File
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { Box, Text, useInput } from "ink"; import { Box, Text, useInput, useStdin } from "ink";
import { makerConfig } from "../config"; import { makerConfig } from "../config";
import { AsterExchangeAdapter } from "../exchanges/aster-adapter"; import { AsterExchangeAdapter } from "../exchanges/aster-adapter";
import { MakerEngine, type MakerEngineSnapshot } from "../core/maker-engine"; import { MakerEngine, type MakerEngineSnapshot } from "../core/maker-engine";
@@ -10,9 +10,13 @@ interface MakerAppProps {
onExit: () => void; onExit: () => void;
} }
const inputSupported = Boolean(process.stdin && (process.stdin as any).isTTY); function useInputSupported() {
const { isRawModeSupported } = useStdin();
return Boolean(isRawModeSupported);
}
export function MakerApp({ onExit }: MakerAppProps) { export function MakerApp({ onExit }: MakerAppProps) {
const inputSupported = useInputSupported();
const [snapshot, setSnapshot] = useState<MakerEngineSnapshot | null>(null); const [snapshot, setSnapshot] = useState<MakerEngineSnapshot | null>(null);
const [error, setError] = useState<Error | null>(null); const [error, setError] = useState<Error | null>(null);
const engineRef = useRef<MakerEngine | null>(null); const engineRef = useRef<MakerEngine | null>(null);
+6 -2
View File
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { Box, Text, useInput } from "ink"; import { Box, Text, useInput, useStdin } from "ink";
import { makerConfig } from "../config"; import { makerConfig } from "../config";
import { AsterExchangeAdapter } from "../exchanges/aster-adapter"; import { AsterExchangeAdapter } from "../exchanges/aster-adapter";
import { OffsetMakerEngine, type OffsetMakerEngineSnapshot } from "../core/offset-maker-engine"; import { OffsetMakerEngine, type OffsetMakerEngineSnapshot } from "../core/offset-maker-engine";
@@ -10,9 +10,13 @@ interface OffsetMakerAppProps {
onExit: () => void; onExit: () => void;
} }
const inputSupported = Boolean(process.stdin && (process.stdin as any).isTTY); function useInputSupported() {
const { isRawModeSupported } = useStdin();
return Boolean(isRawModeSupported);
}
export function OffsetMakerApp({ onExit }: OffsetMakerAppProps) { export function OffsetMakerApp({ onExit }: OffsetMakerAppProps) {
const inputSupported = useInputSupported();
const [snapshot, setSnapshot] = useState<OffsetMakerEngineSnapshot | null>(null); const [snapshot, setSnapshot] = useState<OffsetMakerEngineSnapshot | null>(null);
const [error, setError] = useState<Error | null>(null); const [error, setError] = useState<Error | null>(null);
const engineRef = useRef<OffsetMakerEngine | null>(null); const engineRef = useRef<OffsetMakerEngine | null>(null);
+6 -2
View File
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { Box, Text, useInput } from "ink"; import { Box, Text, useInput, useStdin } from "ink";
import { tradingConfig } from "../config"; import { tradingConfig } from "../config";
import { AsterExchangeAdapter } from "../exchanges/aster-adapter"; import { AsterExchangeAdapter } from "../exchanges/aster-adapter";
import { TrendEngine, type TrendEngineSnapshot } from "../core/trend-engine"; import { TrendEngine, type TrendEngineSnapshot } from "../core/trend-engine";
@@ -12,9 +12,13 @@ interface TrendAppProps {
onExit: () => void; onExit: () => void;
} }
const inputSupported = Boolean(process.stdin && (process.stdin as any).isTTY); function useInputSupported() {
const { isRawModeSupported } = useStdin();
return Boolean(isRawModeSupported);
}
export function TrendApp({ onExit }: TrendAppProps) { export function TrendApp({ onExit }: TrendAppProps) {
const inputSupported = useInputSupported();
const [snapshot, setSnapshot] = useState<TrendEngineSnapshot | null>(null); const [snapshot, setSnapshot] = useState<TrendEngineSnapshot | null>(null);
const [error, setError] = useState<Error | null>(null); const [error, setError] = useState<Error | null>(null);
const engineRef = useRef<TrendEngine | null>(null); const engineRef = useRef<TrendEngine | null>(null);