-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from tmtmtoo/tweak
refactor: tweak
- Loading branch information
Showing
9 changed files
with
99 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,7 @@ | ||
use crate::exec::*; | ||
use crate::prelude::*; | ||
mod cmd_executor; | ||
mod cmd_not_found; | ||
mod wait; | ||
|
||
#[derive(new)] | ||
pub struct CmdExecutor { | ||
pub command: String, | ||
pub executor: Arc<dyn PipedCmdExecutor + Send + Sync>, | ||
} | ||
|
||
#[async_trait] | ||
impl super::Component for CmdExecutor { | ||
type Output = Result<Exit>; | ||
|
||
async fn handle(&self) -> Self::Output { | ||
let output = self.executor.piped_exec(self.command.as_str()).await?; | ||
Ok(output) | ||
} | ||
} | ||
|
||
#[derive(new)] | ||
pub struct PrintableCmdNotFound<C> { | ||
pub command: String, | ||
pub inner: C, | ||
} | ||
|
||
#[async_trait] | ||
impl<T: 'static, C: super::Component<Output = Result<T>> + Send + Sync> super::Component | ||
for PrintableCmdNotFound<C> | ||
{ | ||
type Output = Result<T>; | ||
|
||
async fn handle(&self) -> Self::Output { | ||
let result = self.inner.handle().await; | ||
|
||
match &result { | ||
Err(_) => { | ||
if self.command.is_empty() { | ||
eprintln!("cx: no command entered") | ||
} else { | ||
eprintln!( | ||
"cx: command not found '{}'", | ||
self.command | ||
.split(" ") | ||
.collect::<Vec<_>>() | ||
.get(0) | ||
.unwrap_or(&"") | ||
) | ||
} | ||
} | ||
_ => (), | ||
}; | ||
|
||
result | ||
} | ||
} | ||
|
||
pub struct WaitSec { | ||
pub sec: f64, | ||
} | ||
|
||
#[async_trait] | ||
impl super::Component for WaitSec { | ||
type Output = (); | ||
|
||
async fn handle(&self) -> Self::Output { | ||
tokio::time::sleep(tokio::time::Duration::from_secs_f64(self.sec)).await | ||
} | ||
} | ||
pub use cmd_executor::*; | ||
pub use cmd_not_found::*; | ||
pub use wait::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::app::*; | ||
use crate::exec::*; | ||
|
||
#[derive(new)] | ||
pub struct CmdExecutor { | ||
pub command: String, | ||
pub executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Component for CmdExecutor { | ||
type Output = anyhow::Result<Exit>; | ||
|
||
async fn handle(&self) -> Self::Output { | ||
let output = self.executor.piped_exec(self.command.as_str()).await?; | ||
Ok(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::app::*; | ||
|
||
#[derive(new)] | ||
pub struct PrintableCmdNotFound<C> { | ||
pub command: String, | ||
pub inner: C, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<T: 'static, C: Component<Output = anyhow::Result<T>> + Send + Sync> Component | ||
for PrintableCmdNotFound<C> | ||
{ | ||
type Output = anyhow::Result<T>; | ||
|
||
async fn handle(&self) -> Self::Output { | ||
let result = self.inner.handle().await; | ||
|
||
match &result { | ||
Err(_) => { | ||
if self.command.is_empty() { | ||
eprintln!("cx: no command entered") | ||
} else { | ||
eprintln!( | ||
"cx: command not found '{}'", | ||
self.command | ||
.split(" ") | ||
.collect::<Vec<_>>() | ||
.get(0) | ||
.unwrap_or(&"") | ||
) | ||
} | ||
} | ||
_ => (), | ||
}; | ||
|
||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::app::*; | ||
|
||
pub struct WaitSec { | ||
pub sec: f64, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Component for WaitSec { | ||
type Output = (); | ||
|
||
async fn handle(&self) -> Self::Output { | ||
tokio::time::sleep(tokio::time::Duration::from_secs_f64(self.sec)).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.