Skip to content

Commit

Permalink
feat: refactor modules location
Browse files Browse the repository at this point in the history
  • Loading branch information
rvigo committed Jun 8, 2024
1 parent 1d04e6a commit 7820edc
Show file tree
Hide file tree
Showing 57 changed files with 1,106 additions and 1,289 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benches/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ mod core {
let edited_command = build_command!(alias => "updated";);

c.bench_function("update command (alias)", |b: &mut Bencher| {
b.iter(|| commands.add_edited(&edited_command, &command));
b.iter(|| commands.edit(&edited_command, &command));
});

let edited_command = build_command!(namespace => "updated";);

c.bench_function("update command (namespace)", |b: &mut Bencher| {
b.iter(|| commands.add_edited(&edited_command, &command));
b.iter(|| commands.edit(&edited_command, &command));
});
}
}
Expand Down
14 changes: 5 additions & 9 deletions cl-core/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ impl Commands {
Ok(self.commands.to_owned())
}

pub fn add_edited(
&mut self,
new_command: &Command,
old_command: &Command,
) -> Result<CommandMap> {
pub fn edit(&mut self, new_command: &Command, old_command: &Command) -> Result<CommandMap> {
self.compare_edited_command(new_command, old_command)?;

if let Some(commands) = self.commands.get_mut(&old_command.namespace) {
Expand Down Expand Up @@ -291,7 +287,7 @@ mod test {
edited_command.alias = String::from("new");

let old_command = new_command;
let commands_with_edited_command = commands.add_edited(&edited_command, &old_command);
let commands_with_edited_command = commands.edit(&edited_command, &old_command);

assert!(commands_with_edited_command.is_ok());
if let Ok(command_map) = commands_with_edited_command {
Expand All @@ -313,7 +309,7 @@ mod test {
edited_command.namespace = String::from("edited_namespace");

let old_command = new_command;
let commands_with_edited_command = commands.add_edited(&edited_command, &old_command);
let commands_with_edited_command = commands.edit(&edited_command, &old_command);

assert!(commands_with_edited_command.is_ok());
if let Ok(command_map) = commands_with_edited_command {
Expand All @@ -336,7 +332,7 @@ mod test {
let mut edited_command = command2.clone();
edited_command.alias = String::from("alias1");

let command_list_with_edited_command = commands.add_edited(&edited_command, &command2);
let command_list_with_edited_command = commands.edit(&edited_command, &command2);

assert!(command_list_with_edited_command.is_err());
assert_eq!(
Expand All @@ -358,7 +354,7 @@ mod test {
let mut edited_command = command2.clone();
edited_command.alias = String::from("alias1");
edited_command.namespace = String::from("namespace1");
let command_list_with_edited_command = commands.add_edited(&edited_command, &command2);
let command_list_with_edited_command = commands.edit(&edited_command, &command2);

assert!(command_list_with_edited_command.is_err());
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions cl-gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parking_lot = "0.12.1"
thiserror = "1.0.47"
tui = { package = "ratatui", version = "0.24.0" }
tui-textarea = "0.4.0"
unicode-width = "0.1"

[dev-dependencies]
tempdir.workspace = true
File renamed without changes.
98 changes: 98 additions & 0 deletions cl-gui/src/context/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use super::{
namespace_context::{NamespaceContext, DEFAULT_NAMESPACE},
CommandsContext, FieldMap,
};
use crate::Clipboard;
use anyhow::Result;
use cl_core::{resource::FileService, Command, CommandVec, Commands, Preferences};

pub struct Application {
pub commands: CommandsContext,
pub namespaces: NamespaceContext,
preferences: Preferences,
clipboard: Option<Clipboard>,
}

impl Application {
pub fn init(
commands: Commands,
file_service: FileService,
preferences: Preferences,
) -> Application {
let clipboard = Clipboard::new().ok();
let namespaces = commands
.to_list()
.iter()
.map(|c| c.namespace.to_owned())
.collect::<Vec<_>>();

Application {
commands: CommandsContext::new(commands, file_service),
namespaces: NamespaceContext::new(namespaces),
preferences,
clipboard,
}
}

pub fn copy_to_clipboard<T>(&mut self, content: T) -> Result<()>
where
T: Into<String>,
{
if let Some(ref mut clipboard) = &mut self.clipboard {
clipboard.set_content(content.into())?;
}
Ok(())
}

/// Reloads the command context, filtering all commands and reseting the select command idx
pub fn reload(&mut self) {
self.commands.filter_commands(DEFAULT_NAMESPACE, "");
self.commands.reset_selected_command_idx();
}

// other
pub fn should_highlight(&mut self) -> bool {
self.preferences.highlight()
}

/// Filters the command list using the querybox input as query
pub fn filter_commands(&mut self, query_string: &str) -> CommandVec {
let current_namespace = self.namespaces.current();
self.commands
.filter_commands(&current_namespace, query_string)
}
}

// Callback
impl Application {
/// Executes the callback command
pub fn execute_callback(&self) -> Result<()> {
self.commands.execute(self.preferences.quiet_mode())
}
}

impl Application {
pub fn add_command(&mut self, command_value_map: FieldMap) -> Result<()> {
let commands = self.commands.add(&command_value_map.into())?;
self.namespaces.update(&commands.keys().collect::<Vec<_>>());
Ok(())
}

pub fn edit_command(
&mut self,
edited_command: FieldMap,
current_command: &Command,
) -> Result<()> {
let commands = self
.commands
.edit(&edited_command.into(), current_command)?;
self.namespaces.update(&commands.keys().collect::<Vec<_>>());
Ok(())
}

pub fn remove_command(&mut self, command: &Command) -> Result<()> {
let commands = self.commands.remove(command)?;
self.namespaces.update(&commands.keys().collect::<Vec<_>>());
Ok(())
}
}
Loading

0 comments on commit 7820edc

Please sign in to comment.