Excised CommandOutput type
This commit is contained in:
1 parent
26e87c9d8a
commit
ead112cdae
4 files changed
+29
-48
No files matched your search
+7
-22
@@ -26,7 +26,7 @@ use crate::{
|
||||
|
||||
use self::{
|
||||
buffer::{Buffer, BufferId, TextBuffer},
|
||||
commands::{CommandOutput, Commands},
|
||||
commands::Commands,
|
||||
error::{log_only, BasicError},
|
||||
pane_layout::PaneLayout,
|
||||
};
|
||||
@@ -60,7 +60,7 @@ pub(crate) struct EditorCore {
|
||||
mode: Mode,
|
||||
configuration: Configuration,
|
||||
commands: Commands,
|
||||
command_output_rx: UnboundedReceiver<CommandOutput>,
|
||||
command_output_rx: UnboundedReceiver<EditorAction>,
|
||||
pub(crate) should_quit: bool,
|
||||
}
|
||||
|
||||
@@ -145,26 +145,12 @@ impl EditorCore {
|
||||
|
||||
//TODO this needs to only emit EditorAction's , it can't directly modify anything
|
||||
pub(crate) fn process_commands(&mut self) {
|
||||
if let Ok(command) = self.command_output_rx.try_recv() {
|
||||
info!(?command, "Handling command");
|
||||
match command {
|
||||
CommandOutput::OpenFile(file) => log_only(self.load_file(&file)),
|
||||
CommandOutput::Quit => {
|
||||
self.should_quit = true;
|
||||
}
|
||||
CommandOutput::Error(err) => {
|
||||
error!(err, "Command Output Error");
|
||||
}
|
||||
CommandOutput::WriteBuffer(path) => log_only(self.write_current_buffer(path)),
|
||||
CommandOutput::ShowHelp(toggle) => {
|
||||
info!("showing help");
|
||||
self.handle_action(EditorAction::PopupHelp(toggle));
|
||||
}
|
||||
CommandOutput::ToggleFinder(toggle) => {
|
||||
self.handle_action(EditorAction::ToggleFinder(toggle));
|
||||
}
|
||||
};
|
||||
if let Ok(action) = self.command_output_rx.try_recv() {
|
||||
info!(?action, "Handling action from command output");
|
||||
self.handle_action(action);
|
||||
|
||||
//TODO this should probably be handled by the command API itself, since this will break
|
||||
//when teh command api starts emitting a vec of length greater than 1
|
||||
self.handle_action(EditorAction::TransitionToMode(Mode::Normal));
|
||||
}
|
||||
}
|
||||
@@ -254,7 +240,6 @@ impl EditorCore {
|
||||
Error(err) => {
|
||||
error!(err, "Command Output Error");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ pub(crate) enum EditorAction {
|
||||
/// Quit the editor
|
||||
Quit,
|
||||
|
||||
|
||||
// TODO: ported over from old CommandOutput struct - clean up later
|
||||
OpenFile(PathBuf),
|
||||
WriteBuffer(Option<PathBuf>),
|
||||
|
||||
+8
-14
@@ -1,21 +1,13 @@
|
||||
mod api;
|
||||
|
||||
use std::{collections::VecDeque, path::PathBuf};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use tracing::{error, info};
|
||||
|
||||
use self::api::CommandAPI;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum CommandOutput {
|
||||
OpenFile(PathBuf),
|
||||
WriteBuffer(Option<PathBuf>),
|
||||
Quit,
|
||||
Error(String),
|
||||
ShowHelp(bool),
|
||||
ToggleFinder(bool),
|
||||
}
|
||||
use super::EditorAction;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Commands {
|
||||
@@ -23,11 +15,11 @@ pub(crate) struct Commands {
|
||||
pub(crate) command_api: CommandAPI,
|
||||
command_history: VecDeque<String>,
|
||||
max_history: usize,
|
||||
command_tx: UnboundedSender<CommandOutput>,
|
||||
command_tx: UnboundedSender<EditorAction>,
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
pub(crate) fn new(command_tx: UnboundedSender<CommandOutput>) -> Self {
|
||||
pub(crate) fn new(command_tx: UnboundedSender<EditorAction>) -> Self {
|
||||
Self {
|
||||
current_command: None,
|
||||
command_api: CommandAPI::new(),
|
||||
@@ -70,8 +62,10 @@ impl Commands {
|
||||
self.command_history.pop_front();
|
||||
}
|
||||
|
||||
if let Some(output) = self.command_api.execute(current) {
|
||||
if let Err(err) = self.command_tx.send(output) {
|
||||
//TODO - maybe execute() should have some kind of explicit error output? not sure
|
||||
//if let Some(output) = self.command_api.execute(current) {
|
||||
for action in self.command_api.execute(current) {
|
||||
if let Err(err) = self.command_tx.send(action) {
|
||||
error!("Error sending command: {:?}", err.0);
|
||||
}
|
||||
}
|
||||
|
||||
+14
-11
@@ -1,6 +1,8 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::CommandOutput;
|
||||
use tracing::info;
|
||||
|
||||
use crate::core::EditorAction;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct CommandAPI {}
|
||||
@@ -9,7 +11,7 @@ impl CommandAPI {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
pub(crate) fn execute(&mut self, command_str: String) -> Option<CommandOutput> {
|
||||
pub(crate) fn execute(&mut self, command_str: String) -> Vec<EditorAction> {
|
||||
let mut commands = command_str.split_whitespace();
|
||||
let operation = match commands.next() {
|
||||
Some(s) if s == "open" => Command::Open,
|
||||
@@ -17,35 +19,36 @@ impl CommandAPI {
|
||||
Some(s) if s == "write" || s == "w" => Command::Write,
|
||||
Some(s) if s == "help" || s == "h" => Command::Help,
|
||||
Some(s) if s == "finder" || s == "h" => Command::Finder,
|
||||
Some(other) => return Some(CommandOutput::Error(format!("Invalid command: {other}"))),
|
||||
None => return None,
|
||||
Some(other) => return vec![EditorAction::Error(format!("Invalid command: {other}"))],
|
||||
None => return vec![],
|
||||
};
|
||||
|
||||
match operation {
|
||||
Command::Write => {
|
||||
let path = commands.next().map(PathBuf::from);
|
||||
Some(CommandOutput::WriteBuffer(path))
|
||||
vec![EditorAction::WriteBuffer(path)]
|
||||
}
|
||||
Command::Open => {
|
||||
let path = match commands.next() {
|
||||
Some(p) => PathBuf::from(p),
|
||||
None => return Some(CommandOutput::Error("open: no path given".to_string())),
|
||||
None => return vec![EditorAction::Error("open: no path given".to_string())],
|
||||
};
|
||||
|
||||
Some(CommandOutput::OpenFile(path))
|
||||
vec![EditorAction::OpenFile(path)]
|
||||
}
|
||||
Command::Quit => Some(CommandOutput::Quit),
|
||||
Command::Quit => vec![EditorAction::Quit],
|
||||
Command::Help => {
|
||||
let toggle = commands.next();
|
||||
let toggle = match toggle {
|
||||
Some("on") => true,
|
||||
None => true,
|
||||
Some("off") => false,
|
||||
_ => return None,
|
||||
_ => return vec![],
|
||||
};
|
||||
Some(CommandOutput::ShowHelp(toggle))
|
||||
info!("showing help");
|
||||
vec![EditorAction::PopupHelp(toggle)]
|
||||
}
|
||||
Command::Finder => Some(CommandOutput::ToggleFinder(true)),
|
||||
Command::Finder => vec![EditorAction::ToggleFinder(true)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user