context getters

This commit is contained in:
veeso
2021-07-08 15:43:23 +02:00
parent e6b44e1461
commit 37abe596c7
14 changed files with 168 additions and 97 deletions
+12 -9
View File
@@ -97,14 +97,17 @@ impl ActivityManager {
entry_directory: Option<PathBuf>, entry_directory: Option<PathBuf>,
) { ) {
// Put params into the context // Put params into the context
self.context.as_mut().unwrap().ft_params = Some(FileTransferParams { self.context
address, .as_mut()
port, .unwrap()
protocol, .set_ftparams(FileTransferParams {
username, address,
password, port,
entry_directory, protocol,
}); username,
password,
entry_directory,
});
} }
/// ### run /// ### run
@@ -202,7 +205,7 @@ impl ActivityManager {
} }
}; };
// If ft params is None, return None // If ft params is None, return None
let ft_params: &FileTransferParams = match ctx.ft_params.as_ref() { let ft_params: &FileTransferParams = match ctx.ft_params() {
Some(ft_params) => &ft_params, Some(ft_params) => &ft_params,
None => { None => {
error!("Failed to start FileTransferActivity: file transfer params is None"); error!("Failed to start FileTransferActivity: file transfer params is None");
+26 -27
View File
@@ -111,23 +111,24 @@ impl AuthActivity {
fn check_for_updates(&mut self) { fn check_for_updates(&mut self) {
debug!("Check for updates..."); debug!("Check for updates...");
// Check version only if unset in the store // Check version only if unset in the store
let ctx: &mut Context = self.context.as_mut().unwrap(); let ctx: &mut Context = self.context_mut();
if !ctx.store.isset(STORE_KEY_LATEST_VERSION) { if !ctx.store().isset(STORE_KEY_LATEST_VERSION) {
debug!("Version is not set in storage"); debug!("Version is not set in storage");
if ctx.config_client.get_check_for_updates() { if ctx.config().get_check_for_updates() {
debug!("Check for updates is enabled"); debug!("Check for updates is enabled");
// Send request // Send request
match git::check_for_updates(env!("CARGO_PKG_VERSION")) { match git::check_for_updates(env!("CARGO_PKG_VERSION")) {
Ok(Some(git::GithubTag { tag_name, body })) => { Ok(Some(git::GithubTag { tag_name, body })) => {
// If some, store version and release notes // If some, store version and release notes
info!("Latest version is: {}", tag_name); info!("Latest version is: {}", tag_name);
ctx.store.set_string(STORE_KEY_LATEST_VERSION, tag_name); ctx.store_mut()
ctx.store.set_string(STORE_KEY_RELEASE_NOTES, body); .set_string(STORE_KEY_LATEST_VERSION, tag_name);
ctx.store_mut().set_string(STORE_KEY_RELEASE_NOTES, body);
} }
Ok(None) => { Ok(None) => {
info!("Latest version is: {} (current)", env!("CARGO_PKG_VERSION")); info!("Latest version is: {} (current)", env!("CARGO_PKG_VERSION"));
// Just set flag as check // Just set flag as check
ctx.store.set(STORE_KEY_LATEST_VERSION); ctx.store_mut().set(STORE_KEY_LATEST_VERSION);
} }
Err(err) => { Err(err) => {
// Report error // Report error
@@ -140,30 +141,28 @@ impl AuthActivity {
} else { } else {
info!("Check for updates is disabled"); info!("Check for updates is disabled");
} }
/*
let ctx: &mut Context = self.context.as_mut().unwrap();
// Set version into the store (or just a flag)
match github_tag.take() {
Some(git::GithubTag { tag_name, body }) => {
// If some store version and release notes
info!("Latest version is: {}", tag_name);
ctx.store.set_string(STORE_KEY_LATEST_VERSION, tag_name);
ctx.store.set_string(STORE_KEY_RELEASE_NOTES, body);
}
None => {
info!("Latest version is: {} (current)", env!("CARGO_PKG_VERSION"));
// Just set flag as check
ctx.store.set(STORE_KEY_LATEST_VERSION);
}
}*/
} }
} }
/// ### context
///
/// Returns a reference to context
fn context(&self) -> &Context {
self.context.as_ref().unwrap()
}
/// ### context_mut
///
/// Returns a mutable reference to context
fn context_mut(&mut self) -> &mut Context {
self.context.as_mut().unwrap()
}
/// ### theme /// ### theme
/// ///
/// Returns a reference to theme /// Returns a reference to theme
fn theme(&self) -> &Theme { fn theme(&self) -> &Theme {
self.context.as_ref().unwrap().theme_provider.theme() self.context().theme_provider().theme()
} }
} }
@@ -176,11 +175,11 @@ impl Activity for AuthActivity {
fn on_create(&mut self, mut context: Context) { fn on_create(&mut self, mut context: Context) {
debug!("Initializing activity"); debug!("Initializing activity");
// Initialize file transfer params // Initialize file transfer params
context.ft_params = Some(FileTransferParams::default()); context.set_ftparams(FileTransferParams::default());
// Set context // Set context
self.context = Some(context); self.context = Some(context);
// Clear terminal // Clear terminal
self.context.as_mut().unwrap().clear_screen(); self.context_mut().clear_screen();
// Put raw mode on enabled // Put raw mode on enabled
if let Err(err) = enable_raw_mode() { if let Err(err) = enable_raw_mode() {
error!("Failed to enter raw mode: {}", err); error!("Failed to enter raw mode: {}", err);
@@ -197,7 +196,7 @@ impl Activity for AuthActivity {
self.view_recent_connections(); self.view_recent_connections();
} }
// Verify error state from context // Verify error state from context
if let Some(err) = self.context.as_mut().unwrap().error() { if let Some(err) = self.context_mut().error() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
info!("Activity initialized"); info!("Activity initialized");
@@ -213,7 +212,7 @@ impl Activity for AuthActivity {
return; return;
} }
// Read one event // Read one event
if let Ok(Some(event)) = self.context.as_ref().unwrap().input_hnd.read_event() { if let Ok(Some(event)) = self.context().input_hnd().read_event() {
// Set redraw to true // Set redraw to true
self.redraw = true; self.redraw = true;
// Handle on resize // Handle on resize
+14 -12
View File
@@ -331,19 +331,21 @@ impl Update for AuthActivity {
self.save_recent(); self.save_recent();
let (address, port, protocol, username, password) = self.get_input(); let (address, port, protocol, username, password) = self.get_input();
// Set file transfer params to context // Set file transfer params to context
let mut ft_params: &mut FileTransferParams = let params: FileTransferParams = FileTransferParams {
&mut self.context.as_mut().unwrap().ft_params.as_mut().unwrap(); address,
ft_params.address = address; port,
ft_params.port = port; protocol,
ft_params.protocol = protocol; username: match username.is_empty() {
ft_params.username = match username.is_empty() { true => None,
true => None, false => Some(username),
false => Some(username), },
}; password: match password.is_empty() {
ft_params.password = match password.is_empty() { true => None,
true => None, false => Some(password),
false => Some(password), },
entry_directory: None,
}; };
self.context_mut().set_ftparams(params);
// Set exit reason // Set exit reason
self.exit_reason = Some(super::ExitReason::Connect); self.exit_reason = Some(super::ExitReason::Connect);
// Return None // Return None
+7 -13
View File
@@ -109,12 +109,7 @@ impl AuthActivity {
)), )),
); );
// Get default protocol // Get default protocol
let default_protocol: FileTransferProtocol = self let default_protocol: FileTransferProtocol = self.context().config().get_default_protocol();
.context
.as_ref()
.unwrap()
.config_client
.get_default_protocol();
// Protocol // Protocol
self.view.mount( self.view.mount(
super::COMPONENT_RADIO_PROTOCOL, super::COMPONENT_RADIO_PROTOCOL,
@@ -186,12 +181,11 @@ impl AuthActivity {
); );
// Version notice // Version notice
if let Some(version) = self if let Some(version) = self
.context .context()
.as_ref() .store()
.unwrap()
.store
.get_string(super::STORE_KEY_LATEST_VERSION) .get_string(super::STORE_KEY_LATEST_VERSION)
{ {
let version: String = version.to_string();
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_NEW_VERSION, super::COMPONENT_TEXT_NEW_VERSION,
Box::new(Span::new( Box::new(Span::new(
@@ -199,7 +193,7 @@ impl AuthActivity {
.with_foreground(Color::Yellow) .with_foreground(Color::Yellow)
.with_spans(vec![ .with_spans(vec![
TextSpan::from("termscp "), TextSpan::from("termscp "),
TextSpanBuilder::new(version).underlined().bold().build(), TextSpanBuilder::new(version.as_str()).underlined().bold().build(),
TextSpan::from(" is NOW available! Get it from <https://veeso.github.io/termscp/>; view release notes with <CTRL+R>"), TextSpan::from(" is NOW available! Get it from <https://veeso.github.io/termscp/>; view release notes with <CTRL+R>"),
]) ])
.build(), .build(),
@@ -242,7 +236,7 @@ impl AuthActivity {
/// Display view on canvas /// Display view on canvas
pub(super) fn view(&mut self) { pub(super) fn view(&mut self) {
let mut ctx: Context = self.context.take().unwrap(); let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| { let _ = ctx.terminal().draw(|f| {
// Check window size // Check window size
let height: u16 = f.size().height; let height: u16 = f.size().height;
self.check_minimum_window_size(height); self.check_minimum_window_size(height);
@@ -784,7 +778,7 @@ impl AuthActivity {
/// mount release notes text area /// mount release notes text area
pub(super) fn mount_release_notes(&mut self) { pub(super) fn mount_release_notes(&mut self) {
if let Some(ctx) = self.context.as_ref() { if let Some(ctx) = self.context.as_ref() {
if let Some(release_notes) = ctx.store.get_string(super::STORE_KEY_RELEASE_NOTES) { if let Some(release_notes) = ctx.store().get_string(super::STORE_KEY_RELEASE_NOTES) {
// make spans // make spans
let spans: Vec<TextSpan> = release_notes.lines().map(TextSpan::from).collect(); let spans: Vec<TextSpan> = release_notes.lines().map(TextSpan::from).collect();
self.view.mount( self.view.mount(
+1 -1
View File
@@ -111,7 +111,7 @@ impl FileTransferActivity {
/// Read one event. /// Read one event.
/// Returns whether at least one event has been handled /// Returns whether at least one event has been handled
pub(super) fn read_input_event(&mut self) -> bool { pub(super) fn read_input_event(&mut self) -> bool {
if let Ok(Some(event)) = self.context.as_ref().unwrap().input_hnd.read_event() { if let Ok(Some(event)) = self.context().input_hnd().read_event() {
// Handle event // Handle event
let msg = self.view.on(event); let msg = self.view.on(event);
self.update(msg); self.update(msg);
+18 -4
View File
@@ -210,18 +210,32 @@ impl FileTransferActivity {
}) })
} }
/// ### context
///
/// Returns a reference to context
fn context(&self) -> &Context {
self.context.as_ref().unwrap()
}
/// ### context_mut
///
/// Returns a mutable reference to context
fn context_mut(&mut self) -> &mut Context {
self.context.as_mut().unwrap()
}
/// ### config /// ### config
/// ///
/// Returns config client reference /// Returns config client reference
fn config(&self) -> &ConfigClient { fn config(&self) -> &ConfigClient {
&self.context.as_ref().unwrap().config_client &self.context().config()
} }
/// ### theme /// ### theme
/// ///
/// Get a reference to `Theme` /// Get a reference to `Theme`
fn theme(&self) -> &Theme { fn theme(&self) -> &Theme {
self.context.as_ref().unwrap().theme_provider.theme() self.context().theme_provider().theme()
} }
} }
@@ -241,7 +255,7 @@ impl Activity for FileTransferActivity {
// Set context // Set context
self.context = Some(context); self.context = Some(context);
// Clear terminal // Clear terminal
self.context.as_mut().unwrap().clear_screen(); self.context_mut().clear_screen();
// Put raw mode on enabled // Put raw mode on enabled
if let Err(err) = enable_raw_mode() { if let Err(err) = enable_raw_mode() {
error!("Failed to enter raw mode: {}", err); error!("Failed to enter raw mode: {}", err);
@@ -276,7 +290,7 @@ impl Activity for FileTransferActivity {
} }
// Check if connected (popup must be None, otherwise would try reconnecting in loop in case of error) // Check if connected (popup must be None, otherwise would try reconnecting in loop in case of error)
if !self.client.is_connected() && self.view.get_props(COMPONENT_TEXT_FATAL).is_none() { if !self.client.is_connected() && self.view.get_props(COMPONENT_TEXT_FATAL).is_none() {
let params = self.context.as_ref().unwrap().ft_params.as_ref().unwrap(); let params = self.context().ft_params().unwrap();
info!( info!(
"Client is not connected to remote; connecting to {}:{}", "Client is not connected to remote; connecting to {}:{}",
params.address, params.port params.address, params.port
+5 -5
View File
@@ -76,15 +76,15 @@ impl FileTransferActivity {
/// ///
/// Connect to remote /// Connect to remote
pub(super) fn connect(&mut self) { pub(super) fn connect(&mut self) {
let params = self.context.as_ref().unwrap().ft_params.as_ref().unwrap(); let params = self.context().ft_params().unwrap().clone();
let addr: String = params.address.clone(); let addr: String = params.address.clone();
let entry_dir: Option<PathBuf> = params.entry_directory.clone(); let entry_dir: Option<PathBuf> = params.entry_directory.clone();
// Connect to remote // Connect to remote
match self.client.connect( match self.client.connect(
params.address.clone(), params.address,
params.port, params.port,
params.username.clone(), params.username,
params.password.clone(), params.password,
) { ) {
Ok(welcome) => { Ok(welcome) => {
if let Some(banner) = welcome { if let Some(banner) = welcome {
@@ -121,7 +121,7 @@ impl FileTransferActivity {
/// ///
/// disconnect from remote /// disconnect from remote
pub(super) fn disconnect(&mut self) { pub(super) fn disconnect(&mut self) {
let params = self.context.as_ref().unwrap().ft_params.as_ref().unwrap(); let params = self.context().ft_params().unwrap();
let msg: String = format!("Disconnecting from {}...", params.address); let msg: String = format!("Disconnecting from {}...", params.address);
// Show popup disconnecting // Show popup disconnecting
self.mount_wait(msg.as_str()); self.mount_wait(msg.as_str());
+5 -9
View File
@@ -720,10 +720,8 @@ impl FileTransferActivity {
Some(props) => { Some(props) => {
// Get width // Get width
let width: usize = self let width: usize = self
.context .context()
.as_ref() .store()
.unwrap()
.store
.get_unsigned(super::STORAGE_EXPLORER_WIDTH) .get_unsigned(super::STORAGE_EXPLORER_WIDTH)
.unwrap_or(256); .unwrap_or(256);
let hostname: String = match hostname::get() { let hostname: String = match hostname::get() {
@@ -768,13 +766,11 @@ impl FileTransferActivity {
Some(props) => { Some(props) => {
// Get width // Get width
let width: usize = self let width: usize = self
.context .context()
.as_ref() .store()
.unwrap()
.store
.get_unsigned(super::STORAGE_EXPLORER_WIDTH) .get_unsigned(super::STORAGE_EXPLORER_WIDTH)
.unwrap_or(256); .unwrap_or(256);
let params = self.context.as_ref().unwrap().ft_params.as_ref().unwrap(); let params = self.context().ft_params().unwrap();
let hostname: String = format!( let hostname: String = format!(
"{}:{} ", "{}:{} ",
params.address, params.address,
+3 -3
View File
@@ -92,7 +92,7 @@ impl SetupActivity {
None => Ok(()), None => Ok(()),
Some(ctx) => { Some(ctx) => {
// Set editor if config client exists // Set editor if config client exists
env::set_var("EDITOR", ctx.config_client.get_text_editor()); env::set_var("EDITOR", ctx.config().get_text_editor());
// Prepare terminal // Prepare terminal
if let Err(err) = disable_raw_mode() { if let Err(err) = disable_raw_mode() {
error!("Failed to disable raw mode: {}", err); error!("Failed to disable raw mode: {}", err);
@@ -101,10 +101,10 @@ impl SetupActivity {
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
ctx.leave_alternate_screen(); ctx.leave_alternate_screen();
// Get result // Get result
let result: Result<(), String> = match ctx.config_client.iter_ssh_keys().nth(idx) { let result: Result<(), String> = match ctx.config().iter_ssh_keys().nth(idx) {
Some(key) => { Some(key) => {
// Get key path // Get key path
match ctx.config_client.get_ssh_key(key) { match ctx.config().get_ssh_key(key) {
Ok(ssh_key) => match ssh_key { Ok(ssh_key) => match ssh_key {
None => Ok(()), None => Ok(()),
Some((_, _, key_path)) => { Some((_, _, key_path)) => {
+20 -6
View File
@@ -134,24 +134,38 @@ impl Default for SetupActivity {
} }
impl SetupActivity { impl SetupActivity {
/// ### context
///
/// Returns a reference to context
fn context(&self) -> &Context {
self.context.as_ref().unwrap()
}
/// ### context_mut
///
/// Returns a mutable reference to context
fn context_mut(&mut self) -> &mut Context {
self.context.as_mut().unwrap()
}
fn config(&self) -> &ConfigClient { fn config(&self) -> &ConfigClient {
&self.context.as_ref().unwrap().config_client &self.context().config()
} }
fn config_mut(&mut self) -> &mut ConfigClient { fn config_mut(&mut self) -> &mut ConfigClient {
&mut self.context.as_mut().unwrap().config_client self.context_mut().config_mut()
} }
fn theme(&self) -> &Theme { fn theme(&self) -> &Theme {
self.context.as_ref().unwrap().theme_provider.theme() self.context().theme_provider().theme()
} }
fn theme_mut(&mut self) -> &mut Theme { fn theme_mut(&mut self) -> &mut Theme {
self.context.as_mut().unwrap().theme_provider.theme_mut() self.context_mut().theme_provider_mut().theme_mut()
} }
fn theme_provider(&mut self) -> &mut ThemeProvider { fn theme_provider(&mut self) -> &mut ThemeProvider {
&mut self.context.as_mut().unwrap().theme_provider self.context_mut().theme_provider_mut()
} }
} }
@@ -188,7 +202,7 @@ impl Activity for SetupActivity {
return; return;
} }
// Read one event // Read one event
if let Ok(Some(event)) = self.context.as_ref().unwrap().input_hnd.read_event() { if let Ok(Some(event)) = self.context().input_hnd().read_event() {
// Set redraw to true // Set redraw to true
self.redraw = true; self.redraw = true;
// Handle event // Handle event
+1 -1
View File
@@ -197,7 +197,7 @@ impl SetupActivity {
pub(super) fn view_setup(&mut self) { pub(super) fn view_setup(&mut self) {
let mut ctx: Context = self.context.take().unwrap(); let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| { let _ = ctx.terminal().draw(|f| {
// Prepare main chunks // Prepare main chunks
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
+1 -1
View File
@@ -111,7 +111,7 @@ impl SetupActivity {
pub(crate) fn view_ssh_keys(&mut self) { pub(crate) fn view_ssh_keys(&mut self) {
let mut ctx: Context = self.context.take().unwrap(); let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| { let _ = ctx.terminal().draw(|f| {
// Prepare main chunks // Prepare main chunks
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
+1 -1
View File
@@ -175,7 +175,7 @@ impl SetupActivity {
pub(super) fn view_theme(&mut self) { pub(super) fn view_theme(&mut self) {
let mut ctx: Context = self.context.take().unwrap(); let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| { let _ = ctx.terminal().draw(|f| {
// Prepare main chunks // Prepare main chunks
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
+54 -5
View File
@@ -41,22 +41,25 @@ use std::path::PathBuf;
use tuirealm::tui::backend::CrosstermBackend; use tuirealm::tui::backend::CrosstermBackend;
use tuirealm::tui::Terminal; use tuirealm::tui::Terminal;
type TuiTerminal = Terminal<CrosstermBackend<Stdout>>;
/// ## Context /// ## Context
/// ///
/// Context holds data structures used by the ui /// Context holds data structures used by the ui
pub struct Context { pub struct Context {
pub ft_params: Option<FileTransferParams>, ft_params: Option<FileTransferParams>,
pub(crate) config_client: ConfigClient, config_client: ConfigClient,
pub(crate) store: Store, pub(crate) store: Store,
pub(crate) input_hnd: InputHandler, input_hnd: InputHandler,
pub(crate) terminal: Terminal<CrosstermBackend<Stdout>>, pub(crate) terminal: TuiTerminal,
pub(crate) theme_provider: ThemeProvider, theme_provider: ThemeProvider,
error: Option<String>, error: Option<String>,
} }
/// ### FileTransferParams /// ### FileTransferParams
/// ///
/// Holds connection parameters for file transfers /// Holds connection parameters for file transfers
#[derive(Clone)]
pub struct FileTransferParams { pub struct FileTransferParams {
pub address: String, pub address: String,
pub port: u16, pub port: u16,
@@ -89,6 +92,52 @@ impl Context {
} }
} }
// -- getters
pub fn ft_params(&self) -> Option<&FileTransferParams> {
self.ft_params.as_ref()
}
pub fn config(&self) -> &ConfigClient {
&self.config_client
}
pub fn config_mut(&mut self) -> &mut ConfigClient {
&mut self.config_client
}
pub(crate) fn input_hnd(&self) -> &InputHandler {
&self.input_hnd
}
pub(crate) fn store(&self) -> &Store {
&self.store
}
pub(crate) fn store_mut(&mut self) -> &mut Store {
&mut self.store
}
pub fn theme_provider(&self) -> &ThemeProvider {
&self.theme_provider
}
pub fn theme_provider_mut(&mut self) -> &mut ThemeProvider {
&mut self.theme_provider
}
pub fn terminal(&mut self) -> &mut TuiTerminal {
&mut self.terminal
}
// -- setter
pub fn set_ftparams(&mut self, params: FileTransferParams) {
self.ft_params = Some(params);
}
// -- error
/// ### set_error /// ### set_error
/// ///
/// Set context error /// Set context error