wip: midi import/export browser, pt.4

This commit is contained in:
🪞👃🪞 2024-11-24 19:52:57 +01:00
parent a4febb8a0d
commit bd354bbc67
3 changed files with 47 additions and 14 deletions

View file

@ -266,18 +266,30 @@ impl<T: PhrasesControl> Command<T> for FileBrowserCommand {
fn execute (self, state: &mut T) -> Perhaps<Self> {
use FileBrowserCommand::*;
match state.phrases_mode_mut().clone() {
Some(PhrasesMode::Import(index, browser)) => {
todo!()
Some(PhrasesMode::Import(index, browser)) => match self {
Cancel => {
*state.phrases_mode_mut() = None;
},
Some(PhrasesMode::Export(index, browser)) => {
todo!()
Chdir(cwd) => {
*state.phrases_mode_mut() = Some(PhrasesMode::Import(
index, FileBrowser::new(Some(cwd))?
));
},
_ => match self {
Begin => {
todo!()
_ => todo!(),
_ => unreachable!()
},
Some(PhrasesMode::Export(index, browser)) => match self {
Cancel => {
*state.phrases_mode_mut() = None;
},
Chdir(cwd) => {
*state.phrases_mode_mut() = Some(PhrasesMode::Export(
index, FileBrowser::new(Some(cwd))?
));
},
_ => unreachable!()
}
},
_ => unreachable!(),
};
Ok(None)
}

View file

@ -187,7 +187,15 @@ impl<'a, T: PhrasesViewState> Content for PhrasesView<'a, T> {
impl Content for FileBrowser {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
"todo: file browser"
Stack::down(|add|{
for (_, name) in self.dirs.iter() {
add(&name.as_str())?;
}
for (_, name) in self.files.iter() {
add(&name.as_str())?;
}
Ok(())
})
}
}

View file

@ -172,8 +172,8 @@ pub enum PhrasesMode {
#[derive(Debug, Clone)]
pub struct FileBrowser {
pub cwd: PathBuf,
pub dirs: Vec<PathBuf>,
pub files: Vec<PathBuf>,
pub dirs: Vec<(OsString, String)>,
pub files: Vec<(OsString, String)>,
pub filter: String,
pub index: usize,
pub scroll: usize,
@ -183,10 +183,23 @@ pub struct FileBrowser {
impl FileBrowser {
pub fn new (cwd: Option<PathBuf>) -> Usually<Self> {
let cwd = if let Some(cwd) = cwd { cwd } else { std::env::current_dir()? };
let mut dirs = vec![];
let mut files = vec![];
for entry in std::fs::read_dir(&cwd)? {
let entry = entry?;
let name = entry.file_name();
let decoded = name.clone().into_string().unwrap_or_else(|_|"<unreadable>".to_string());
let meta = entry.metadata()?;
if meta.is_dir() {
dirs.push((name, format!("📁 {decoded}")));
} else if meta.is_file() {
files.push((name, format!("📄 {decoded}")));
}
}
Ok(Self {
cwd,
dirs: vec![/*todo*/],
files: vec![/*todo*/],
dirs,
files,
filter: "".to_string(),
index: 0,
scroll: 0,