diff options
author | Canop <cano.petrole@gmail.com> | 2021-04-08 17:22:30 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-04-15 13:00:23 +0200 |
commit | 831c10d6c5ccf74d0109b6bc3ffd708a9da61cab (patch) | |
tree | 1435f23e6f19549e49f01e52f4cbfc702ec3a06b /src/app/selection.rs | |
parent | acade5051d04d14509b5526f48b1bb9de2ee4f70 (diff) |
staging area [WIP]
The staging area will be the way to operate on multiple files.
See issue #266
(it's far from ready)
Diffstat (limited to 'src/app/selection.rs')
-rw-r--r-- | src/app/selection.rs | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/src/app/selection.rs b/src/app/selection.rs index c84003e..98ad2b1 100644 --- a/src/app/selection.rs +++ b/src/app/selection.rs @@ -7,11 +7,13 @@ use { crate::{ errors::ProgramError, launchable::Launchable, + path::longest_common_ancestor, + stage::Stage, }, std::{ fs::OpenOptions, io::Write, - path::Path, + path::{Path, PathBuf}, }, }; @@ -26,12 +28,6 @@ pub enum SelectionType { Any, } -impl SelectionType { - pub fn respects(self, constraint: Self) -> bool { - constraint == Self::Any || self == constraint - } -} - /// light information about the currently selected /// file and maybe line number #[derive(Debug, Clone, Copy)] @@ -42,6 +38,35 @@ pub struct Selection<'s> { pub is_exe: bool, } +#[derive(Debug)] +pub enum SelInfo<'s> { + None, + One(Selection<'s>), + More(&'s Stage), // by contract the stage contains at least 2 paths +} + +impl SelectionType { + pub fn respects(self, constraint: Self) -> bool { + constraint == Self::Any || self == constraint + } + pub fn is_respected_by(self, sel_type: Option<Self>) -> bool { + match (self, sel_type) { + (Self::File, Some(Self::File)) => true, + (Self::Directory, Some(Self::Directory)) => true, + (Self::Any, _) => true, + _ => false, + } + } + pub fn from(path: &Path) -> Self { + if path.is_dir() { + Self::Directory + } else { + Self::File + } + } +} + + impl Selection<'_> { /// build a CmdResult with a launchable which will be used to @@ -69,5 +94,30 @@ impl Selection<'_> { CmdResult::from(Launchable::opener(self.path.to_path_buf())) }) } +} +impl SelInfo<'_> { + pub fn common_path(&self) -> Option<PathBuf> { + match self { + SelInfo::None => None, + SelInfo::One(sel) => Some(sel.path.into()), // TODO way to avoid this clone ? + SelInfo::More(stage) => Some(longest_common_ancestor(&stage.paths)) + } + } + pub fn common_stype(&self) -> Option<SelectionType> { + match self { + SelInfo::None => None, + SelInfo::One(sel) => Some(sel.stype), + SelInfo::More(stage) => { + let stype = SelectionType::from(&stage.paths[0]); + for path in stage.paths.iter().skip(1) { + if stype != SelectionType::from(path) { + return None; + } + } + Some(stype) + } + } + + } } |