feat: choose-file mode (#69)

This commit is contained in:
Yifan Song 2023-08-18 01:04:40 +08:00 committed by GitHub
parent 1135c13d0f
commit b70f83ced7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 9 deletions

View file

@ -1,5 +1,5 @@
use core::{emit, files::FilesOp, input::InputMode, Event};
use std::os::unix::prelude::OsStrExt;
use std::{ffi::OsString, os::unix::prelude::OsStrExt};
use anyhow::{Ok, Result};
use config::{keymap::{Control, Key, KeymapLayer}, BOOT};
@ -167,6 +167,17 @@ impl App {
}
Event::Open(targets, opener) => {
if let Some(p) = &BOOT.chooser_file {
let paths = targets.into_iter().fold(OsString::new(), |mut s, (p, _)| {
s.push(p);
s.push("\n");
s
});
std::fs::write(p, paths.as_bytes()).ok();
return emit!(Quit);
}
if let Some(opener) = opener {
tasks.file_open_with(&opener, &targets.into_iter().map(|(f, _)| f).collect::<Vec<_>>());
} else {

View file

@ -2,32 +2,51 @@ use std::{env, fs, os::unix::prelude::OsStrExt, path::{Path, PathBuf}, time::{se
use clap::{command, Parser};
use md5::{Digest, Md5};
use shared::absolute_path;
#[derive(Debug)]
pub struct Boot {
pub cwd: PathBuf,
pub cwd_file: Option<PathBuf>,
pub cache_dir: PathBuf,
pub state_dir: PathBuf,
pub cwd_file: Option<PathBuf>,
pub chooser_file: Option<PathBuf>,
}
#[derive(Debug, Parser)]
#[command(name = "yazi")]
#[command(version = "0.1.3")]
struct Args {
/// Set the current working directory
#[arg(long, short)]
cwd: Option<PathBuf>,
/// Write the cwd on exit to this file
#[arg(long)]
cwd_file: Option<PathBuf>,
cwd_file: Option<PathBuf>,
/// Write the selected files on open emitted by the chooser mode
#[arg(long)]
chooser_file: Option<PathBuf>,
}
impl Default for Boot {
fn default() -> Self {
let args = Args::parse();
let cwd = args
.cwd
.map(|p| futures::executor::block_on(absolute_path(p)))
.and_then(|p| p.is_dir().then_some(p))
.or_else(|| env::current_dir().ok());
let boot = Self {
cwd: env::current_dir().unwrap_or("/".into()),
cwd_file: args.cwd_file,
cwd: cwd.unwrap_or("/".into()),
cache_dir: env::temp_dir().join("yazi"),
state_dir: xdg::BaseDirectories::with_prefix("yazi").unwrap().get_state_home(),
cwd_file: args.cwd_file,
chooser_file: args.chooser_file,
};
if !boot.cache_dir.is_dir() {

View file

@ -28,4 +28,3 @@ pub async fn clipboard_set(s: &str) -> Result<()> {
bail!("failed to set clipboard")
}

View file

@ -1,8 +1,7 @@
use anyhow::{anyhow, Result};
use shared::tty_size;
use tokio::sync::oneshot::Sender;
use super::{SelectOpt, SELECT_PADDING};
use super::SelectOpt;
use crate::Position;
#[derive(Default)]

View file

@ -1,4 +1,3 @@
mod which;
pub use which::*;