Compare commits
4 Commits
96eb05856e
...
452c2ccf9b
Author | SHA1 | Date | |
---|---|---|---|
452c2ccf9b | |||
830f6e42a8 | |||
b940cfd70d | |||
4588c89557 |
12
bun.lock
12
bun.lock
@@ -50,6 +50,10 @@
|
||||
"remark-gfm": "^4.0.0",
|
||||
"tailwind-merge": "^2.6.0",
|
||||
"tailwindcss": "^4.1.8",
|
||||
"xterm": "^5.3.0",
|
||||
"xterm-addon-search": "^0.13.0",
|
||||
"xterm-addon-unicode11": "^0.6.0",
|
||||
"xterm-addon-web-links": "^0.9.0",
|
||||
"zod": "^3.24.1",
|
||||
"zustand": "^5.0.6",
|
||||
},
|
||||
@@ -1363,6 +1367,14 @@
|
||||
|
||||
"xtend": ["xtend@4.0.2", "", {}, "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="],
|
||||
|
||||
"xterm": ["xterm@5.3.0", "https://registry.npmmirror.com/xterm/-/xterm-5.3.0.tgz", {}, "sha512-8QqjlekLUFTrU6x7xck1MsPzPA571K5zNqWm0M0oroYEWVOptZ0+ubQSkQ3uxIEhcIHRujJy6emDWX4A7qyFzg=="],
|
||||
|
||||
"xterm-addon-search": ["xterm-addon-search@0.13.0", "https://registry.npmmirror.com/xterm-addon-search/-/xterm-addon-search-0.13.0.tgz", { "peerDependencies": { "xterm": "^5.0.0" } }, "sha512-sDUwG4CnqxUjSEFh676DlS3gsh3XYCzAvBPSvJ5OPgF3MRL3iHLPfsb06doRicLC2xXNpeG2cWk8x1qpESWJMA=="],
|
||||
|
||||
"xterm-addon-unicode11": ["xterm-addon-unicode11@0.6.0", "https://registry.npmmirror.com/xterm-addon-unicode11/-/xterm-addon-unicode11-0.6.0.tgz", { "peerDependencies": { "xterm": "^5.0.0" } }, "sha512-5pkb8YoS/deRtNqQRw8t640mu+Ga8B2MG3RXGQu0bwgcfr8XiXIRI880TWM49ICAHhTmnOLPzIIBIjEnCq7k2A=="],
|
||||
|
||||
"xterm-addon-web-links": ["xterm-addon-web-links@0.9.0", "https://registry.npmmirror.com/xterm-addon-web-links/-/xterm-addon-web-links-0.9.0.tgz", { "peerDependencies": { "xterm": "^5.0.0" } }, "sha512-LIzi4jBbPlrKMZF3ihoyqayWyTXAwGfu4yprz1aK2p71e9UKXN6RRzVONR0L+Zd+Ik5tPVI9bwp9e8fDTQh49Q=="],
|
||||
|
||||
"yallist": ["yallist@5.0.0", "", {}, "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw=="],
|
||||
|
||||
"yocto-queue": ["yocto-queue@0.1.0", "https://registry.npmmirror.com/yocto-queue/-/yocto-queue-0.1.0.tgz", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
|
||||
|
@@ -60,6 +60,10 @@
|
||||
"remark-gfm": "^4.0.0",
|
||||
"tailwind-merge": "^2.6.0",
|
||||
"tailwindcss": "^4.1.8",
|
||||
"xterm": "^5.3.0",
|
||||
"xterm-addon-search": "^0.13.0",
|
||||
"xterm-addon-unicode11": "^0.6.0",
|
||||
"xterm-addon-web-links": "^0.9.0",
|
||||
"zod": "^3.24.1",
|
||||
"zustand": "^5.0.6"
|
||||
},
|
||||
|
151
src-tauri/Cargo.lock
generated
151
src-tauri/Cargo.lock
generated
@@ -736,6 +736,7 @@ dependencies = [
|
||||
"notify",
|
||||
"objc",
|
||||
"once_cell",
|
||||
"portable-pty",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"rusqlite",
|
||||
@@ -1250,7 +1251,7 @@ dependencies = [
|
||||
"rustc_version",
|
||||
"toml",
|
||||
"vswhom",
|
||||
"winreg",
|
||||
"winreg 0.55.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1419,10 +1420,21 @@ version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f"
|
||||
dependencies = [
|
||||
"memoffset",
|
||||
"memoffset 0.9.1",
|
||||
"rustc_version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filedescriptor"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e40758ed24c9b2eeb76c35fb0aebc66c626084edd827e07e1552279814c6682d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"thiserror 1.0.69",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.25"
|
||||
@@ -2462,6 +2474,15 @@ dependencies = [
|
||||
"unic-langid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ioctl-rs"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7970510895cee30b3e9128319f2cefd4bde883a39f38baa279567ba3a7eb97d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnet"
|
||||
version = "2.11.0"
|
||||
@@ -2846,6 +2867,15 @@ version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.1"
|
||||
@@ -2980,6 +3010,20 @@ version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f346ff70e7dbfd675fe90590b92d59ef2de15a8779ae305ebcbfd3f0caf59be4"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"bitflags 1.3.2",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"memoffset 0.6.5",
|
||||
"pin-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.30.1"
|
||||
@@ -2990,7 +3034,7 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"cfg_aliases",
|
||||
"libc",
|
||||
"memoffset",
|
||||
"memoffset 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3743,6 +3787,27 @@ dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "portable-pty"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "806ee80c2a03dbe1a9fb9534f8d19e4c0546b790cde8fd1fea9d6390644cb0be"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bitflags 1.3.2",
|
||||
"downcast-rs",
|
||||
"filedescriptor",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"log",
|
||||
"nix 0.25.1",
|
||||
"serial",
|
||||
"shared_library",
|
||||
"shell-words",
|
||||
"winapi",
|
||||
"winreg 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "potential_utf"
|
||||
version = "0.1.2"
|
||||
@@ -4655,6 +4720,48 @@ dependencies = [
|
||||
"unsafe-libyaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1237a96570fc377c13baa1b88c7589ab66edced652e43ffb17088f003db3e86"
|
||||
dependencies = [
|
||||
"serial-core",
|
||||
"serial-unix",
|
||||
"serial-windows",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial-core"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f46209b345401737ae2125fe5b19a77acce90cd53e1658cda928e4fe9a64581"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial-unix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f03fbca4c9d866e24a459cbca71283f545a37f8e3e002ad8c70593871453cab7"
|
||||
dependencies = [
|
||||
"ioctl-rs",
|
||||
"libc",
|
||||
"serial-core",
|
||||
"termios",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial-windows"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15c6d3b776267a75d31bbdfd5d36c0ca051251caafc285827052bc53bcdc8162"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"serial-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serialize-to-javascript"
|
||||
version = "0.1.1"
|
||||
@@ -4708,6 +4815,22 @@ dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shared_library"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shell-words"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
@@ -5494,6 +5617,15 @@ dependencies = [
|
||||
"utf-8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termios"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thin-slice"
|
||||
version = "0.1.1"
|
||||
@@ -5879,7 +6011,7 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89daebc3e6fd160ac4aa9fc8b3bf71e1f74fbf92367ae71fb83a037e8bf164b9"
|
||||
dependencies = [
|
||||
"memoffset",
|
||||
"memoffset 0.9.1",
|
||||
"tempfile",
|
||||
"winapi",
|
||||
]
|
||||
@@ -6874,6 +7006,15 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.55.0"
|
||||
@@ -7075,7 +7216,7 @@ dependencies = [
|
||||
"futures-core",
|
||||
"futures-lite",
|
||||
"hex",
|
||||
"nix",
|
||||
"nix 0.30.1",
|
||||
"ordered-stream",
|
||||
"serde",
|
||||
"serde_repr",
|
||||
|
@@ -48,6 +48,7 @@ reqwest = { version = "0.12", features = ["json", "native-tls-vendored"] }
|
||||
futures = "0.3"
|
||||
async-trait = "0.1"
|
||||
tempfile = "3"
|
||||
portable-pty = "0.8"
|
||||
which = "7"
|
||||
sha2 = "0.10"
|
||||
zstd = "0.13"
|
||||
|
@@ -13,3 +13,4 @@ pub mod relay_adapters;
|
||||
pub mod packycode_nodes;
|
||||
pub mod filesystem;
|
||||
pub mod git;
|
||||
pub mod terminal;
|
||||
|
268
src-tauri/src/commands/terminal.rs
Normal file
268
src-tauri/src/commands/terminal.rs
Normal file
@@ -0,0 +1,268 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
use tokio::sync::Mutex;
|
||||
use uuid::Uuid;
|
||||
use anyhow::Result;
|
||||
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TerminalSession {
|
||||
pub id: String,
|
||||
pub working_directory: String,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
pub is_active: bool,
|
||||
}
|
||||
|
||||
/// Terminal child process wrapper
|
||||
pub struct TerminalChild {
|
||||
writer: Arc<Mutex<Box<dyn Write + Send>>>,
|
||||
}
|
||||
|
||||
/// State for managing terminal sessions
|
||||
pub type TerminalState = Arc<Mutex<HashMap<String, (TerminalSession, Option<TerminalChild>)>>>;
|
||||
|
||||
/// Creates a new terminal session using PTY
|
||||
#[tauri::command]
|
||||
pub async fn create_terminal_session(
|
||||
working_directory: String,
|
||||
app_handle: AppHandle,
|
||||
terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<String, String> {
|
||||
let session_id = Uuid::new_v4().to_string();
|
||||
|
||||
log::info!("Creating terminal session: {} in {}", session_id, working_directory);
|
||||
|
||||
// Check if working directory exists
|
||||
if !std::path::Path::new(&working_directory).exists() {
|
||||
return Err(format!("Working directory does not exist: {}", working_directory));
|
||||
}
|
||||
|
||||
let session = TerminalSession {
|
||||
id: session_id.clone(),
|
||||
working_directory: working_directory.clone(),
|
||||
created_at: chrono::Utc::now(),
|
||||
is_active: true,
|
||||
};
|
||||
|
||||
// Create PTY system
|
||||
let pty_system = native_pty_system();
|
||||
|
||||
// Create PTY pair with size
|
||||
let pty_pair = pty_system.openpty(PtySize {
|
||||
rows: 30,
|
||||
cols: 120,
|
||||
pixel_width: 0,
|
||||
pixel_height: 0,
|
||||
}).map_err(|e| format!("Failed to create PTY: {}", e))?;
|
||||
|
||||
// Get shell command
|
||||
let shell = get_default_shell();
|
||||
let mut cmd = CommandBuilder::new(&shell);
|
||||
|
||||
// Set as login interactive shell
|
||||
if shell.contains("bash") || shell.contains("zsh") {
|
||||
cmd.arg("-il"); // Interactive login shell
|
||||
} else if shell.contains("fish") {
|
||||
cmd.arg("-il");
|
||||
}
|
||||
|
||||
// Set working directory
|
||||
cmd.cwd(working_directory.clone());
|
||||
|
||||
// Set environment variables
|
||||
cmd.env("TERM", "xterm-256color");
|
||||
cmd.env("COLORTERM", "truecolor");
|
||||
cmd.env("LANG", std::env::var("LANG").unwrap_or_else(|_| "en_US.UTF-8".to_string()));
|
||||
cmd.env("LC_ALL", std::env::var("LC_ALL").unwrap_or_else(|_| "en_US.UTF-8".to_string()));
|
||||
cmd.env("LC_CTYPE", std::env::var("LC_CTYPE").unwrap_or_else(|_| "en_US.UTF-8".to_string()));
|
||||
|
||||
// 继承其他环境变量
|
||||
for (key, value) in std::env::vars() {
|
||||
if !key.starts_with("TERM") && !key.starts_with("COLORTERM") && !key.starts_with("LC_") && !key.starts_with("LANG") {
|
||||
cmd.env(&key, &value);
|
||||
}
|
||||
}
|
||||
|
||||
// Spawn the shell process
|
||||
let _child = pty_pair.slave.spawn_command(cmd)
|
||||
.map_err(|e| format!("Failed to spawn shell: {}", e))?;
|
||||
|
||||
// Get writer for stdin
|
||||
let writer = pty_pair.master.take_writer()
|
||||
.map_err(|e| format!("Failed to get PTY writer: {}", e))?;
|
||||
|
||||
// Start reading output in background
|
||||
let session_id_clone = session_id.clone();
|
||||
let app_handle_clone = app_handle.clone();
|
||||
let mut reader = pty_pair.master.try_clone_reader()
|
||||
.map_err(|e| format!("Failed to get PTY reader: {}", e))?;
|
||||
|
||||
// Spawn reader thread
|
||||
std::thread::spawn(move || {
|
||||
let mut buffer = [0u8; 4096];
|
||||
loop {
|
||||
match reader.read(&mut buffer) {
|
||||
Ok(0) => break, // EOF
|
||||
Ok(n) => {
|
||||
let data = String::from_utf8_lossy(&buffer[..n]).to_string();
|
||||
let _ = app_handle_clone.emit(&format!("terminal-output:{}", session_id_clone), &data);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error reading PTY output: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
log::debug!("PTY reader thread finished for session: {}", session_id_clone);
|
||||
});
|
||||
|
||||
// Store the session with PTY writer
|
||||
let terminal_child = TerminalChild {
|
||||
writer: Arc::new(Mutex::new(writer)),
|
||||
};
|
||||
|
||||
{
|
||||
let mut state = terminal_state.lock().await;
|
||||
state.insert(session_id.clone(), (session, Some(terminal_child)));
|
||||
}
|
||||
|
||||
log::info!("Terminal session created successfully: {}", session_id);
|
||||
Ok(session_id)
|
||||
}
|
||||
|
||||
/// Sends input to a terminal session
|
||||
#[tauri::command]
|
||||
pub async fn send_terminal_input(
|
||||
session_id: String,
|
||||
input: String,
|
||||
terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<(), String> {
|
||||
let state = terminal_state.lock().await;
|
||||
|
||||
if let Some((_session, child_opt)) = state.get(&session_id) {
|
||||
if let Some(child) = child_opt {
|
||||
log::debug!("Sending input to terminal {}: {:?}", session_id, input);
|
||||
|
||||
// Write to PTY
|
||||
let mut writer = child.writer.lock().await;
|
||||
writer.write_all(input.as_bytes())
|
||||
.map_err(|e| format!("Failed to write to terminal: {}", e))?;
|
||||
writer.flush()
|
||||
.map_err(|e| format!("Failed to flush terminal input: {}", e))?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("Terminal session not found or not active: {}", session_id))
|
||||
}
|
||||
|
||||
/// Closes a terminal session
|
||||
#[tauri::command]
|
||||
pub async fn close_terminal_session(
|
||||
session_id: String,
|
||||
terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<(), String> {
|
||||
let mut state = terminal_state.lock().await;
|
||||
|
||||
if let Some((mut session, _child)) = state.remove(&session_id) {
|
||||
session.is_active = false;
|
||||
// PTY and child process will be dropped automatically
|
||||
|
||||
log::info!("Closed terminal session: {}", session_id);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Terminal session not found: {}", session_id))
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all active terminal sessions
|
||||
#[tauri::command]
|
||||
pub async fn list_terminal_sessions(
|
||||
terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let state = terminal_state.lock().await;
|
||||
|
||||
let sessions: Vec<String> = state.iter()
|
||||
.filter_map(|(id, (session, _))| {
|
||||
if session.is_active {
|
||||
Some(id.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(sessions)
|
||||
}
|
||||
|
||||
/// Resizes a terminal session
|
||||
#[tauri::command]
|
||||
pub async fn resize_terminal(
|
||||
session_id: String,
|
||||
_cols: u16,
|
||||
_rows: u16,
|
||||
_terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<(), String> {
|
||||
// Note: With the current architecture, resize is not supported
|
||||
// To support resize, we would need to keep a reference to the PTY master
|
||||
// or use a different approach
|
||||
log::warn!("Terminal resize not currently supported for session: {}", session_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cleanup orphaned terminal sessions
|
||||
#[tauri::command]
|
||||
pub async fn cleanup_terminal_sessions(
|
||||
terminal_state: State<'_, TerminalState>,
|
||||
) -> Result<u32, String> {
|
||||
let mut state = terminal_state.lock().await;
|
||||
let mut cleaned_up = 0;
|
||||
|
||||
let mut to_remove = Vec::new();
|
||||
|
||||
for (id, (session, _child)) in state.iter() {
|
||||
if !session.is_active {
|
||||
to_remove.push(id.clone());
|
||||
cleaned_up += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the sessions
|
||||
for id in to_remove {
|
||||
state.remove(&id);
|
||||
}
|
||||
|
||||
if cleaned_up > 0 {
|
||||
log::info!("Cleaned up {} orphaned terminal sessions", cleaned_up);
|
||||
}
|
||||
|
||||
Ok(cleaned_up)
|
||||
}
|
||||
|
||||
/// Get the default shell for the current platform
|
||||
fn get_default_shell() -> String {
|
||||
if cfg!(target_os = "windows") {
|
||||
// Try PowerShell first, fallback to cmd
|
||||
if std::process::Command::new("pwsh").arg("--version").output().is_ok() {
|
||||
"pwsh".to_string()
|
||||
} else if std::process::Command::new("powershell").arg("-Version").output().is_ok() {
|
||||
"powershell".to_string()
|
||||
} else {
|
||||
"cmd".to_string()
|
||||
}
|
||||
} else {
|
||||
// Unix-like systems: try zsh, bash, then sh
|
||||
std::env::var("SHELL").unwrap_or_else(|_| {
|
||||
if std::path::Path::new("/bin/zsh").exists() {
|
||||
"/bin/zsh".to_string()
|
||||
} else if std::path::Path::new("/bin/bash").exists() {
|
||||
"/bin/bash".to_string()
|
||||
} else {
|
||||
"/bin/sh".to_string()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -75,6 +75,10 @@ use commands::filesystem::{
|
||||
use commands::git::{
|
||||
get_git_status, get_git_history, get_git_branches, get_git_diff, get_git_commits,
|
||||
};
|
||||
use commands::terminal::{
|
||||
create_terminal_session, send_terminal_input, close_terminal_session,
|
||||
list_terminal_sessions, resize_terminal, cleanup_terminal_sessions, TerminalState,
|
||||
};
|
||||
use process::ProcessRegistryState;
|
||||
use file_watcher::FileWatcherState;
|
||||
use std::sync::Mutex;
|
||||
@@ -220,6 +224,9 @@ fn main() {
|
||||
app.manage(UsageIndexState::default());
|
||||
app.manage(UsageCacheState::default());
|
||||
|
||||
// Initialize Terminal state
|
||||
app.manage(TerminalState::default());
|
||||
|
||||
// Optionally auto-open DevTools if env var is set (works in packaged builds)
|
||||
if std::env::var("TAURI_OPEN_DEVTOOLS").ok().as_deref() == Some("1") {
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
@@ -402,6 +409,14 @@ fn main() {
|
||||
get_git_branches,
|
||||
get_git_diff,
|
||||
get_git_commits,
|
||||
|
||||
// Terminal
|
||||
create_terminal_session,
|
||||
send_terminal_input,
|
||||
close_terminal_session,
|
||||
list_terminal_sessions,
|
||||
resize_terminal,
|
||||
cleanup_terminal_sessions,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
@@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef, useMemo, useCallback } from "react"
|
||||
import { motion, AnimatePresence } from "framer-motion";
|
||||
import {
|
||||
ArrowLeft,
|
||||
Terminal,
|
||||
Terminal as TerminalIcon,
|
||||
FolderOpen,
|
||||
Copy,
|
||||
ChevronDown,
|
||||
@@ -22,7 +22,8 @@ import {
|
||||
FileText,
|
||||
FilePlus,
|
||||
FileX,
|
||||
Clock
|
||||
Clock,
|
||||
Square
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
@@ -64,6 +65,7 @@ import { FlexLayoutContainer } from "@/components/layout/FlexLayoutContainer";
|
||||
import { MainContentArea } from "@/components/layout/MainContentArea";
|
||||
import { SidePanel } from "@/components/layout/SidePanel";
|
||||
import { ChatView } from "@/components/layout/ChatView";
|
||||
import { Terminal } from "@/components/Terminal";
|
||||
|
||||
interface ClaudeCodeSessionProps {
|
||||
/**
|
||||
@@ -120,7 +122,10 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
openFileEditor,
|
||||
closeFileEditor,
|
||||
openPreview: openLayoutPreview,
|
||||
closePreview: closeLayoutPreview
|
||||
closePreview: closeLayoutPreview,
|
||||
openTerminal,
|
||||
closeTerminal,
|
||||
toggleTerminalMaximize
|
||||
} = layoutManager;
|
||||
|
||||
const [projectPath, setProjectPath] = useState(initialProjectPath || session?.project_path || "");
|
||||
@@ -1389,7 +1394,7 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
<AnimatePresence>
|
||||
{displayableMessages.length === 0 ? (
|
||||
<div className="flex flex-col items-center justify-center h-full min-h-[200px] text-muted-foreground">
|
||||
<Terminal className="h-12 w-12 mb-3 opacity-50" />
|
||||
<TerminalIcon className="h-12 w-12 mb-3 opacity-50" />
|
||||
<p className="text-sm">开始对话或等待消息加载...</p>
|
||||
</div>
|
||||
) : (
|
||||
@@ -1560,6 +1565,29 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
</motion.div>
|
||||
);
|
||||
|
||||
// If terminal is maximized, render only the Terminal in full screen
|
||||
if (layout.activeView === 'terminal' && layout.isTerminalMaximized) {
|
||||
return (
|
||||
<AnimatePresence>
|
||||
<motion.div
|
||||
className="fixed inset-0 z-50 bg-background"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
>
|
||||
<Terminal
|
||||
onClose={closeTerminal}
|
||||
isMaximized={layout.isTerminalMaximized}
|
||||
onToggleMaximize={toggleTerminalMaximize}
|
||||
projectPath={projectPath}
|
||||
className="h-full w-full"
|
||||
/>
|
||||
</motion.div>
|
||||
</AnimatePresence>
|
||||
);
|
||||
}
|
||||
|
||||
// If preview is maximized, render only the WebviewPreview in full screen
|
||||
if (layout.activeView === 'preview' && layout.previewUrl && isPreviewMaximized) {
|
||||
return (
|
||||
@@ -1604,7 +1632,7 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
<ArrowLeft className="h-4 w-4" />
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<Terminal className="h-5 w-5 text-muted-foreground" />
|
||||
<TerminalIcon className="h-5 w-5 text-muted-foreground" />
|
||||
<div className="flex-1">
|
||||
<h1 className="text-xl font-bold">{t('app.claudeCodeSession')}</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
@@ -1624,6 +1652,27 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Terminal Toggle */}
|
||||
{projectPath && (
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={openTerminal}
|
||||
className={cn("h-8 w-8", layout.activeView === 'terminal' && "text-primary")}
|
||||
>
|
||||
<Square className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>终端</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
)}
|
||||
|
||||
{/* File Explorer Toggle */}
|
||||
{projectPath && (
|
||||
<TooltipProvider>
|
||||
@@ -1840,14 +1889,27 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
visible: true,
|
||||
content: (
|
||||
<MainContentArea isEditing={layout.activeView === 'editor'}>
|
||||
{layout.activeView === 'editor' && layout.editingFile ? (
|
||||
// 文件编辑器视图
|
||||
<FileEditorEnhanced
|
||||
filePath={layout.editingFile}
|
||||
onClose={closeFileEditor}
|
||||
className="h-full"
|
||||
{/* 终端始终渲染,通过显示/隐藏控制 */}
|
||||
<div className={cn("absolute inset-0", layout.activeView === 'terminal' ? 'block' : 'hidden')}>
|
||||
<Terminal
|
||||
onClose={closeTerminal}
|
||||
isMaximized={layout.isTerminalMaximized}
|
||||
onToggleMaximize={toggleTerminalMaximize}
|
||||
projectPath={projectPath}
|
||||
className="h-full w-full"
|
||||
/>
|
||||
) : layout.activeView === 'preview' && layout.previewUrl ? (
|
||||
</div>
|
||||
|
||||
{/* 其他视图 */}
|
||||
<div className={cn("h-full w-full", layout.activeView === 'terminal' ? 'hidden' : 'block')}>
|
||||
{layout.activeView === 'editor' && layout.editingFile ? (
|
||||
// 文件编辑器视图
|
||||
<FileEditorEnhanced
|
||||
filePath={layout.editingFile}
|
||||
onClose={closeFileEditor}
|
||||
className="h-full"
|
||||
/>
|
||||
) : layout.activeView === 'preview' && layout.previewUrl ? (
|
||||
// 预览视图
|
||||
<SplitPane
|
||||
left={
|
||||
@@ -1885,24 +1947,24 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
minRightWidth={400}
|
||||
className="h-full"
|
||||
/>
|
||||
) : (
|
||||
// 默认聊天视图
|
||||
<ChatView
|
||||
projectPathInput={projectPathInput}
|
||||
messagesList={messagesList}
|
||||
floatingInput={
|
||||
<div className="w-full max-w-5xl mx-auto px-4">
|
||||
<FloatingPromptInput
|
||||
ref={floatingPromptRef}
|
||||
onSend={handleSendPrompt}
|
||||
onCancel={handleCancelExecution}
|
||||
isLoading={isLoading}
|
||||
disabled={!projectPath}
|
||||
projectPath={projectPath}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
floatingElements={
|
||||
) : (
|
||||
// 默认聊天视图
|
||||
<ChatView
|
||||
projectPathInput={projectPathInput}
|
||||
messagesList={messagesList}
|
||||
floatingInput={
|
||||
<div className="w-full max-w-5xl mx-auto px-4">
|
||||
<FloatingPromptInput
|
||||
ref={floatingPromptRef}
|
||||
onSend={handleSendPrompt}
|
||||
onCancel={handleCancelExecution}
|
||||
isLoading={isLoading}
|
||||
disabled={!projectPath}
|
||||
projectPath={projectPath}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
floatingElements={
|
||||
<>
|
||||
{/* 文件监控展开面板 */}
|
||||
<AnimatePresence>
|
||||
@@ -2055,7 +2117,8 @@ export const ClaudeCodeSession: React.FC<ClaudeCodeSessionProps> = ({
|
||||
</>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
)}
|
||||
</div>
|
||||
</MainContentArea>
|
||||
)
|
||||
},
|
||||
|
459
src/components/Terminal.tsx
Normal file
459
src/components/Terminal.tsx
Normal file
@@ -0,0 +1,459 @@
|
||||
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
||||
import { Terminal as XTerm } from 'xterm';
|
||||
import { WebLinksAddon } from 'xterm-addon-web-links';
|
||||
import { SearchAddon } from 'xterm-addon-search';
|
||||
import 'xterm/css/xterm.css';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { X, Maximize2, Minimize2 } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { api } from '@/lib/api';
|
||||
|
||||
interface TerminalProps {
|
||||
className?: string;
|
||||
onClose?: () => void;
|
||||
isMaximized?: boolean;
|
||||
onToggleMaximize?: () => void;
|
||||
projectPath?: string;
|
||||
}
|
||||
|
||||
export const Terminal: React.FC<TerminalProps> = ({
|
||||
className,
|
||||
onClose,
|
||||
isMaximized = false,
|
||||
onToggleMaximize,
|
||||
projectPath
|
||||
}) => {
|
||||
const terminalRef = useRef<HTMLDivElement>(null);
|
||||
const xtermRef = useRef<XTerm | null>(null);
|
||||
const isInitializedRef = useRef(false);
|
||||
const unlistenRef = useRef<(() => void) | null>(null);
|
||||
const resizeTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [sessionId, setSessionId] = useState<string | null>(null);
|
||||
const [terminalSize, setTerminalSize] = useState({ cols: 80, rows: 24 });
|
||||
const [containerWidth, setContainerWidth] = useState(0);
|
||||
|
||||
// 计算终端应该有的尺寸
|
||||
const calculateOptimalSize = useCallback(() => {
|
||||
if (!terminalRef.current) return { cols: 80, rows: 24 };
|
||||
|
||||
const container = terminalRef.current;
|
||||
const rect = container.getBoundingClientRect();
|
||||
|
||||
// 获取或估算字符尺寸
|
||||
const fontSize = 14; // 我们设置的字体大小
|
||||
const charWidth = fontSize * 0.6; // 等宽字体的典型宽度比例
|
||||
const lineHeight = fontSize * 1.2; // 行高
|
||||
|
||||
// 计算能容纳的最大列数和行数
|
||||
const availableWidth = rect.width - 2;
|
||||
const availableHeight = rect.height - 2;
|
||||
|
||||
const cols = Math.max(80, Math.floor(availableWidth / charWidth));
|
||||
const rows = Math.max(24, Math.floor(availableHeight / lineHeight));
|
||||
|
||||
// 计算实际使用的宽度
|
||||
const usedWidth = cols * charWidth;
|
||||
const unusedWidth = availableWidth - usedWidth;
|
||||
const percentUsed = ((usedWidth / availableWidth) * 100).toFixed(1);
|
||||
|
||||
console.log('[Terminal] Size calculation:', {
|
||||
containerWidth: rect.width,
|
||||
availableWidth,
|
||||
charWidth,
|
||||
cols,
|
||||
usedWidth,
|
||||
unusedWidth,
|
||||
percentUsed: `${percentUsed}%`,
|
||||
message: unusedWidth > 10 ? `还有 ${unusedWidth.toFixed(1)}px 未使用` : '宽度使用正常'
|
||||
});
|
||||
|
||||
return { cols, rows };
|
||||
}, []);
|
||||
|
||||
// 调整终端大小
|
||||
const resizeTerminal = useCallback(() => {
|
||||
if (!xtermRef.current || !terminalRef.current) return;
|
||||
|
||||
// 先尝试获取实际的字符尺寸
|
||||
let actualCharWidth = 8.4; // 默认值
|
||||
let actualLineHeight = 16.8; // 默认值
|
||||
|
||||
try {
|
||||
const core = (xtermRef.current as any)._core;
|
||||
if (core && core._renderService && core._renderService.dimensions) {
|
||||
const dims = core._renderService.dimensions;
|
||||
if (dims.actualCellWidth) actualCharWidth = dims.actualCellWidth;
|
||||
if (dims.actualCellHeight) actualLineHeight = dims.actualCellHeight;
|
||||
|
||||
console.log('[Terminal] Using actual char dimensions:', {
|
||||
actualCharWidth,
|
||||
actualLineHeight
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// 使用默认值
|
||||
}
|
||||
|
||||
// 使用实际字符尺寸计算新的列数和行数
|
||||
const rect = terminalRef.current.getBoundingClientRect();
|
||||
const availableWidth = rect.width - 2;
|
||||
const availableHeight = rect.height - 2;
|
||||
|
||||
// 更新容器宽度显示
|
||||
setContainerWidth(rect.width);
|
||||
|
||||
const newCols = Math.max(80, Math.floor(availableWidth / actualCharWidth));
|
||||
const newRows = Math.max(24, Math.floor(availableHeight / actualLineHeight));
|
||||
|
||||
// 计算宽度使用情况
|
||||
const usedWidth = newCols * actualCharWidth;
|
||||
const unusedWidth = availableWidth - usedWidth;
|
||||
const percentUsed = ((usedWidth / availableWidth) * 100).toFixed(1);
|
||||
|
||||
// 只有当尺寸真的改变时才调整
|
||||
if (newCols !== terminalSize.cols || newRows !== terminalSize.rows) {
|
||||
console.log('[Terminal] Resizing:', {
|
||||
from: terminalSize,
|
||||
to: { cols: newCols, rows: newRows },
|
||||
containerWidth: rect.width,
|
||||
availableWidth,
|
||||
usedWidth,
|
||||
unusedWidth,
|
||||
percentUsed: `${percentUsed}%`
|
||||
});
|
||||
|
||||
setTerminalSize({ cols: newCols, rows: newRows });
|
||||
xtermRef.current.resize(newCols, newRows);
|
||||
|
||||
// 更新后端
|
||||
if (sessionId) {
|
||||
api.resizeTerminal(sessionId, newCols, newRows).catch(console.error);
|
||||
}
|
||||
|
||||
// 强制刷新渲染
|
||||
try {
|
||||
const core = (xtermRef.current as any)._core;
|
||||
if (core && core._renderService && core._renderService._onIntersectionChange) {
|
||||
core._renderService._onIntersectionChange({ intersectionRatio: 1 });
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略错误,某些版本的 xterm 可能没有这个方法
|
||||
}
|
||||
}
|
||||
}, [terminalSize, sessionId]);
|
||||
|
||||
// 防抖的resize处理
|
||||
const handleResize = useCallback(() => {
|
||||
if (resizeTimeoutRef.current) {
|
||||
clearTimeout(resizeTimeoutRef.current);
|
||||
}
|
||||
|
||||
resizeTimeoutRef.current = setTimeout(() => {
|
||||
resizeTerminal();
|
||||
}, 100);
|
||||
}, [resizeTerminal]);
|
||||
|
||||
// 初始化终端
|
||||
useEffect(() => {
|
||||
if (isInitializedRef.current || !terminalRef.current) return;
|
||||
|
||||
let isMounted = true;
|
||||
|
||||
const initializeTerminal = async () => {
|
||||
try {
|
||||
console.log('[Terminal] Initializing...');
|
||||
isInitializedRef.current = true;
|
||||
|
||||
// 先计算初始尺寸
|
||||
const initialSize = calculateOptimalSize();
|
||||
setTerminalSize(initialSize);
|
||||
|
||||
// 创建终端实例
|
||||
const xterm = new XTerm({
|
||||
cols: initialSize.cols,
|
||||
rows: initialSize.rows,
|
||||
theme: {
|
||||
background: '#1e1e1e',
|
||||
foreground: '#d4d4d4',
|
||||
cursor: '#ffffff',
|
||||
cursorAccent: '#000000',
|
||||
selectionBackground: '#264f78',
|
||||
black: '#000000',
|
||||
red: '#cd3131',
|
||||
green: '#0dbc79',
|
||||
yellow: '#e5e510',
|
||||
blue: '#2472c8',
|
||||
magenta: '#bc3fbc',
|
||||
cyan: '#11a8cd',
|
||||
white: '#e5e5e5',
|
||||
brightBlack: '#666666',
|
||||
brightRed: '#f14c4c',
|
||||
brightGreen: '#23d18b',
|
||||
brightYellow: '#f5f543',
|
||||
brightBlue: '#3b8eea',
|
||||
brightMagenta: '#d670d6',
|
||||
brightCyan: '#29b8db',
|
||||
brightWhite: '#e5e5e5',
|
||||
},
|
||||
fontFamily: '"MesloLGS NF", "JetBrainsMono Nerd Font", "FiraCode Nerd Font", "Hack Nerd Font", "JetBrains Mono", "SF Mono", "Monaco", "Consolas", "Courier New", monospace',
|
||||
fontSize: 14,
|
||||
fontWeight: 'normal',
|
||||
fontWeightBold: 'bold',
|
||||
lineHeight: 1.2,
|
||||
letterSpacing: 0,
|
||||
scrollback: 10000,
|
||||
convertEol: true,
|
||||
cursorBlink: true,
|
||||
cursorStyle: 'block',
|
||||
drawBoldTextInBrightColors: true,
|
||||
macOptionIsMeta: true,
|
||||
rightClickSelectsWord: true,
|
||||
allowProposedApi: true,
|
||||
// @ts-ignore
|
||||
rendererType: 'canvas',
|
||||
windowsMode: false,
|
||||
});
|
||||
|
||||
// 添加插件
|
||||
const webLinksAddon = new WebLinksAddon();
|
||||
const searchAddon = new SearchAddon();
|
||||
|
||||
xterm.loadAddon(webLinksAddon);
|
||||
xterm.loadAddon(searchAddon);
|
||||
|
||||
// 打开终端
|
||||
if (terminalRef.current) {
|
||||
xterm.open(terminalRef.current);
|
||||
} else {
|
||||
console.error('[Terminal] Terminal container ref is null');
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存引用
|
||||
xtermRef.current = xterm;
|
||||
|
||||
// 延迟一下确保渲染完成,然后获取实际字符尺寸并调整
|
||||
setTimeout(() => {
|
||||
if (xtermRef.current && terminalRef.current) {
|
||||
// 尝试获取实际的字符尺寸
|
||||
try {
|
||||
const core = (xtermRef.current as any)._core;
|
||||
if (core && core._renderService && core._renderService.dimensions) {
|
||||
const dims = core._renderService.dimensions;
|
||||
const actualCharWidth = dims.actualCellWidth || dims.scaledCellWidth;
|
||||
const actualLineHeight = dims.actualCellHeight || dims.scaledCellHeight;
|
||||
|
||||
if (actualCharWidth && actualLineHeight) {
|
||||
console.log('[Terminal] Actual character dimensions:', {
|
||||
charWidth: actualCharWidth,
|
||||
lineHeight: actualLineHeight
|
||||
});
|
||||
|
||||
// 使用实际尺寸重新计算
|
||||
const rect = terminalRef.current.getBoundingClientRect();
|
||||
const availableWidth = rect.width - 2;
|
||||
const newCols = Math.floor(availableWidth / actualCharWidth);
|
||||
|
||||
console.log('[Terminal] Recalculating with actual dimensions:', {
|
||||
availableWidth,
|
||||
actualCharWidth,
|
||||
newCols,
|
||||
currentCols: xtermRef.current.cols
|
||||
});
|
||||
|
||||
if (newCols > xtermRef.current.cols) {
|
||||
xtermRef.current.resize(newCols, xtermRef.current.rows);
|
||||
setTerminalSize({ cols: newCols, rows: xtermRef.current.rows });
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[Terminal] Could not get actual char dimensions:', e);
|
||||
}
|
||||
}
|
||||
resizeTerminal();
|
||||
}, 150);
|
||||
|
||||
// 创建终端会话
|
||||
const newSessionId = await api.createTerminalSession(projectPath || process.cwd());
|
||||
|
||||
if (!isMounted) {
|
||||
await api.closeTerminalSession(newSessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
setSessionId(newSessionId);
|
||||
setIsConnected(true);
|
||||
|
||||
// 监听终端输出
|
||||
const unlisten = await api.listenToTerminalOutput(newSessionId, (data: string) => {
|
||||
if (xtermRef.current && isMounted) {
|
||||
xtermRef.current.write(data);
|
||||
}
|
||||
});
|
||||
|
||||
unlistenRef.current = unlisten;
|
||||
|
||||
// 监听数据输入
|
||||
xterm.onData((data) => {
|
||||
if (newSessionId && isMounted) {
|
||||
api.sendTerminalInput(newSessionId, data).catch((error) => {
|
||||
console.error('[Terminal] Failed to send input:', error);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[Terminal] Initialized with session:', newSessionId);
|
||||
|
||||
} catch (error) {
|
||||
console.error('[Terminal] Failed to initialize:', error);
|
||||
if (xtermRef.current && isMounted) {
|
||||
xtermRef.current.write('\r\n\x1b[31mFailed to start terminal session\x1b[0m\r\n');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
initializeTerminal();
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
|
||||
if (resizeTimeoutRef.current) {
|
||||
clearTimeout(resizeTimeoutRef.current);
|
||||
}
|
||||
|
||||
if (unlistenRef.current) {
|
||||
unlistenRef.current();
|
||||
unlistenRef.current = null;
|
||||
}
|
||||
|
||||
if (sessionId) {
|
||||
api.closeTerminalSession(sessionId).catch(console.error);
|
||||
}
|
||||
|
||||
if (xtermRef.current) {
|
||||
xtermRef.current.dispose();
|
||||
xtermRef.current = null;
|
||||
}
|
||||
|
||||
isInitializedRef.current = false;
|
||||
|
||||
setTimeout(() => {
|
||||
api.cleanupTerminalSessions().catch(console.error);
|
||||
}, 1000);
|
||||
};
|
||||
}, []); // 只运行一次
|
||||
|
||||
// 监听容器大小变化
|
||||
useEffect(() => {
|
||||
if (!terminalRef.current) return;
|
||||
|
||||
// 初始化时立即获取容器宽度
|
||||
const rect = terminalRef.current.getBoundingClientRect();
|
||||
setContainerWidth(rect.width);
|
||||
|
||||
const resizeObserver = new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const { width } = entry.contentRect;
|
||||
setContainerWidth(width);
|
||||
}
|
||||
handleResize();
|
||||
});
|
||||
|
||||
resizeObserver.observe(terminalRef.current);
|
||||
|
||||
// 监听窗口大小变化
|
||||
window.addEventListener('resize', handleResize);
|
||||
|
||||
return () => {
|
||||
resizeObserver.disconnect();
|
||||
window.removeEventListener('resize', handleResize);
|
||||
};
|
||||
}, [handleResize]);
|
||||
|
||||
// 最大化状态改变时调整大小
|
||||
useEffect(() => {
|
||||
handleResize();
|
||||
}, [isMaximized, handleResize]);
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col h-full w-full bg-[#1e1e1e]', className)}>
|
||||
{/* 终端头部 */}
|
||||
<div className="flex items-center justify-between px-3 py-2 bg-gray-900 border-b border-gray-700">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className={cn(
|
||||
'w-2 h-2 rounded-full',
|
||||
isConnected ? 'bg-green-500' : 'bg-red-500'
|
||||
)} />
|
||||
<span className="text-sm text-gray-300">
|
||||
Terminal {sessionId ? `(${sessionId.slice(0, 8)})` : ''}
|
||||
</span>
|
||||
</div>
|
||||
{projectPath && (
|
||||
<span className="text-xs text-gray-500">
|
||||
{projectPath}
|
||||
</span>
|
||||
)}
|
||||
<span className="text-xs text-gray-400">
|
||||
{terminalSize.cols}×{terminalSize.rows}
|
||||
</span>
|
||||
<span className="text-xs text-yellow-400">
|
||||
容器宽度: {containerWidth.toFixed(0)}px
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1">
|
||||
{onToggleMaximize && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={onToggleMaximize}
|
||||
className="h-6 w-6 text-gray-400 hover:text-white"
|
||||
>
|
||||
{isMaximized ? (
|
||||
<Minimize2 className="h-3 w-3" />
|
||||
) : (
|
||||
<Maximize2 className="h-3 w-3" />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{onClose && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={onClose}
|
||||
className="h-6 w-6 text-gray-400 hover:text-white hover:bg-red-600"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 终端主体 */}
|
||||
<div className="flex-1 relative overflow-hidden">
|
||||
<div
|
||||
ref={terminalRef}
|
||||
className="absolute inset-0 p-1"
|
||||
style={{
|
||||
backgroundColor: '#1e1e1e',
|
||||
}}
|
||||
/>
|
||||
|
||||
{!isConnected && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/50">
|
||||
<div className="text-center">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-white mx-auto mb-2" />
|
||||
<p className="text-gray-300 text-sm">正在连接终端...</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Terminal;
|
@@ -104,10 +104,10 @@ export const FlexLayoutContainer: React.FC<FlexLayoutContainerProps> = ({
|
||||
key={panel.id}
|
||||
className={cn(
|
||||
'relative h-full',
|
||||
isMain ? 'flex-1 min-w-0' : 'overflow-hidden',
|
||||
isMain ? 'flex-1 min-w-0 w-full overflow-hidden' : 'overflow-hidden',
|
||||
panel.className
|
||||
)}
|
||||
style={!isMain ? { width, flexShrink: 0 } : undefined}
|
||||
style={!isMain ? { width, flexShrink: 0 } : { width: '100%' }}
|
||||
>
|
||||
{panel.content}
|
||||
|
||||
|
@@ -14,7 +14,7 @@ export const MainContentArea: React.FC<MainContentAreaProps> = ({
|
||||
}) => {
|
||||
return (
|
||||
<div className={cn(
|
||||
'h-full w-full flex flex-col',
|
||||
'h-full w-full flex flex-col overflow-hidden',
|
||||
'bg-background',
|
||||
isEditing && 'relative',
|
||||
className
|
||||
|
@@ -9,9 +9,12 @@ interface LayoutState {
|
||||
showTimeline: boolean;
|
||||
splitPosition: number;
|
||||
isCompactMode: boolean;
|
||||
activeView: 'chat' | 'editor' | 'preview'; // 新增:当前活动视图
|
||||
activeView: 'chat' | 'editor' | 'preview' | 'terminal'; // 新增终端视图
|
||||
editingFile: string | null; // 新增:正在编辑的文件
|
||||
previewUrl: string | null; // 新增:预览URL
|
||||
isTerminalMaximized: boolean; // 新增:终端是否最大化
|
||||
isTerminalOpen: boolean; // 新增:终端是否打开(保持会话)
|
||||
previousView: 'chat' | 'editor' | 'preview' | null; // 新增:记录终端打开前的视图
|
||||
}
|
||||
|
||||
interface LayoutBreakpoints {
|
||||
@@ -35,6 +38,9 @@ const DEFAULT_LAYOUT: LayoutState = {
|
||||
activeView: 'chat', // 默认显示聊天视图
|
||||
editingFile: null,
|
||||
previewUrl: null,
|
||||
isTerminalMaximized: false, // 默认终端不最大化
|
||||
isTerminalOpen: false, // 默认终端关闭
|
||||
previousView: null, // 默认无前一个视图
|
||||
};
|
||||
|
||||
const STORAGE_KEY = 'claudia_layout_preferences';
|
||||
@@ -298,6 +304,40 @@ export function useLayoutManager(projectPath?: string) {
|
||||
previewUrl: null,
|
||||
});
|
||||
}, [saveLayout]);
|
||||
|
||||
// 打开/切换终端
|
||||
const openTerminal = useCallback(() => {
|
||||
if (layout.activeView === 'terminal') {
|
||||
// 如果终端已经显示,收起它(恢复之前的视图)
|
||||
saveLayout({
|
||||
activeView: layout.previousView || 'chat',
|
||||
previousView: null,
|
||||
});
|
||||
} else {
|
||||
// 显示终端,记住当前视图
|
||||
const prev = layout.activeView as 'chat' | 'editor' | 'preview';
|
||||
saveLayout({
|
||||
activeView: 'terminal',
|
||||
isTerminalOpen: true,
|
||||
previousView: prev,
|
||||
});
|
||||
}
|
||||
}, [layout.activeView, layout.previousView, saveLayout]);
|
||||
|
||||
// 关闭终端(恢复之前的视图)
|
||||
const closeTerminal = useCallback(() => {
|
||||
saveLayout({
|
||||
activeView: layout.previousView || 'chat',
|
||||
previousView: null,
|
||||
});
|
||||
}, [layout.previousView, saveLayout]);
|
||||
|
||||
// 切换终端最大化状态
|
||||
const toggleTerminalMaximize = useCallback(() => {
|
||||
saveLayout({
|
||||
isTerminalMaximized: !layout.isTerminalMaximized,
|
||||
});
|
||||
}, [layout.isTerminalMaximized, saveLayout]);
|
||||
|
||||
return {
|
||||
layout,
|
||||
@@ -319,5 +359,9 @@ export function useLayoutManager(projectPath?: string) {
|
||||
openPreview,
|
||||
closePreview,
|
||||
switchToChatView,
|
||||
// 终端相关方法
|
||||
openTerminal,
|
||||
closeTerminal,
|
||||
toggleTerminalMaximize,
|
||||
};
|
||||
}
|
106
src/lib/api.ts
106
src/lib/api.ts
@@ -2512,5 +2512,111 @@ export const api = {
|
||||
console.error("Failed to unwatch Claude project directory:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// ============= Terminal API =============
|
||||
|
||||
/**
|
||||
* Creates a new terminal session using Zellij
|
||||
* @param workingDirectory - The working directory for the terminal session
|
||||
* @returns Promise resolving to the session ID
|
||||
*/
|
||||
async createTerminalSession(workingDirectory: string): Promise<string> {
|
||||
try {
|
||||
return await invoke<string>("create_terminal_session", { workingDirectory });
|
||||
} catch (error) {
|
||||
console.error("Failed to create terminal session:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sends input to a terminal session
|
||||
* @param sessionId - The terminal session ID
|
||||
* @param input - The input data to send
|
||||
* @returns Promise resolving when input is sent
|
||||
*/
|
||||
async sendTerminalInput(sessionId: string, input: string): Promise<void> {
|
||||
try {
|
||||
return await invoke<void>("send_terminal_input", { sessionId, input });
|
||||
} catch (error) {
|
||||
console.error("Failed to send terminal input:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Listen to terminal output for a session
|
||||
* @param sessionId - The terminal session ID
|
||||
* @param callback - Callback function to handle output
|
||||
* @returns Promise resolving to unlisten function
|
||||
*/
|
||||
async listenToTerminalOutput(sessionId: string, callback: (data: string) => void): Promise<() => void> {
|
||||
try {
|
||||
const { listen } = await import("@tauri-apps/api/event");
|
||||
const unlisten = await listen<string>(`terminal-output:${sessionId}`, (event) => {
|
||||
callback(event.payload);
|
||||
});
|
||||
return unlisten;
|
||||
} catch (error) {
|
||||
console.error("Failed to listen to terminal output:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Closes a terminal session
|
||||
* @param sessionId - The terminal session ID to close
|
||||
* @returns Promise resolving when session is closed
|
||||
*/
|
||||
async closeTerminalSession(sessionId: string): Promise<void> {
|
||||
try {
|
||||
return await invoke<void>("close_terminal_session", { sessionId });
|
||||
} catch (error) {
|
||||
console.error("Failed to close terminal session:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Lists all active terminal sessions
|
||||
* @returns Promise resolving to array of active terminal session IDs
|
||||
*/
|
||||
async listTerminalSessions(): Promise<string[]> {
|
||||
try {
|
||||
return await invoke<string[]>("list_terminal_sessions");
|
||||
} catch (error) {
|
||||
console.error("Failed to list terminal sessions:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Resizes a terminal session
|
||||
* @param sessionId - The terminal session ID
|
||||
* @param cols - Number of columns
|
||||
* @param rows - Number of rows
|
||||
* @returns Promise resolving when resize is complete
|
||||
*/
|
||||
async resizeTerminal(sessionId: string, cols: number, rows: number): Promise<void> {
|
||||
try {
|
||||
return await invoke<void>("resize_terminal", { sessionId, cols, rows });
|
||||
} catch (error) {
|
||||
console.error("Failed to resize terminal:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Cleanup orphaned terminal sessions
|
||||
* @returns Promise resolving to the number of sessions cleaned up
|
||||
*/
|
||||
async cleanupTerminalSessions(): Promise<number> {
|
||||
try {
|
||||
return await invoke<number>("cleanup_terminal_sessions");
|
||||
} catch (error) {
|
||||
console.error("Failed to cleanup terminal sessions:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -1,6 +1,63 @@
|
||||
@import "tailwindcss";
|
||||
@import "./styles/grid-layout.css";
|
||||
|
||||
/* xterm.js 全宽度样式 */
|
||||
.xterm-full-width {
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
position: relative !important;
|
||||
}
|
||||
|
||||
.xterm-full-width .xterm {
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
padding: 0 !important;
|
||||
margin: 0 !important;
|
||||
}
|
||||
|
||||
.xterm-full-width .xterm-viewport {
|
||||
width: 100% !important;
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
.xterm-full-width .xterm-screen {
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
}
|
||||
|
||||
.xterm-full-width .xterm-helper-textarea {
|
||||
width: 100% !important;
|
||||
}
|
||||
|
||||
.xterm-full-width canvas {
|
||||
width: 100% !important;
|
||||
display: block !important;
|
||||
}
|
||||
|
||||
/* xterm.js 修复右侧边框 - 确保所有子元素完全填充 */
|
||||
.xterm-full-width .xterm-rows {
|
||||
width: 100% !important;
|
||||
}
|
||||
|
||||
/* 确保 xterm 容器内部元素正确对齐 */
|
||||
.xterm-full-width .xterm-scroll-area {
|
||||
width: 100% !important;
|
||||
}
|
||||
|
||||
/* 强制覆盖 xterm 内联样式 */
|
||||
.xterm-full-width > div {
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
}
|
||||
|
||||
/* 移除任何可能的内边距或边距 */
|
||||
.xterm-full-width .terminal {
|
||||
padding: 0 !important;
|
||||
margin: 0 !important;
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
}
|
||||
|
||||
/* Custom scrollbar hiding */
|
||||
.scrollbar-hide {
|
||||
scrollbar-width: none;
|
||||
|
Reference in New Issue
Block a user