feat: ZeroSend v2.5.0 - Live Folder Sync, WebDAV Drive (Z:), Watch Party, Full Vim Editor & Remote Grep
This commit is contained in:
Generated
+2
-1
@@ -6372,7 +6372,7 @@ checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e"
|
||||
|
||||
[[package]]
|
||||
name = "zerosend"
|
||||
version = "2.4.1"
|
||||
version = "2.5.0"
|
||||
dependencies = [
|
||||
"arboard",
|
||||
"axum",
|
||||
@@ -6402,6 +6402,7 @@ dependencies = [
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"tray-icon",
|
||||
"url",
|
||||
"uuid",
|
||||
"walkdir",
|
||||
"windows-sys 0.59.0",
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "zerosend"
|
||||
version = "2.4.1"
|
||||
version = "2.5.0"
|
||||
edition = "2021"
|
||||
description = "Fast, peer-to-peer file transfer designed for ZeroTier, Tailscale, Radmin VPN and LAN"
|
||||
authors = ["RarDog"]
|
||||
@@ -14,6 +14,7 @@ axum = { version = "0.8", features = ["multipart"] }
|
||||
tower = "0.5"
|
||||
tower-http = { version = "0.6", features = ["cors", "trace", "fs"] }
|
||||
reqwest = { version = "0.12", features = ["stream", "json", "multipart"] }
|
||||
url = "2.5"
|
||||
socket2 = { version = "0.5", features = ["all"] }
|
||||
network-interface = "2.0"
|
||||
ipnetwork = "0.20"
|
||||
|
||||
@@ -59,6 +59,14 @@ pub struct AppConfig {
|
||||
pub check_updates_on_startup: bool,
|
||||
#[serde(default)]
|
||||
pub gitea_api_url: Option<String>,
|
||||
#[serde(default)]
|
||||
pub folder_sync_pairs: Vec<crate::transfer::FolderSyncPairConfig>,
|
||||
#[serde(default = "default_webdav_port")]
|
||||
pub webdav_port: u16,
|
||||
}
|
||||
|
||||
fn default_webdav_port() -> u16 {
|
||||
53319
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
@@ -92,6 +100,8 @@ impl Default for AppConfig {
|
||||
shared_folders: Vec::new(),
|
||||
check_updates_on_startup: true,
|
||||
gitea_api_url: None,
|
||||
folder_sync_pairs: Vec::new(),
|
||||
webdav_port: 53319,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-1
@@ -272,6 +272,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
);
|
||||
server.start().await?;
|
||||
|
||||
// Start embedded local WebDAV server for Windows Network Drive mounting
|
||||
let shared_folders_ref = server.state().shared_folders.clone();
|
||||
let webdav_port = config.webdav_port;
|
||||
tokio::spawn(async move {
|
||||
crate::transfer::start_webdav_server(shared_folders_ref, webdav_port).await;
|
||||
});
|
||||
|
||||
if cli.tui {
|
||||
ui::tui::run_tui(discovery, server).await?;
|
||||
} else {
|
||||
@@ -291,7 +298,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
.with_inner_size([config.window_width, config.window_height])
|
||||
.with_min_inner_size([780.0, 500.0])
|
||||
.with_visible(!start_minimized)
|
||||
.with_title("ZeroSend v2.4.1 - P2P Sharing for ZeroTier & VPN"),
|
||||
.with_title("ZeroSend v2.5.0 - P2P Sharing for ZeroTier & VPN"),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
||||
+125
-2
@@ -1,7 +1,8 @@
|
||||
use super::archive::compress_folder_to_zip;
|
||||
use super::protocol::{
|
||||
FileMetadata, PrepareTransferRequest, PrepareTransferResponse, SharedFolderInfo,
|
||||
SharedFolderTreeResponse, SharedUploadResponse, TextMessage, TransferProgress, TransferState,
|
||||
FileMetadata, GrepResponse, PrepareTransferRequest, PrepareTransferResponse,
|
||||
SharedFileSaveRequest, SharedFileSaveResponse, SharedFolderInfo, SharedFolderTreeResponse,
|
||||
SharedUploadResponse, TextMessage, TransferProgress, TransferState, WatchPartySyncEvent,
|
||||
};
|
||||
use super::rate_limiter::RateLimiter;
|
||||
use super::text_analyzer::{analyze_text, AnalyzedText};
|
||||
@@ -763,4 +764,126 @@ impl TransferClient {
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse upload response: {}", e))
|
||||
}
|
||||
|
||||
/// Save updated text/code back to a peer's writable shared folder
|
||||
pub async fn save_shared_file(
|
||||
&self,
|
||||
peer_url: &str,
|
||||
folder_id: &str,
|
||||
relative_path: &str,
|
||||
content: &str,
|
||||
pin: Option<&str>,
|
||||
) -> Result<SharedFileSaveResponse, String> {
|
||||
let clean = relative_path.trim_start_matches('/').trim_start_matches('\\');
|
||||
let url = format!("{}/api/shared/save/{}/{}", peer_url, folder_id, clean);
|
||||
|
||||
let req_body = SharedFileSaveRequest {
|
||||
content: content.to_string(),
|
||||
pin: pin.map(|p| p.to_string()),
|
||||
};
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.post(&url)
|
||||
.json(&req_body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Save error: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Save failed: HTTP {}", resp.status()));
|
||||
}
|
||||
|
||||
resp.json::<SharedFileSaveResponse>()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse save response: {}", e))
|
||||
}
|
||||
|
||||
/// Full-text search across remote shared folder
|
||||
pub async fn grep_shared_folder(
|
||||
&self,
|
||||
peer_url: &str,
|
||||
folder_id: &str,
|
||||
query: &str,
|
||||
pin: Option<&str>,
|
||||
subpath: Option<&str>,
|
||||
case_sensitive: bool,
|
||||
) -> Result<GrepResponse, String> {
|
||||
let mut url = format!("{}/api/shared/grep/{}?query={}", peer_url, folder_id, urlencoding::encode(query));
|
||||
if let Some(p) = pin.filter(|p| !p.trim().is_empty()) {
|
||||
url = format!("{}&pin={}", url, urlencoding::encode(p));
|
||||
}
|
||||
if let Some(sub) = subpath.filter(|s| !s.trim().is_empty()) {
|
||||
url = format!("{}&subpath={}", url, urlencoding::encode(sub));
|
||||
}
|
||||
if case_sensitive {
|
||||
url = format!("{}&case_sensitive=true", url);
|
||||
}
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Search error: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Search failed: HTTP {}", resp.status()));
|
||||
}
|
||||
|
||||
resp.json::<GrepResponse>()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse search response: {}", e))
|
||||
}
|
||||
|
||||
/// Fetch active watch-party state from host peer
|
||||
pub async fn get_watch_party(
|
||||
&self,
|
||||
peer_url: &str,
|
||||
) -> Result<Option<WatchPartySyncEvent>, String> {
|
||||
let url = format!("{}/api/shared/watch-party", peer_url);
|
||||
let resp = self
|
||||
.client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Watch party sync error: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("HTTP {}", resp.status()));
|
||||
}
|
||||
|
||||
resp.json::<Option<WatchPartySyncEvent>>()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse watch party event: {}", e))
|
||||
}
|
||||
|
||||
/// Send watch-party sync event to host peer
|
||||
pub async fn send_watch_party_event(
|
||||
&self,
|
||||
peer_url: &str,
|
||||
event: &WatchPartySyncEvent,
|
||||
) -> Result<(), String> {
|
||||
let url = format!("{}/api/shared/watch-party", peer_url);
|
||||
let resp = self
|
||||
.client
|
||||
.post(&url)
|
||||
.json(event)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to broadcast watch party event: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Broadcast failed: HTTP {}", resp.status()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod urlencoding {
|
||||
pub fn encode(s: &str) -> String {
|
||||
url::form_urlencoded::byte_serialize(s.as_bytes()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
use crate::transfer::client::TransferClient;
|
||||
use crate::transfer::protocol::FolderSyncPairConfig;
|
||||
use chrono::Local;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::{mpsc, RwLock};
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SyncStatusUpdate {
|
||||
pub pair_id: String,
|
||||
pub message: String,
|
||||
pub is_error: bool,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
pub struct FolderSyncEngine {
|
||||
client: Arc<TransferClient>,
|
||||
pairs: Arc<RwLock<Vec<FolderSyncPairConfig>>>,
|
||||
status_tx: mpsc::UnboundedSender<SyncStatusUpdate>,
|
||||
}
|
||||
|
||||
impl FolderSyncEngine {
|
||||
pub fn new(
|
||||
client: Arc<TransferClient>,
|
||||
pairs: Vec<FolderSyncPairConfig>,
|
||||
status_tx: mpsc::UnboundedSender<SyncStatusUpdate>,
|
||||
) -> Self {
|
||||
Self {
|
||||
client,
|
||||
pairs: Arc::new(RwLock::new(pairs)),
|
||||
status_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pairs_handle(&self) -> Arc<RwLock<Vec<FolderSyncPairConfig>>> {
|
||||
self.pairs.clone()
|
||||
}
|
||||
|
||||
pub async fn start_background_sync(
|
||||
self: Arc<Self>,
|
||||
peers_map: Arc<RwLock<HashMap<String, crate::network::PeerInfo>>>,
|
||||
) {
|
||||
info!("Folder auto-sync engine started.");
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(8)).await;
|
||||
|
||||
let pairs = self.pairs.read().await.clone();
|
||||
for pair in pairs {
|
||||
if !pair.is_active {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if peer is currently online
|
||||
let peer_opt = {
|
||||
let peers = peers_map.read().await;
|
||||
peers.get(&pair.remote_peer_id).cloned()
|
||||
};
|
||||
|
||||
let peer = match peer_opt {
|
||||
Some(p) if p.is_online => p,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let peer_url = peer.transfer_url();
|
||||
let client = self.client.clone();
|
||||
let status_tx = self.status_tx.clone();
|
||||
let pair_id = pair.id.clone();
|
||||
let pair_name = pair.name.clone();
|
||||
let local_path = pair.local_path.clone();
|
||||
let remote_folder_id = pair.remote_folder_id.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = Self::sync_pair(
|
||||
&client,
|
||||
&peer_url,
|
||||
&local_path,
|
||||
&remote_folder_id,
|
||||
&pair_id,
|
||||
&pair_name,
|
||||
&status_tx,
|
||||
)
|
||||
.await
|
||||
{
|
||||
let _ = status_tx.send(SyncStatusUpdate {
|
||||
pair_id,
|
||||
message: format!("Sync error: {}", e),
|
||||
is_error: true,
|
||||
timestamp: Local::now().format("%H:%M:%S").to_string(),
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_pair(
|
||||
client: &TransferClient,
|
||||
peer_url: &str,
|
||||
local_path: &Path,
|
||||
remote_folder_id: &str,
|
||||
pair_id: &str,
|
||||
pair_name: &str,
|
||||
status_tx: &mpsc::UnboundedSender<SyncStatusUpdate>,
|
||||
) -> Result<(), String> {
|
||||
if !local_path.exists() {
|
||||
let _ = tokio::fs::create_dir_all(local_path).await;
|
||||
}
|
||||
|
||||
// Fetch remote directory tree
|
||||
let remote_tree = client
|
||||
.fetch_shared_tree(peer_url, remote_folder_id, None, None)
|
||||
.await?;
|
||||
|
||||
// 1. Scan local files
|
||||
let mut local_files: HashMap<String, (u64, u64)> = HashMap::new(); // rel_path -> (size, mtime)
|
||||
if let Ok(entries) = walkdir_local(local_path) {
|
||||
for (rel, size, mtime) in entries {
|
||||
local_files.insert(rel, (size, mtime));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Map remote files
|
||||
let mut remote_files: HashMap<String, u64> = HashMap::new();
|
||||
for entry in &remote_tree.entries {
|
||||
if !entry.is_dir {
|
||||
remote_files.insert(entry.relative_path.clone(), entry.size);
|
||||
}
|
||||
}
|
||||
|
||||
let mut synced_count = 0;
|
||||
|
||||
// A. Upload newly created or changed local files to remote
|
||||
if !remote_tree.read_only {
|
||||
for (rel, (size, _)) in &local_files {
|
||||
let need_upload = match remote_files.get(rel) {
|
||||
Some(remote_size) => *remote_size != *size,
|
||||
None => true, // New local file
|
||||
};
|
||||
|
||||
if need_upload {
|
||||
let file_full_path = local_path.join(rel);
|
||||
let subpath = Path::new(rel).parent().and_then(|p| p.to_str()).unwrap_or("");
|
||||
if let Ok(_) = client
|
||||
.upload_to_shared_folder(
|
||||
peer_url,
|
||||
remote_folder_id,
|
||||
subpath,
|
||||
&file_full_path,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
synced_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// B. Download newly created remote files to local
|
||||
for (rel, remote_size) in &remote_files {
|
||||
let need_download = match local_files.get(rel) {
|
||||
Some((local_size, _)) => *local_size != *remote_size,
|
||||
None => true, // New remote file
|
||||
};
|
||||
|
||||
if need_download {
|
||||
let target_file_path = local_path.join(rel);
|
||||
if let Some(parent) = target_file_path.parent() {
|
||||
let _ = tokio::fs::create_dir_all(parent).await;
|
||||
}
|
||||
|
||||
// Download file
|
||||
if let Ok(_) = client
|
||||
.download_shared_file(peer_url, remote_folder_id, rel, &target_file_path, None)
|
||||
.await
|
||||
{
|
||||
synced_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if synced_count > 0 {
|
||||
let _ = status_tx.send(SyncStatusUpdate {
|
||||
pair_id: pair_id.to_string(),
|
||||
message: format!("Synced {} item(s) in '{}'", synced_count, pair_name),
|
||||
is_error: false,
|
||||
timestamp: Local::now().format("%H:%M:%S").to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn walkdir_local(base: &Path) -> Result<Vec<(String, u64, u64)>, std::io::Error> {
|
||||
let mut results = Vec::new();
|
||||
for entry in walkdir::WalkDir::new(base).into_iter().filter_map(|e| e.ok()) {
|
||||
if entry.file_type().is_file() {
|
||||
if let Ok(rel) = entry.path().strip_prefix(base) {
|
||||
let rel_str = rel.to_string_lossy().replace('\\', "/");
|
||||
let meta = entry.metadata().ok();
|
||||
let size = meta.as_ref().map(|m| m.len()).unwrap_or(0);
|
||||
let mtime = meta
|
||||
.as_ref()
|
||||
.and_then(|m| m.modified().ok())
|
||||
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
results.push((rel_str, size, mtime));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(results)
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
pub mod archive;
|
||||
pub mod client;
|
||||
pub mod folder_sync;
|
||||
pub mod protocol;
|
||||
pub mod rate_limiter;
|
||||
pub mod server;
|
||||
pub mod text_analyzer;
|
||||
pub mod webdav;
|
||||
|
||||
pub use client::*;
|
||||
pub use folder_sync::*;
|
||||
pub use protocol::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use rate_limiter::RateLimiter;
|
||||
pub use server::*;
|
||||
pub use text_analyzer::*;
|
||||
pub use webdav::*;
|
||||
|
||||
@@ -119,6 +119,62 @@ pub struct SharedUploadResponse {
|
||||
pub bytes_written: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GrepMatch {
|
||||
pub file_path: String,
|
||||
pub line_number: usize,
|
||||
pub line_content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GrepResponse {
|
||||
pub matches: Vec<GrepMatch>,
|
||||
pub total_files_searched: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SharedFileSaveRequest {
|
||||
pub content: String,
|
||||
#[serde(default)]
|
||||
pub pin: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SharedFileSaveResponse {
|
||||
pub status: String,
|
||||
pub bytes_written: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct WatchPartySyncEvent {
|
||||
pub room_id: String,
|
||||
pub host_peer_id: String,
|
||||
pub host_peer_name: String,
|
||||
pub folder_id: String,
|
||||
pub relative_path: String,
|
||||
pub is_playing: bool,
|
||||
pub position_secs: f64,
|
||||
pub timestamp_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FolderSyncPairConfig {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub local_path: std::path::PathBuf,
|
||||
pub remote_peer_id: String,
|
||||
pub remote_peer_name: String,
|
||||
pub remote_folder_id: String,
|
||||
#[serde(default = "default_true_val")]
|
||||
pub is_active: bool,
|
||||
#[serde(default)]
|
||||
pub last_sync: Option<String>,
|
||||
}
|
||||
|
||||
fn default_true_val() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn get_mime_type(filename: &str) -> &'static str {
|
||||
let lower = filename.to_lowercase();
|
||||
if lower.ends_with(".mp4") {
|
||||
|
||||
+223
-3
@@ -1,8 +1,9 @@
|
||||
use super::archive::extract_zip_archive;
|
||||
use super::protocol::{
|
||||
FileMetadata, PrepareTransferRequest, PrepareTransferResponse,
|
||||
SharedFileEntry, SharedFolderInfo, SharedFolderTreeResponse, SharedUploadResponse,
|
||||
TextMessage, TransferProgress, TransferState,
|
||||
FileMetadata, GrepMatch, GrepResponse, PrepareTransferRequest, PrepareTransferResponse,
|
||||
SharedFileEntry, SharedFileSaveRequest, SharedFileSaveResponse, SharedFolderInfo,
|
||||
SharedFolderTreeResponse, SharedUploadResponse, TextMessage, TransferProgress, TransferState,
|
||||
WatchPartySyncEvent,
|
||||
};
|
||||
use crate::config::SharedFolderConfig;
|
||||
use crate::network::DiscoveryBeacon;
|
||||
@@ -36,6 +37,7 @@ pub struct ServerState {
|
||||
pub pin_code: Arc<RwLock<String>>,
|
||||
pub auto_extract_zip: Arc<RwLock<bool>>,
|
||||
pub shared_folders: Arc<RwLock<Vec<SharedFolderConfig>>>,
|
||||
pub watch_party_state: Arc<RwLock<Option<WatchPartySyncEvent>>>,
|
||||
pub pending_requests: Arc<RwLock<HashMap<String, PrepareTransferRequest>>>,
|
||||
pub accepted_requests: Arc<RwLock<HashMap<String, PrepareTransferRequest>>>,
|
||||
pub pending_confirmations: Arc<Mutex<HashMap<String, oneshot::Sender<bool>>>>,
|
||||
@@ -85,6 +87,7 @@ impl TransferServer {
|
||||
pin_code: Arc::new(RwLock::new(pin_code)),
|
||||
auto_extract_zip: Arc::new(RwLock::new(auto_extract_zip)),
|
||||
shared_folders: Arc::new(RwLock::new(shared_folders)),
|
||||
watch_party_state: Arc::new(RwLock::new(None)),
|
||||
pending_requests: Arc::new(RwLock::new(HashMap::new())),
|
||||
accepted_requests: Arc::new(RwLock::new(HashMap::new())),
|
||||
pending_confirmations: Arc::new(Mutex::new(HashMap::new())),
|
||||
@@ -206,6 +209,9 @@ impl TransferServer {
|
||||
"/api/shared/upload/{folder_id}/{*subpath}",
|
||||
post(handle_shared_upload).layer(DefaultBodyLimit::disable()),
|
||||
)
|
||||
.route("/api/shared/save/{folder_id}/{*subpath}", post(handle_shared_save))
|
||||
.route("/api/shared/grep/{folder_id}", get(handle_shared_grep))
|
||||
.route("/api/shared/watch-party", get(handle_watch_party_get).post(handle_watch_party_post))
|
||||
.layer(CorsLayer::permissive())
|
||||
.with_state(self.state.clone());
|
||||
|
||||
@@ -1521,6 +1527,220 @@ async fn handle_shared_upload(
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn handle_shared_save(
|
||||
State(state): State<Arc<ServerState>>,
|
||||
Path((folder_id, subpath)): Path<(String, String)>,
|
||||
Json(payload): Json<SharedFileSaveRequest>,
|
||||
) -> Response {
|
||||
let shares = state.shared_folders.read().await;
|
||||
let folder = match shares.iter().find(|s| s.id == folder_id) {
|
||||
Some(f) => f.clone(),
|
||||
None => {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({ "error": "Shared folder not found" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
drop(shares);
|
||||
|
||||
if folder.read_only {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({ "error": "Shared folder is Read-Only" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
if let Some(expected_pin) = &folder.pin {
|
||||
if payload.pin.as_deref() != Some(expected_pin.as_str()) {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({ "error": "Incorrect or missing PIN code" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
|
||||
let base_canonical = match folder.path.canonicalize() {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({ "error": "Base path not found" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let clean_sub = subpath.trim_start_matches('/').trim_start_matches('\\');
|
||||
let target_file_path = base_canonical.join(clean_sub);
|
||||
|
||||
if !target_file_path.starts_with(&base_canonical) {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({ "error": "Path traversal detected" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
if let Some(parent) = target_file_path.parent() {
|
||||
let _ = tokio::fs::create_dir_all(parent).await;
|
||||
}
|
||||
|
||||
let bytes = payload.content.as_bytes();
|
||||
match tokio::fs::write(&target_file_path, bytes).await {
|
||||
Ok(_) => Json(SharedFileSaveResponse {
|
||||
status: "success".to_string(),
|
||||
bytes_written: bytes.len() as u64,
|
||||
})
|
||||
.into_response(),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({ "error": format!("Failed to write file: {}", e) })),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct GrepQuery {
|
||||
query: String,
|
||||
pin: Option<String>,
|
||||
subpath: Option<String>,
|
||||
case_sensitive: Option<bool>,
|
||||
}
|
||||
|
||||
async fn handle_shared_grep(
|
||||
State(state): State<Arc<ServerState>>,
|
||||
Path(folder_id): Path<String>,
|
||||
Query(params): Query<GrepQuery>,
|
||||
) -> Response {
|
||||
let shares = state.shared_folders.read().await;
|
||||
let folder = match shares.iter().find(|s| s.id == folder_id) {
|
||||
Some(f) => f.clone(),
|
||||
None => {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({ "error": "Shared folder not found" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
drop(shares);
|
||||
|
||||
if let Some(expected_pin) = &folder.pin {
|
||||
if params.pin.as_deref() != Some(expected_pin.as_str()) {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({ "error": "Incorrect or missing PIN code" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
|
||||
let base_canonical = match folder.path.canonicalize() {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({ "error": "Base path not found" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let target_dir = if let Some(sub) = ¶ms.subpath {
|
||||
let clean = sub.trim_start_matches('/').trim_start_matches('\\');
|
||||
base_canonical.join(clean)
|
||||
} else {
|
||||
base_canonical.clone()
|
||||
};
|
||||
|
||||
if !target_dir.starts_with(&base_canonical) {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({ "error": "Path traversal detected" })),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let search_query = params.query.clone();
|
||||
let is_case_sens = params.case_sensitive.unwrap_or(false);
|
||||
let mut matches = Vec::new();
|
||||
let mut files_searched = 0;
|
||||
|
||||
let base_clone = base_canonical.clone();
|
||||
for entry in walkdir::WalkDir::new(&target_dir).into_iter().filter_map(|e| e.ok()) {
|
||||
if entry.file_type().is_file() {
|
||||
let path = entry.path();
|
||||
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
let mime = crate::transfer::get_mime_type(name);
|
||||
let is_text = mime.starts_with("text/")
|
||||
|| mime.starts_with("application/json")
|
||||
|| mime.starts_with("application/xml")
|
||||
|| mime.starts_with("application/javascript")
|
||||
|| mime.ends_with("x-sh")
|
||||
|| name.ends_with(".rs")
|
||||
|| name.ends_with(".py")
|
||||
|| name.ends_with(".toml")
|
||||
|| name.ends_with(".md")
|
||||
|| name.ends_with(".txt")
|
||||
|| name.ends_with(".log");
|
||||
|
||||
if is_text {
|
||||
files_searched += 1;
|
||||
if let Ok(content) = std::fs::read_to_string(path) {
|
||||
if let Ok(rel) = path.strip_prefix(&base_clone) {
|
||||
let rel_str = rel.to_string_lossy().replace('\\', "/");
|
||||
for (idx, line) in content.lines().enumerate() {
|
||||
let is_match = if is_case_sens {
|
||||
line.contains(&search_query)
|
||||
} else {
|
||||
line.to_lowercase().contains(&search_query.to_lowercase())
|
||||
};
|
||||
if is_match {
|
||||
matches.push(GrepMatch {
|
||||
file_path: rel_str.clone(),
|
||||
line_number: idx + 1,
|
||||
line_content: line.trim().to_string(),
|
||||
});
|
||||
if matches.len() >= 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches.len() >= 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Json(GrepResponse {
|
||||
matches,
|
||||
total_files_searched: files_searched,
|
||||
})
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn handle_watch_party_get(State(state): State<Arc<ServerState>>) -> Response {
|
||||
let guard = state.watch_party_state.read().await;
|
||||
Json((*guard).clone()).into_response()
|
||||
}
|
||||
|
||||
async fn handle_watch_party_post(
|
||||
State(state): State<Arc<ServerState>>,
|
||||
Json(event): Json<WatchPartySyncEvent>,
|
||||
) -> Response {
|
||||
let mut guard = state.watch_party_state.write().await;
|
||||
*guard = Some(event);
|
||||
Json(serde_json::json!({ "status": "ok" })).into_response()
|
||||
}
|
||||
|
||||
async fn handle_web_ui(State(state): State<Arc<ServerState>>) -> Html<String> {
|
||||
let html = format!(
|
||||
r###"<!DOCTYPE html>
|
||||
|
||||
@@ -0,0 +1,428 @@
|
||||
use crate::config::SharedFolderConfig;
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Path, State},
|
||||
http::{header, Method, Request, Response, StatusCode},
|
||||
routing::any,
|
||||
Router,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::fs::File;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio_util::io::ReaderStream;
|
||||
use tracing::{info, warn};
|
||||
|
||||
pub struct WebDavState {
|
||||
pub shared_folders: Arc<RwLock<Vec<SharedFolderConfig>>>,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
pub fn create_webdav_router(state: Arc<WebDavState>) -> Router {
|
||||
Router::new()
|
||||
.route("/webdav", any(handle_webdav_root))
|
||||
.route("/webdav/{*path}", any(handle_webdav_path))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
pub async fn start_webdav_server(
|
||||
shared_folders: Arc<RwLock<Vec<SharedFolderConfig>>>,
|
||||
port: u16,
|
||||
) {
|
||||
let state = Arc::new(WebDavState {
|
||||
shared_folders,
|
||||
port,
|
||||
});
|
||||
let app = create_webdav_router(state);
|
||||
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], port));
|
||||
|
||||
info!("Starting local WebDAV server on http://{}", addr);
|
||||
|
||||
match tokio::net::TcpListener::bind(addr).await {
|
||||
Ok(listener) => {
|
||||
let _ = axum::serve(listener, app).await;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to bind WebDAV server on {}: {}", addr, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_webdav_root(
|
||||
State(state): State<Arc<WebDavState>>,
|
||||
req: Request<Body>,
|
||||
) -> Response<Body> {
|
||||
let method = req.method().clone();
|
||||
let headers = req.headers().clone();
|
||||
|
||||
if method == Method::OPTIONS {
|
||||
return options_response();
|
||||
}
|
||||
|
||||
if method.as_str() == "PROPFIND" {
|
||||
let shares = state.shared_folders.read().await;
|
||||
let depth = headers
|
||||
.get("Depth")
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.unwrap_or("1");
|
||||
|
||||
let mut xml = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n");
|
||||
|
||||
// Root response
|
||||
xml.push_str(" <D:response>\n");
|
||||
xml.push_str(" <D:href>/webdav/</D:href>\n");
|
||||
xml.push_str(" <D:propstat>\n");
|
||||
xml.push_str(" <D:prop>\n");
|
||||
xml.push_str(" <D:displayname>ZeroSend WebDAV</D:displayname>\n");
|
||||
xml.push_str(" <D:resourcetype><D:collection/></D:resourcetype>\n");
|
||||
xml.push_str(" </D:prop>\n");
|
||||
xml.push_str(" <D:status>HTTP/1.1 200 OK</D:status>\n");
|
||||
xml.push_str(" </D:propstat>\n");
|
||||
xml.push_str(" </D:response>\n");
|
||||
|
||||
if depth != "0" {
|
||||
for s in shares.iter() {
|
||||
let encoded_name = urlencoding::encode(&s.name);
|
||||
xml.push_str(" <D:response>\n");
|
||||
xml.push_str(&format!(" <D:href>/webdav/{}/</D:href>\n", encoded_name));
|
||||
xml.push_str(" <D:propstat>\n");
|
||||
xml.push_str(" <D:prop>\n");
|
||||
xml.push_str(&format!(" <D:displayname>{}</D:displayname>\n", escape_xml(&s.name)));
|
||||
xml.push_str(" <D:resourcetype><D:collection/></D:resourcetype>\n");
|
||||
xml.push_str(" </D:prop>\n");
|
||||
xml.push_str(" <D:status>HTTP/1.1 200 OK</D:status>\n");
|
||||
xml.push_str(" </D:propstat>\n");
|
||||
xml.push_str(" </D:response>\n");
|
||||
}
|
||||
}
|
||||
|
||||
xml.push_str("</D:multistatus>");
|
||||
|
||||
return Response::builder()
|
||||
.status(StatusCode::MULTI_STATUS)
|
||||
.header(header::CONTENT_TYPE, "application/xml; charset=utf-8")
|
||||
.header("DAV", "1, 2")
|
||||
.header("MS-Author-Via", "DAV")
|
||||
.body(Body::from(xml))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
options_response()
|
||||
}
|
||||
|
||||
async fn handle_webdav_path(
|
||||
State(state): State<Arc<WebDavState>>,
|
||||
Path(raw_path): Path<String>,
|
||||
req: Request<Body>,
|
||||
) -> Response<Body> {
|
||||
let method = req.method().clone();
|
||||
let headers = req.headers().clone();
|
||||
|
||||
if method == Method::OPTIONS {
|
||||
return options_response();
|
||||
}
|
||||
|
||||
let decoded_path = urlencoding::decode(&raw_path)
|
||||
.unwrap_or(std::borrow::Cow::Borrowed(&raw_path))
|
||||
.to_string();
|
||||
let parts: Vec<&str> = decoded_path.split('/').filter(|s| !s.is_empty()).collect();
|
||||
|
||||
if parts.is_empty() {
|
||||
return handle_webdav_root(State(state), req).await;
|
||||
}
|
||||
|
||||
let share_name = parts[0];
|
||||
let subpath_parts = &parts[1..];
|
||||
|
||||
let shares = state.shared_folders.read().await;
|
||||
let folder = match shares.iter().find(|s| s.name == share_name || s.id == share_name) {
|
||||
Some(f) => f.clone(),
|
||||
None => {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Folder not found"))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
drop(shares);
|
||||
|
||||
let base_canonical = match folder.path.canonicalize() {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Base path not found"))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
let mut target_path = base_canonical.clone();
|
||||
for seg in subpath_parts {
|
||||
if *seg == ".." || *seg == "." {
|
||||
continue;
|
||||
}
|
||||
target_path.push(seg);
|
||||
}
|
||||
|
||||
// Safety check against path traversal
|
||||
if !target_path.starts_with(&base_canonical) {
|
||||
return Response::builder()
|
||||
.status(StatusCode::FORBIDDEN)
|
||||
.body(Body::from("Forbidden path"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
match method.as_str() {
|
||||
"PROPFIND" => {
|
||||
let depth = headers
|
||||
.get("Depth")
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.unwrap_or("1");
|
||||
|
||||
let mut xml = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n");
|
||||
|
||||
if let Ok(meta) = std::fs::metadata(&target_path) {
|
||||
let is_dir = meta.is_dir();
|
||||
let href_path = format!("/webdav/{}{}", raw_path.trim_end_matches('/'), if is_dir { "/" } else { "" });
|
||||
|
||||
xml.push_str(" <D:response>\n");
|
||||
xml.push_str(&format!(" <D:href>{}</D:href>\n", href_path));
|
||||
xml.push_str(" <D:propstat>\n");
|
||||
xml.push_str(" <D:prop>\n");
|
||||
let name = target_path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| share_name.to_string());
|
||||
xml.push_str(&format!(" <D:displayname>{}</D:displayname>\n", escape_xml(&name)));
|
||||
if is_dir {
|
||||
xml.push_str(" <D:resourcetype><D:collection/></D:resourcetype>\n");
|
||||
} else {
|
||||
xml.push_str(" <D:resourcetype/>\n");
|
||||
xml.push_str(&format!(" <D:getcontentlength>{}</D:getcontentlength>\n", meta.len()));
|
||||
let mime = crate::transfer::get_mime_type(&name);
|
||||
xml.push_str(&format!(" <D:getcontenttype>{}</D:getcontenttype>\n", mime));
|
||||
}
|
||||
xml.push_str(" </D:prop>\n");
|
||||
xml.push_str(" <D:status>HTTP/1.1 200 OK</D:status>\n");
|
||||
xml.push_str(" </D:propstat>\n");
|
||||
xml.push_str(" </D:response>\n");
|
||||
|
||||
if is_dir && depth != "0" {
|
||||
if let Ok(entries) = std::fs::read_dir(&target_path) {
|
||||
for entry in entries.flatten() {
|
||||
if let Ok(child_meta) = entry.metadata() {
|
||||
let child_name = entry.file_name().to_string_lossy().into_owned();
|
||||
let child_is_dir = child_meta.is_dir();
|
||||
let child_href = format!("{}/{}{}", href_path.trim_end_matches('/'), urlencoding::encode(&child_name), if child_is_dir { "/" } else { "" });
|
||||
|
||||
xml.push_str(" <D:response>\n");
|
||||
xml.push_str(&format!(" <D:href>{}</D:href>\n", child_href));
|
||||
xml.push_str(" <D:propstat>\n");
|
||||
xml.push_str(" <D:prop>\n");
|
||||
xml.push_str(&format!(" <D:displayname>{}</D:displayname>\n", escape_xml(&child_name)));
|
||||
if child_is_dir {
|
||||
xml.push_str(" <D:resourcetype><D:collection/></D:resourcetype>\n");
|
||||
} else {
|
||||
xml.push_str(" <D:resourcetype/>\n");
|
||||
xml.push_str(&format!(" <D:getcontentlength>{}</D:getcontentlength>\n", child_meta.len()));
|
||||
let mime = crate::transfer::get_mime_type(&child_name);
|
||||
xml.push_str(&format!(" <D:getcontenttype>{}</D:getcontenttype>\n", mime));
|
||||
}
|
||||
xml.push_str(" </D:prop>\n");
|
||||
xml.push_str(" <D:status>HTTP/1.1 200 OK</D:status>\n");
|
||||
xml.push_str(" </D:propstat>\n");
|
||||
xml.push_str(" </D:response>\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Path not found"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
xml.push_str("</D:multistatus>");
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::MULTI_STATUS)
|
||||
.header(header::CONTENT_TYPE, "application/xml; charset=utf-8")
|
||||
.header("DAV", "1, 2")
|
||||
.header("MS-Author-Via", "DAV")
|
||||
.body(Body::from(xml))
|
||||
.unwrap()
|
||||
}
|
||||
"GET" | "HEAD" => {
|
||||
if target_path.is_dir() {
|
||||
return Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(Body::from("<h1>Directory Index</h1>"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
match File::open(&target_path).await {
|
||||
Ok(file) => {
|
||||
let meta = file.metadata().await.ok();
|
||||
let file_size = meta.as_ref().map(|m| m.len()).unwrap_or(0);
|
||||
let filename = target_path.file_name().map(|f| f.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let mime = crate::transfer::get_mime_type(&filename);
|
||||
|
||||
let stream = ReaderStream::new(file);
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, mime)
|
||||
.header(header::CONTENT_LENGTH, file_size.to_string())
|
||||
.header(header::ACCEPT_RANGES, "bytes")
|
||||
.body(Body::from_stream(stream))
|
||||
.unwrap()
|
||||
}
|
||||
Err(_) => Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("File not found"))
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
"PUT" => {
|
||||
if folder.read_only {
|
||||
return Response::builder()
|
||||
.status(StatusCode::FORBIDDEN)
|
||||
.body(Body::from("Folder is read-only"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let bytes = match axum::body::to_bytes(req.into_body(), 100 * 1024 * 1024).await {
|
||||
Ok(b) => b,
|
||||
Err(e) => {
|
||||
return Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body(Body::from(format!("Failed to read body: {}", e)))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(parent) = target_path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
|
||||
if std::fs::write(&target_path, bytes).is_ok() {
|
||||
Response::builder()
|
||||
.status(StatusCode::CREATED)
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
} else {
|
||||
Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::from("Failed to write file"))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
"MKCOL" => {
|
||||
if folder.read_only {
|
||||
return Response::builder()
|
||||
.status(StatusCode::FORBIDDEN)
|
||||
.body(Body::from("Folder is read-only"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if std::fs::create_dir_all(&target_path).is_ok() {
|
||||
Response::builder()
|
||||
.status(StatusCode::CREATED)
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
} else {
|
||||
Response::builder()
|
||||
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||
.body(Body::from("Failed to create folder"))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
"DELETE" => {
|
||||
if folder.read_only {
|
||||
return Response::builder()
|
||||
.status(StatusCode::FORBIDDEN)
|
||||
.body(Body::from("Folder is read-only"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if target_path.is_dir() {
|
||||
let _ = std::fs::remove_dir_all(&target_path);
|
||||
} else {
|
||||
let _ = std::fs::remove_file(&target_path);
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::NO_CONTENT)
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
}
|
||||
_ => options_response(),
|
||||
}
|
||||
}
|
||||
|
||||
fn options_response() -> Response<Body> {
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header("DAV", "1, 2")
|
||||
.header("MS-Author-Via", "DAV")
|
||||
.header("Allow", "OPTIONS, GET, HEAD, POST, DELETE, PROPFIND, PROPPATCH, COPY, MOVE, MKCOL, PUT, LOCK, UNLOCK")
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn escape_xml(s: &str) -> String {
|
||||
s.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
}
|
||||
|
||||
mod urlencoding {
|
||||
pub fn encode(s: &str) -> String {
|
||||
url::form_urlencoded::byte_serialize(s.as_bytes()).collect()
|
||||
}
|
||||
|
||||
pub fn decode(s: &str) -> Result<std::borrow::Cow<'_, str>, ()> {
|
||||
url::form_urlencoded::parse(s.as_bytes())
|
||||
.next()
|
||||
.map(|(k, _)| k)
|
||||
.ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to mount Windows Network Drive via `net use`
|
||||
pub fn mount_webdav_drive(drive_letter: char, port: u16) -> Result<String, String> {
|
||||
let drive = format!("{}:", drive_letter);
|
||||
let url = format!("http://127.0.0.1:{}/webdav", port);
|
||||
|
||||
// Unmount first if already mapped
|
||||
let _ = std::process::Command::new("net")
|
||||
.args(["use", &drive, "/delete", "/y"])
|
||||
.output();
|
||||
|
||||
let output = std::process::Command::new("net")
|
||||
.args(["use", &drive, &url])
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run net use: {}", e))?;
|
||||
|
||||
if output.status.success() {
|
||||
Ok(format!("Successfully mounted {} to {}", drive, url))
|
||||
} else {
|
||||
let err = String::from_utf8_lossy(&output.stderr);
|
||||
Err(format!("Mount failed: {}", err.trim()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to unmount Windows Network Drive
|
||||
pub fn unmount_webdav_drive(drive_letter: char) -> Result<(), String> {
|
||||
let drive = format!("{}:", drive_letter);
|
||||
let output = std::process::Command::new("net")
|
||||
.args(["use", &drive, "/delete", "/y"])
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run net use: {}", e))?;
|
||||
|
||||
if output.status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
let err = String::from_utf8_lossy(&output.stderr);
|
||||
Err(format!("Unmount failed: {}", err.trim()))
|
||||
}
|
||||
}
|
||||
+764
-42
File diff suppressed because it is too large
Load Diff
+325
-114
@@ -9,13 +9,22 @@ pub struct VimViewerModal {
|
||||
pub file_name: String,
|
||||
pub relative_path: String,
|
||||
pub source_peer: String,
|
||||
pub peer_url: Option<String>,
|
||||
pub folder_id: Option<String>,
|
||||
pub pin: Option<String>,
|
||||
pub is_writable: bool,
|
||||
pub lines: Vec<String>,
|
||||
pub buffer_content: String,
|
||||
pub is_insert_mode: bool,
|
||||
pub is_markdown_view: bool,
|
||||
pub cursor_line: usize, // 1-indexed
|
||||
pub search_query: String,
|
||||
pub is_searching: bool,
|
||||
pub command_input: String,
|
||||
pub is_command_mode: bool,
|
||||
pub status_msg: Option<String>,
|
||||
pub pending_save_request: Option<(String, String, String, String, Option<String>)>, // (peer_url, folder_id, rel_path, content, pin)
|
||||
pub is_dirty: bool,
|
||||
last_g_press: Option<Instant>,
|
||||
}
|
||||
|
||||
@@ -26,40 +35,112 @@ impl Default for VimViewerModal {
|
||||
file_name: String::new(),
|
||||
relative_path: String::new(),
|
||||
source_peer: String::new(),
|
||||
peer_url: None,
|
||||
folder_id: None,
|
||||
pin: None,
|
||||
is_writable: false,
|
||||
lines: Vec::new(),
|
||||
buffer_content: String::new(),
|
||||
is_insert_mode: false,
|
||||
is_markdown_view: false,
|
||||
cursor_line: 1,
|
||||
search_query: String::new(),
|
||||
is_searching: false,
|
||||
command_input: String::new(),
|
||||
is_command_mode: false,
|
||||
status_msg: None,
|
||||
pending_save_request: None,
|
||||
is_dirty: false,
|
||||
last_g_press: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VimViewerModal {
|
||||
pub fn open(&mut self, file_name: String, relative_path: String, source_peer: String, content: String) {
|
||||
pub fn open(
|
||||
&mut self,
|
||||
file_name: String,
|
||||
relative_path: String,
|
||||
source_peer: String,
|
||||
content: String,
|
||||
) {
|
||||
self.open_with_meta(file_name, relative_path, source_peer, content, None, None, None, false);
|
||||
}
|
||||
|
||||
pub fn open_with_meta(
|
||||
&mut self,
|
||||
file_name: String,
|
||||
relative_path: String,
|
||||
source_peer: String,
|
||||
content: String,
|
||||
peer_url: Option<String>,
|
||||
folder_id: Option<String>,
|
||||
pin: Option<String>,
|
||||
is_writable: bool,
|
||||
) {
|
||||
self.is_open = true;
|
||||
self.file_name = file_name;
|
||||
self.relative_path = relative_path;
|
||||
self.source_peer = source_peer;
|
||||
self.peer_url = peer_url;
|
||||
self.folder_id = folder_id;
|
||||
self.pin = pin;
|
||||
self.is_writable = is_writable;
|
||||
self.buffer_content = content.clone();
|
||||
self.lines = content.lines().map(|s| s.to_string()).collect();
|
||||
if self.lines.is_empty() {
|
||||
self.lines.push(String::new());
|
||||
}
|
||||
self.cursor_line = 1;
|
||||
self.is_insert_mode = false;
|
||||
self.is_markdown_view = self.file_name.to_lowercase().ends_with(".md");
|
||||
self.search_query.clear();
|
||||
self.is_searching = false;
|
||||
self.command_input.clear();
|
||||
self.is_command_mode = false;
|
||||
self.status_msg = Some(format!("Loaded {} lines", self.lines.len()));
|
||||
self.is_dirty = false;
|
||||
self.status_msg = Some(format!(
|
||||
"Loaded {} lines • {}",
|
||||
self.lines.len(),
|
||||
if self.is_writable { "Writable [i: edit, :w: save]" } else { "Read-Only" }
|
||||
));
|
||||
self.last_g_press = None;
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.is_open = false;
|
||||
self.lines.clear();
|
||||
self.buffer_content.clear();
|
||||
self.is_insert_mode = false;
|
||||
self.is_dirty = false;
|
||||
}
|
||||
|
||||
pub fn trigger_save(&mut self) {
|
||||
if !self.is_writable {
|
||||
self.status_msg = Some("❌ Cannot save: folder is Read-Only".to_string());
|
||||
return;
|
||||
}
|
||||
|
||||
if let (Some(url), Some(fid)) = (&self.peer_url, &self.folder_id) {
|
||||
self.pending_save_request = Some((
|
||||
url.clone(),
|
||||
fid.clone(),
|
||||
self.relative_path.clone(),
|
||||
self.buffer_content.clone(),
|
||||
self.pin.clone(),
|
||||
));
|
||||
self.status_msg = Some("⏳ Saving changes to remote peer...".to_string());
|
||||
self.is_dirty = false;
|
||||
} else {
|
||||
self.status_msg = Some("❌ No remote endpoint associated with this file".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync_buffer_to_lines(&mut self) {
|
||||
self.lines = self.buffer_content.lines().map(|s| s.to_string()).collect();
|
||||
if self.lines.is_empty() {
|
||||
self.lines.push(String::new());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_keys(&mut self, ctx: &egui::Context) {
|
||||
@@ -69,9 +150,13 @@ impl VimViewerModal {
|
||||
|
||||
let input = ctx.input(|i| i.clone());
|
||||
|
||||
// Escape always cancels search or command mode, or exits if already normal
|
||||
// Escape always leaves Insert, Search, or Command mode
|
||||
if input.key_pressed(Key::Escape) {
|
||||
if self.is_searching {
|
||||
if self.is_insert_mode {
|
||||
self.is_insert_mode = false;
|
||||
self.sync_buffer_to_lines();
|
||||
self.status_msg = Some("-- NORMAL --".to_string());
|
||||
} else if self.is_searching {
|
||||
self.is_searching = false;
|
||||
} else if self.is_command_mode {
|
||||
self.is_command_mode = false;
|
||||
@@ -82,6 +167,11 @@ impl VimViewerModal {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.is_insert_mode {
|
||||
// While in insert mode, typing is handled directly by egui::TextEdit
|
||||
return;
|
||||
}
|
||||
|
||||
if self.is_searching {
|
||||
if input.key_pressed(Key::Enter) {
|
||||
self.is_searching = false;
|
||||
@@ -103,6 +193,17 @@ impl VimViewerModal {
|
||||
// NORMAL Mode Keybindings
|
||||
let total = self.lines.len().max(1);
|
||||
|
||||
// i or a (enter Insert mode if writable)
|
||||
if (input.key_pressed(Key::I) || input.key_pressed(Key::A)) && !input.modifiers.ctrl && !input.modifiers.alt {
|
||||
if self.is_writable {
|
||||
self.is_insert_mode = true;
|
||||
self.status_msg = Some("-- INSERT -- (type code, Esc to finish)".to_string());
|
||||
return;
|
||||
} else {
|
||||
self.status_msg = Some("Cannot enter Insert mode: Read-Only share".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// j / Down (move down)
|
||||
if input.key_pressed(Key::J) || input.key_pressed(Key::ArrowDown) {
|
||||
self.cursor_line = (self.cursor_line + 1).min(total);
|
||||
@@ -158,14 +259,26 @@ impl VimViewerModal {
|
||||
}
|
||||
|
||||
fn execute_command(&mut self, cmd: &str) {
|
||||
if cmd == "q" || cmd == "q!" || cmd == "quit" {
|
||||
self.close();
|
||||
} else if let Ok(line_num) = cmd.parse::<usize>() {
|
||||
let total = self.lines.len().max(1);
|
||||
self.cursor_line = line_num.clamp(1, total);
|
||||
self.status_msg = Some(format!("Jumped to line {}", self.cursor_line));
|
||||
} else {
|
||||
self.status_msg = Some(format!("Unknown command: :{}", cmd));
|
||||
match cmd {
|
||||
"w" | "write" => {
|
||||
self.trigger_save();
|
||||
}
|
||||
"wq" | "x" => {
|
||||
self.trigger_save();
|
||||
self.close();
|
||||
}
|
||||
"q" | "q!" | "quit" => {
|
||||
self.close();
|
||||
}
|
||||
_ => {
|
||||
if let Ok(line_num) = cmd.parse::<usize>() {
|
||||
let total = self.lines.len().max(1);
|
||||
self.cursor_line = line_num.clamp(1, total);
|
||||
self.status_msg = Some(format!("Jumped to line {}", self.cursor_line));
|
||||
} else {
|
||||
self.status_msg = Some(format!("Unknown command: :{}", cmd));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,54 +320,151 @@ impl VimViewerModal {
|
||||
|
||||
let mut is_open = self.is_open;
|
||||
let screen_rect = ctx.screen_rect();
|
||||
let win_size = Vec2::new((screen_rect.width() * 0.92).min(1100.0), (screen_rect.height() * 0.88).min(750.0));
|
||||
let win_size = Vec2::new(
|
||||
(screen_rect.width() * 0.94).min(1150.0),
|
||||
(screen_rect.height() * 0.90).min(800.0),
|
||||
);
|
||||
|
||||
egui::Window::new(format!("⚡ Vim Viewer • {} ({})", self.file_name, self.source_peer))
|
||||
.open(&mut is_open)
|
||||
.collapsible(false)
|
||||
.resizable(true)
|
||||
.default_size(win_size)
|
||||
.anchor(egui::Align2::CENTER_CENTER, Vec2::ZERO)
|
||||
.frame(
|
||||
Frame::window(&ctx.style())
|
||||
.fill(Color32::from_rgb(15, 18, 28))
|
||||
.stroke(Stroke::new(1.0_f32, Color32::from_rgb(51, 65, 85)))
|
||||
.rounding(Rounding::same(8.0))
|
||||
.inner_margin(Margin::same(0.0)),
|
||||
)
|
||||
.show(ctx, |ui| {
|
||||
// 1. Top Header Bar
|
||||
Frame::none()
|
||||
.fill(Color32::from_rgb(22, 27, 44))
|
||||
.inner_margin(Margin::symmetric(14.0, 10.0))
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(RichText::new("📄").size(15.0));
|
||||
ui.label(
|
||||
RichText::new(&self.relative_path)
|
||||
.strong()
|
||||
.color(Color32::from_rgb(248, 250, 252))
|
||||
.size(13.0),
|
||||
);
|
||||
egui::Window::new(format!(
|
||||
"⚡ Vim Editor • {} ({})",
|
||||
self.file_name, self.source_peer
|
||||
))
|
||||
.open(&mut is_open)
|
||||
.collapsible(false)
|
||||
.resizable(true)
|
||||
.default_size(win_size)
|
||||
.anchor(egui::Align2::CENTER_CENTER, Vec2::ZERO)
|
||||
.frame(
|
||||
Frame::window(&ctx.style())
|
||||
.fill(Color32::from_rgb(13, 16, 24))
|
||||
.stroke(Stroke::new(1.0_f32, Color32::from_rgb(51, 65, 85)))
|
||||
.rounding(Rounding::same(8.0))
|
||||
.inner_margin(Margin::same(0.0)),
|
||||
)
|
||||
.show(ctx, |ui| {
|
||||
// 1. Top Header Bar
|
||||
Frame::none()
|
||||
.fill(Color32::from_rgb(20, 25, 40))
|
||||
.inner_margin(Margin::symmetric(14.0, 10.0))
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(RichText::new("📄").size(15.0));
|
||||
ui.label(
|
||||
RichText::new(&self.relative_path)
|
||||
.strong()
|
||||
.color(Color32::from_rgb(248, 250, 252))
|
||||
.size(13.0),
|
||||
);
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button(RichText::new("✕ Close (:q)").color(Color32::from_rgb(203, 213, 225))).clicked() {
|
||||
self.is_open = false;
|
||||
if self.is_dirty {
|
||||
ui.label(RichText::new("[+]").color(Color32::from_rgb(245, 158, 11)).strong());
|
||||
}
|
||||
|
||||
if self.is_writable {
|
||||
ui.label(RichText::new("• [Writable]").color(Color32::from_rgb(34, 197, 94)).size(11.0));
|
||||
} else {
|
||||
ui.label(RichText::new("• [Read-Only]").color(Color32::from_rgb(148, 163, 184)).size(11.0));
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui
|
||||
.button(RichText::new("✕ Close (:q)").color(Color32::from_rgb(203, 213, 225)))
|
||||
.clicked()
|
||||
{
|
||||
self.close();
|
||||
}
|
||||
|
||||
if self.is_writable {
|
||||
let save_btn = egui::Button::new(
|
||||
RichText::new("💾 Save (:w)")
|
||||
.strong()
|
||||
.color(Color32::WHITE),
|
||||
)
|
||||
.fill(Color32::from_rgb(37, 99, 235))
|
||||
.rounding(Rounding::same(4.0));
|
||||
|
||||
if ui.add(save_btn).clicked() {
|
||||
self.trigger_save();
|
||||
}
|
||||
|
||||
ui.label(
|
||||
RichText::new(format!("{} lines", self.lines.len()))
|
||||
.size(11.0)
|
||||
.color(Color32::from_rgb(148, 163, 184)),
|
||||
);
|
||||
});
|
||||
let edit_label = if self.is_insert_mode { "👁 View Mode (Esc)" } else { "✏️ Edit (i)" };
|
||||
if ui.button(RichText::new(edit_label).color(Color32::from_rgb(226, 232, 240))).clicked() {
|
||||
self.is_insert_mode = !self.is_insert_mode;
|
||||
if !self.is_insert_mode {
|
||||
self.sync_buffer_to_lines();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.file_name.to_lowercase().ends_with(".md") {
|
||||
if ui
|
||||
.selectable_label(self.is_markdown_view, "👁 Markdown Preview")
|
||||
.clicked()
|
||||
{
|
||||
self.is_markdown_view = !self.is_markdown_view;
|
||||
}
|
||||
}
|
||||
|
||||
ui.label(
|
||||
RichText::new(format!("{} lines", self.lines.len()))
|
||||
.size(11.0)
|
||||
.color(Color32::from_rgb(148, 163, 184)),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 2. Code Area with Line Numbers Gutter
|
||||
// 2. Editor / Viewer Body
|
||||
let available_height = ui.available_height() - 34.0;
|
||||
|
||||
if self.is_markdown_view {
|
||||
// Markdown Preview View
|
||||
ScrollArea::vertical()
|
||||
.max_height(available_height)
|
||||
.auto_shrink([false, false])
|
||||
.show(ui, |ui| {
|
||||
Frame::none()
|
||||
.inner_margin(Margin::same(20.0))
|
||||
.show(ui, |ui| {
|
||||
for line in self.buffer_content.lines() {
|
||||
if line.starts_with("# ") {
|
||||
ui.heading(RichText::new(&line[2..]).color(Color32::from_rgb(56, 189, 248)).size(20.0));
|
||||
} else if line.starts_with("## ") {
|
||||
ui.label(RichText::new(&line[3..]).strong().color(Color32::from_rgb(96, 165, 250)).size(16.0));
|
||||
} else if line.starts_with("### ") {
|
||||
ui.label(RichText::new(&line[4..]).strong().color(Color32::from_rgb(147, 197, 253)).size(14.0));
|
||||
} else if line.starts_with("* ") || line.starts_with("- ") {
|
||||
ui.label(RichText::new(format!(" • {}", &line[2..])).color(Color32::from_rgb(226, 232, 240)));
|
||||
} else if line.starts_with("```") {
|
||||
ui.label(RichText::new(line).monospace().color(Color32::from_rgb(148, 163, 184)));
|
||||
} else if line.trim().is_empty() {
|
||||
ui.add_space(6.0);
|
||||
} else {
|
||||
ui.label(RichText::new(line).color(Color32::from_rgb(226, 232, 240)));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if self.is_insert_mode {
|
||||
// Live Insert Mode Text Editor
|
||||
ScrollArea::vertical()
|
||||
.max_height(available_height)
|
||||
.auto_shrink([false, false])
|
||||
.show(ui, |ui| {
|
||||
let edit = TextEdit::multiline(&mut self.buffer_content)
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.desired_width(f32::INFINITY)
|
||||
.lock_focus(true)
|
||||
.frame(false);
|
||||
|
||||
let resp = ui.add(edit);
|
||||
if resp.changed() {
|
||||
self.is_dirty = true;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Normal Vim Line-Numbered Viewer
|
||||
let gutter_width = 54.0;
|
||||
let available_height = ui.available_height() - 32.0;
|
||||
|
||||
ScrollArea::vertical()
|
||||
.max_height(available_height)
|
||||
.auto_shrink([false, false])
|
||||
@@ -266,7 +476,7 @@ impl VimViewerModal {
|
||||
for idx in 1..=self.lines.len() {
|
||||
let is_current = idx == self.cursor_line;
|
||||
let num_color = if is_current {
|
||||
Color32::from_rgb(245, 158, 11) // Amber current line
|
||||
Color32::from_rgb(245, 158, 11)
|
||||
} else {
|
||||
Color32::from_rgb(71, 85, 105)
|
||||
};
|
||||
@@ -281,7 +491,6 @@ impl VimViewerModal {
|
||||
});
|
||||
});
|
||||
|
||||
// Vertical divider line
|
||||
ui.add(egui::Separator::default().vertical());
|
||||
|
||||
// Code column
|
||||
@@ -292,7 +501,6 @@ impl VimViewerModal {
|
||||
for (idx, line) in self.lines.iter().enumerate() {
|
||||
let line_num = idx + 1;
|
||||
let is_current = line_num == self.cursor_line;
|
||||
|
||||
let line_display = if line.is_empty() { " " } else { line.as_str() };
|
||||
|
||||
let line_text = if is_current {
|
||||
@@ -320,70 +528,73 @@ impl VimViewerModal {
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Bottom Vim Status Bar
|
||||
Frame::none()
|
||||
.fill(Color32::from_rgb(11, 15, 25))
|
||||
.stroke(Stroke::new(1.0_f32, Color32::from_rgb(30, 41, 59)))
|
||||
.inner_margin(Margin::symmetric(10.0, 5.0))
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
// Mode Pill
|
||||
let (mode_text, mode_bg, mode_fg) = if self.is_searching {
|
||||
("SEARCH", Color32::from_rgb(217, 119, 6), Color32::WHITE)
|
||||
} else if self.is_command_mode {
|
||||
("COMMAND", Color32::from_rgb(147, 51, 234), Color32::WHITE)
|
||||
} else {
|
||||
("NORMAL", Color32::from_rgb(37, 99, 235), Color32::WHITE)
|
||||
};
|
||||
// 3. Bottom Vim Status Bar
|
||||
Frame::none()
|
||||
.fill(Color32::from_rgb(10, 13, 22))
|
||||
.stroke(Stroke::new(1.0_f32, Color32::from_rgb(30, 41, 59)))
|
||||
.inner_margin(Margin::symmetric(10.0, 5.0))
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
// Mode Pill
|
||||
let (mode_text, mode_bg, mode_fg) = if self.is_insert_mode {
|
||||
("INSERT", Color32::from_rgb(34, 197, 94), Color32::BLACK)
|
||||
} else if self.is_searching {
|
||||
("SEARCH", Color32::from_rgb(217, 119, 6), Color32::WHITE)
|
||||
} else if self.is_command_mode {
|
||||
("COMMAND", Color32::from_rgb(147, 51, 234), Color32::WHITE)
|
||||
} else {
|
||||
("NORMAL", Color32::from_rgb(37, 99, 235), Color32::WHITE)
|
||||
};
|
||||
|
||||
Frame::none()
|
||||
.fill(mode_bg)
|
||||
.rounding(Rounding::same(3.0))
|
||||
.inner_margin(Margin::symmetric(6.0, 2.0))
|
||||
.show(ui, |ui| {
|
||||
ui.label(RichText::new(mode_text).size(10.5).strong().color(mode_fg));
|
||||
});
|
||||
|
||||
if self.is_searching {
|
||||
ui.label(RichText::new("/").monospace().color(Color32::from_rgb(245, 158, 11)));
|
||||
let edit = TextEdit::singleline(&mut self.search_query)
|
||||
.hint_text("pattern (press Enter)...")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.desired_width(200.0);
|
||||
ui.add(edit).request_focus();
|
||||
} else if self.is_command_mode {
|
||||
ui.label(RichText::new(":").monospace().color(Color32::from_rgb(168, 85, 247)));
|
||||
let edit = TextEdit::singleline(&mut self.command_input)
|
||||
.hint_text("q / line number...")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.desired_width(200.0);
|
||||
ui.add(edit).request_focus();
|
||||
} else {
|
||||
if let Some(msg) = &self.status_msg {
|
||||
ui.label(RichText::new(msg).size(11.0).color(Color32::from_rgb(203, 213, 225)));
|
||||
}
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.label(
|
||||
RichText::new("j/k: nav • gg/G: top/bot • /: find • :q: exit")
|
||||
.size(10.0)
|
||||
.color(Color32::from_rgb(100, 116, 139)),
|
||||
);
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label(
|
||||
RichText::new(format!("Ln {}, Col 1", self.cursor_line))
|
||||
.size(11.0)
|
||||
.monospace()
|
||||
.color(Color32::from_rgb(148, 163, 184)),
|
||||
);
|
||||
Frame::none()
|
||||
.fill(mode_bg)
|
||||
.rounding(Rounding::same(3.0))
|
||||
.inner_margin(Margin::symmetric(6.0, 2.0))
|
||||
.show(ui, |ui| {
|
||||
ui.label(RichText::new(mode_text).size(10.5).strong().color(mode_fg));
|
||||
});
|
||||
|
||||
if self.is_searching {
|
||||
ui.label(RichText::new("/").monospace().color(Color32::from_rgb(245, 158, 11)));
|
||||
let edit = TextEdit::singleline(&mut self.search_query)
|
||||
.hint_text("pattern (press Enter)...")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.desired_width(200.0);
|
||||
ui.add(edit).request_focus();
|
||||
} else if self.is_command_mode {
|
||||
ui.label(RichText::new(":").monospace().color(Color32::from_rgb(168, 85, 247)));
|
||||
let edit = TextEdit::singleline(&mut self.command_input)
|
||||
.hint_text("w (save) / q (quit) / line...")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.desired_width(200.0);
|
||||
ui.add(edit).request_focus();
|
||||
} else {
|
||||
if let Some(msg) = &self.status_msg {
|
||||
ui.label(RichText::new(msg).size(11.0).color(Color32::from_rgb(203, 213, 225)));
|
||||
}
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.label(
|
||||
RichText::new("i: edit • Esc: normal • :w: save • :q: exit")
|
||||
.size(10.0)
|
||||
.color(Color32::from_rgb(100, 116, 139)),
|
||||
);
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label(
|
||||
RichText::new(format!("Ln {}, Col 1", self.cursor_line))
|
||||
.size(11.0)
|
||||
.monospace()
|
||||
.color(Color32::from_rgb(148, 163, 184)),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
self.is_open = is_open;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user