import { useCallback, useRef, useState } from '@lynx-js/react'; import { api } from '../api.js'; import { Icon } from '../Icon.js'; import { cv } from '../lynx-input.js'; interface TerminalProps { onBack: () => void; /** 当前是走 Termux 还是系统自带 shell(从 device 工具查询) */ shellChannel: string; onRefreshChannel: () => void; /** win32 / linux / android —— 决定预设命令用哪套 */ platform?: string; } /** 按平台给几条常用命令,点一下就跑,省得在手机上打字 */ const PRESETS_ANDROID = [ { label: '我是谁', command: 'getprop ro.product.model; uname -a' }, { label: 'Python', command: 'python -V' }, { label: '电池', command: 'dumpsys battery | head -8' }, { label: '存储', command: 'df -h /data /sdcard' }, ]; const PRESETS_DESKTOP = [ { label: '系统', command: 'ver' }, { label: 'Python', command: 'python -V' }, { label: '目录', command: 'dir' }, { label: '网络', command: 'ping -n 2 127.0.0.1' }, ]; const PRESETS_POSIX = [ { label: '我是谁', command: 'uname -a; whoami' }, { label: 'Python', command: 'python3 -V' }, { label: '目录', command: 'pwd; ls -la' }, { label: '网络', command: 'ping -c 2 127.0.0.1' }, ]; interface Line { kind: 'cmd' | 'out' | 'err' | 'note'; text: string; } /** 内置几条常用命令,点一下就跑,省得在手机上打字 */ const PRESETS = [ { label: '我是谁', command: 'getprop ro.product.model; uname -a' }, { label: 'Python', command: 'python -V; pip list 2>/dev/null | head -20' }, { label: '文件', command: 'ls -la /sdcard/Android/data/com.doubao.lynxclone/files/tools' }, { label: '网络', command: 'ping -c 2 8.8.8.8' }, ]; /** * 应用内终端。 * * 命令不是直接本地执行的,而是走内核的 shell 工具 —— 这样电脑/手机走的是同一条路: * 手机:内核优先交给 Termux(装了且开了 allow-external-apps),否则退回系统 toybox * 电脑:cmd /c 或 /bin/sh -c * 所以在手机上它其实就是"Termux 的另一个入口",还省得在两个 App 之间来回切。 */ export function TerminalPage(props: TerminalProps) { const { onBack, shellChannel, onRefreshChannel, platform } = props; const presets = platform === 'android' ? PRESETS_ANDROID : platform === 'win32' ? PRESETS_DESKTOP : PRESETS_POSIX; const [lines, setLines] = useState([ { kind: 'note', text: '输入命令回车执行。装了 Termux 的话会自动走 Termux 的 bash。' }, ]); const [draft, setDraft] = useState(''); const [busy, setBusy] = useState(false); const [signal, setSignal] = useState(0); const history = useRef([]); const push = useCallback((line: Line) => { setLines((prev) => [...prev.slice(-200), line]); setSignal((n) => n + 1); }, []); const runCommand = useCallback(async (command: string) => { const cmd = command.trim(); if (!cmd || busy) return; history.current = [cmd, ...history.current.filter((c) => c !== cmd)].slice(0, 30); push({ kind: 'cmd', text: `$ ${cmd}` }); setDraft(''); setBusy(true); try { const res = await api.runTool('shell', { command: cmd, timeout: 60 }); if (!res.ok) { push({ kind: 'err', text: res.error || '执行失败' }); } else { const r = (res.result || {}) as { stdout?: string; stderr?: string; exit_code?: number; via?: string; termux_note?: string }; if (r.stdout) push({ kind: 'out', text: r.stdout.replace(/\s+$/, '') }); if (r.stderr) push({ kind: 'err', text: r.stderr.replace(/\s+$/, '') }); push({ kind: 'note', text: `[退出码 ${r.exit_code ?? '?'}|通道 ${ r.via === 'termux' ? 'Termux' : r.via === 'toybox' ? '系统 shell (toybox)' : r.via === 'cmd' ? 'Windows cmd' : r.via === 'sh' ? '/bin/sh' : r.via || '未知' }]`, }); if (r.termux_note) push({ kind: 'note', text: r.termux_note }); if (r.via && !termuxReady) onRefreshChannel(); } } catch (err) { push({ kind: 'err', text: `连不上内核:${(err as Error).message}` }); } finally { setBusy(false); onRefreshChannel(); } }, [busy, push, onRefreshChannel]); const handleInput = useCallback((e: { detail: { value: string } }) => setDraft(e.detail.value), []); const handleConfirm = useCallback(() => { runCommand(draft); }, [draft, runCommand]); const termuxReady = shellChannel === 'termux'; const channelText = termuxReady ? '通道:Termux(完整 Linux:bash / apt / pip / node)' : shellChannel === 'cmd' ? '通道:Windows cmd' : shellChannel === 'sh' ? '通道:/bin/sh' : shellChannel === 'toybox' || shellChannel === 'android' ? '通道:系统自带 shell(toybox)。装 Termux 并在其中执行一次 echo "allow-external-apps=true" >> ~/.termux/termux.properties 即可升级' : '通道:未知(内核可能没连上)'; return ( 终端 setLines([])}> {channelText} {presets.map((preset) => ( runCommand(preset.command)}> {preset.label} ))} {lines.map((line, index) => ( {line.text} ))} {busy ? 执行中… : null} ); }