mirror of
https://github.com/veeso/termscp.git
synced 2026-04-04 01:01:33 -07:00
Comprehensive design for incremental refactoring of the 13k-line FileTransferActivity god-struct using a unified Pane abstraction. Detailed step-by-step plan covering 6 phases: split monoliths, error handling, Pane struct, action dedup, session split, view reorg. Extract 26 popup components from the monolithic 1,868-line popups.rs into 20 individual files under popups/. Each file contains one or two related components with their own imports. The popups.rs module file now contains only module declarations and re-exports. Replace 8 panic!() calls with error!() logging and early returns/fallthrough. These panics documented invariants (e.g. "this tab can't do X") but would crash the app if somehow triggered. Error logging is safer and more resilient. Replace raw FileExplorer fields in Browser with Pane structs that bundle the explorer and connected state. Move host_bridge_connected and remote_connected from FileTransferActivity into the panes. Add navigation API (fs_pane, opposite_pane, is_find_tab) for future unification tasks. Rename private get_selected_file to get_selected_file_by_id and add three new unified methods (get_selected_entries, get_selected_file, is_selected_one) that dispatch based on self.browser.tab(). Old per-tab methods are kept for now until their callers are migrated in subsequent tasks. Collapse _local_/_remote_ action method pairs (mkdir, delete, symlink, chmod, rename, copy) into unified methods that branch internally on is_local_tab(). This halves the number of action methods and simplifies the update.rs dispatch logic. Also unifies ShowFileInfoPopup and ShowChmodPopup dispatching to use get_selected_entries(). Move `host_bridge` and `client` filesystem fields from FileTransferActivity into the Pane struct, enabling tab-agnostic dispatch via `fs_pane()`/ `fs_pane_mut()`. This eliminates most `is_local_tab()` branching across 15+ action files. Key changes: - Add `fs: Box<dyn HostBridge>` to Pane, remove from FileTransferActivity - Replace per-side method pairs with unified pane-dispatched methods - Unify navigation (changedir, reload, scan, file_exists, has_file_changed) - Replace 147-line popup if/else chain with data-driven priority table - Replace assert!/panic!/unreachable! with proper error handling - Fix typo "filetransfer_activiy" across ~29 files - Add unit tests for Pane Net result: -473 lines, single code path for most file operations.
119 lines
4.9 KiB
Rust
119 lines
4.9 KiB
Rust
use std::env;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use super::super::{ConfigClient, FileTransferActivity};
|
|
use crate::filetransfer::{HostBridgeParams, ProtocolParams};
|
|
use crate::system::environment;
|
|
use crate::utils::path;
|
|
|
|
impl FileTransferActivity {
|
|
/// Initialize configuration client if possible.
|
|
/// This function doesn't return errors.
|
|
pub(in crate::ui::activities::filetransfer) fn init_config_client() -> ConfigClient {
|
|
match environment::init_config_dir() {
|
|
Ok(termscp_dir) => match termscp_dir {
|
|
Some(termscp_dir) => {
|
|
// Make configuration file path and ssh keys path
|
|
let (config_path, ssh_keys_path): (PathBuf, PathBuf) =
|
|
environment::get_config_paths(termscp_dir.as_path());
|
|
match ConfigClient::new(config_path.as_path(), ssh_keys_path.as_path()) {
|
|
Ok(config_client) => config_client,
|
|
Err(_) => ConfigClient::degraded(),
|
|
}
|
|
}
|
|
None => ConfigClient::degraded(),
|
|
},
|
|
Err(_) => ConfigClient::degraded(),
|
|
}
|
|
}
|
|
|
|
/// Set text editor to use
|
|
pub(in crate::ui::activities::filetransfer) fn setup_text_editor(&self) {
|
|
unsafe {
|
|
env::set_var("EDITOR", self.config().get_text_editor());
|
|
}
|
|
}
|
|
|
|
/// Convert a path to absolute according to the current tab's pane explorer
|
|
pub(in crate::ui::activities::filetransfer) fn pane_to_abs_path(&self, path: &Path) -> PathBuf {
|
|
path::absolutize(self.browser.fs_pane().explorer.wrkdir.as_path(), path)
|
|
}
|
|
|
|
/// Get remote hostname
|
|
pub(in crate::ui::activities::filetransfer) fn get_remote_hostname(&self) -> String {
|
|
let ft_params = self.context().remote_params().unwrap();
|
|
self.get_hostname(&ft_params.params)
|
|
}
|
|
|
|
pub(in crate::ui::activities::filetransfer) fn get_hostbridge_hostname(&self) -> String {
|
|
let host_bridge_params = self.context().host_bridge_params().unwrap();
|
|
match host_bridge_params {
|
|
HostBridgeParams::Localhost(_) => {
|
|
let hostname = match hostname::get() {
|
|
Ok(h) => h,
|
|
Err(_) => return String::from("localhost"),
|
|
};
|
|
let hostname: String = hostname.as_os_str().to_string_lossy().to_string();
|
|
let tokens: Vec<&str> = hostname.split('.').collect();
|
|
String::from(*tokens.first().unwrap_or(&"localhost"))
|
|
}
|
|
HostBridgeParams::Remote(_, params) => self.get_hostname(params),
|
|
}
|
|
}
|
|
|
|
fn get_hostname(&self, params: &ProtocolParams) -> String {
|
|
match params {
|
|
ProtocolParams::Generic(params) => params.address.clone(),
|
|
ProtocolParams::AwsS3(params) => params.bucket_name.clone(),
|
|
ProtocolParams::Kube(params) => {
|
|
params.namespace.clone().unwrap_or("default".to_string())
|
|
}
|
|
ProtocolParams::Smb(params) => params.address.clone(),
|
|
ProtocolParams::WebDAV(params) => params.uri.clone(),
|
|
}
|
|
}
|
|
|
|
/// Get connection message to show to client
|
|
pub(in crate::ui::activities::filetransfer) fn get_connection_msg(
|
|
params: &ProtocolParams,
|
|
) -> String {
|
|
match params {
|
|
ProtocolParams::Generic(params) => {
|
|
info!(
|
|
"Client is not connected to remote; connecting to {}:{}",
|
|
params.address, params.port
|
|
);
|
|
format!("Connecting to {}:{}…", params.address, params.port)
|
|
}
|
|
ProtocolParams::AwsS3(params) => {
|
|
info!(
|
|
"Client is not connected to remote; connecting to {}{} ({})",
|
|
params.endpoint.as_deref().unwrap_or(""),
|
|
params.bucket_name,
|
|
params.region.as_deref().unwrap_or("custom")
|
|
);
|
|
format!("Connecting to {}…", params.bucket_name)
|
|
}
|
|
ProtocolParams::Kube(params) => {
|
|
let namespace = params.namespace.as_deref().unwrap_or("default");
|
|
info!("Client is not connected to remote; connecting to namespace {namespace}",);
|
|
format!("Connecting to Kube namespace {namespace}…",)
|
|
}
|
|
ProtocolParams::Smb(params) => {
|
|
info!(
|
|
"Client is not connected to remote; connecting to {}:{}",
|
|
params.address, params.share
|
|
);
|
|
format!("Connecting to \\\\{}\\{}…", params.address, params.share)
|
|
}
|
|
ProtocolParams::WebDAV(params) => {
|
|
info!(
|
|
"Client is not connected to remote; connecting to {}",
|
|
params.uri
|
|
);
|
|
format!("Connecting to {}…", params.uri)
|
|
}
|
|
}
|
|
}
|
|
}
|