Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: running shell #24

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl Executor {
}
"create" => cx.manager.create(),
"rename" => cx.manager.rename(),
"shell" => cx.manager.shell(exec.named.contains_key("block")),
"hidden" => cx.manager.current_mut().hidden(match exec.args.get(0).map(|s| s.as_str()) {
Some("show") => Some(true),
Some("hide") => Some(false),
Expand Down
6 changes: 5 additions & 1 deletion config/docs/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

- open: Open the selected files.

- `--interactive`: Open the selected files with an interactive ui to choose the opening method.
- `--interactive`: Open the selected files with an interactive UI to choose the opening method.

- yank: Copy the selected files.

Expand All @@ -59,6 +59,10 @@

- create: Create a file or directory (ends with `/` for directory).
- rename: Rename a file or directory.
- shell: Run a shell command.

- `--block`: Block the UI until the command finishes.

- hidden: Set the visibility of hidden files.

- `show`: Show hidden files.
Expand Down
2 changes: 2 additions & 0 deletions config/preset/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ keymap = [
{ on = [ "D" ], exec = "remove --permanently" },
{ on = [ "a" ], exec = "create" },
{ on = [ "r" ], exec = "rename" },
{ on = [ ";" ], exec = "shell" },
{ on = [ ":" ], exec = "shell --block" },
{ on = [ "." ], exec = "hidden toggle" },
{ on = [ "s" ], exec = "search fd" },
{ on = [ "S" ], exec = "search rg" },
Expand Down
4 changes: 2 additions & 2 deletions core/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, path::PathBuf};
use std::{collections::BTreeMap, ffi::OsString, path::PathBuf};

use anyhow::Result;
use config::{keymap::{Control, KeymapLayer}, open::Opener};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub enum Event {
Input(InputOpt, oneshot::Sender<Result<String>>),

// Tasks
Open(Vec<(PathBuf, String)>, Option<Opener>),
Open(Vec<(OsString, String)>, Option<Opener>),
Progress(u8, u32),
}

Expand Down
37 changes: 31 additions & 6 deletions core/src/manager/manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, mem, path::PathBuf};

use anyhow::Error;
use config::OPEN;
use config::{open::Opener, OPEN};
use shared::MIME_DIR;
use tokio::fs;

Expand Down Expand Up @@ -126,7 +126,7 @@ impl Manager {
.into_iter()
.map(|f| {
(
f.path(),
f.path().into_os_string(),
if f.meta.is_dir() {
Some(MIME_DIR.to_owned())
} else {
Expand All @@ -146,7 +146,7 @@ impl Manager {
files = files
.into_iter()
.map(|(p, m)| {
let mime = m.or_else(|| mimes.remove(&p));
let mime = m.or_else(|| mimes.remove(&PathBuf::from(&p)));
(p, mime)
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -231,10 +231,30 @@ impl Manager {
false
}

fn bulk_rename(&self) -> bool { false }
pub fn bulk_rename(&self) -> bool { false }

pub fn selected(&self) -> Vec<&File> {
self.current().selected().or_else(|| self.hovered().map(|h| vec![h])).unwrap_or_default()
pub fn shell(&self, block: bool) -> bool {
tokio::spawn(async move {
let result = emit!(Input(InputOpt {
title: "Shell:".to_string(),
value: "".to_string(),
position: Position::Top,
}));

if let Ok(cmd) = result.await {
emit!(Open(
vec![(cmd.into(), "".to_string())],
Some(Opener {
cmd: "sh".to_string(),
args: vec!["-c".to_string(), "$0".to_string()],
block,
spread: false,
})
));
}
});

false
}

pub fn update_read(&mut self, op: FilesOp) -> bool {
Expand Down Expand Up @@ -352,4 +372,9 @@ impl Manager {

#[inline]
pub fn hovered(&self) -> Option<&File> { self.tabs.active().current.hovered.as_ref() }

#[inline]
pub fn selected(&self) -> Vec<&File> {
self.current().selected().or_else(|| self.hovered().map(|h| vec![h])).unwrap_or_default()
}
}
5 changes: 3 additions & 2 deletions core/src/tasks/workers/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ impl Process {
trace!("Failed to spawn {}: {e}", task.cmd);
}
}

emit!(Stop(false)).await;
return Ok(());

self.sch.send(TaskOp::Adv(task.id, 1, 0))?;
return self.done(task.id);
}

self.sch.send(TaskOp::New(task.id, 0))?;
Expand Down