增加终端
This commit is contained in:
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;
|
||||
|
258
src-tauri/src/commands/terminal.rs
Normal file
258
src-tauri/src/commands/terminal.rs
Normal file
@@ -0,0 +1,258 @@
|
||||
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: 24,
|
||||
cols: 80,
|
||||
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");
|
||||
|
||||
// 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");
|
||||
|
Reference in New Issue
Block a user