use {
super::{
AppContext,
AppStateCmdResult,
},
crate::{
errors::ProgramError,
launchable::Launchable,
},
std::{
fs::OpenOptions,
io::Write,
path::Path,
},
};
pub type LineNumber = usize;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum SelectionType {
File,
Directory,
Any,
}
impl SelectionType {
pub fn respects(self, constraint: Self) -> bool {
constraint == Self::Any || self == constraint
}
}
#[derive(Debug, Clone, Copy)]
pub struct Selection<'s> {
pub path: &'s Path,
pub line: LineNumber, pub stype: SelectionType,
pub is_exe: bool,
}
impl Selection<'_> {
pub fn to_opener(
self,
con: &AppContext,
) -> Result<AppStateCmdResult, ProgramError> {
Ok(if self.is_exe {
let path = self.path.to_string_lossy().to_string();
if let Some(export_path) = &con.launch_args.cmd_export_path {
let f = OpenOptions::new().append(true).open(export_path)?;
writeln!(&f, "{}", path)?;
AppStateCmdResult::Quit
} else {
AppStateCmdResult::from(Launchable::program(
vec![path],
None, con,
)?)
}
} else {
AppStateCmdResult::from(Launchable::opener(self.path.to_path_buf()))
})
}
}