use std::{
fmt::Display,
hash::{Hash, Hasher},
path::PathBuf,
};
use devicons::FileIcon;
use strum::EnumString;
#[derive(Clone, Debug, Eq)]
pub struct Entry {
pub name: String,
pub value: Option<String>,
pub name_match_ranges: Option<Vec<(u32, u32)>>,
pub value_match_ranges: Option<Vec<(u32, u32)>>,
pub icon: Option<FileIcon>,
pub line_number: Option<usize>,
pub preview_type: PreviewType,
}
impl Hash for Entry {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
if let Some(line_number) = self.line_number {
line_number.hash(state);
}
}
}
impl PartialEq<Entry> for &Entry {
fn eq(&self, other: &Entry) -> bool {
self.name == other.name
&& (self.line_number.is_none() && other.line_number.is_none()
|| self.line_number == other.line_number)
}
}
impl PartialEq<Entry> for Entry {
fn eq(&self, other: &Entry) -> bool {
self.name == other.name
&& (self.line_number.is_none() && other.line_number.is_none()
|| self.line_number == other.line_number)
}
}
#[allow(clippy::needless_return)]
pub fn merge_ranges(ranges: &[(u32, u32)]) -> Vec<(u32, u32)> {
ranges.iter().fold(
Vec::new(),
|mut acc: Vec<(u32, u32)>, x: &(u32, u32)| {
if let Some(last) = acc.last_mut() {
if last.1 == x.0 {
last.1 = x.1;
} else {
acc.push(*x);
}
} else {
acc.push(*x);
}
return acc;
},
)
}
impl Entry {
pub fn new(name: String, preview_type: PreviewType) -> Self {
Self {
name,
value: None,
name_match_ranges: None,
value_match_ranges: None,
icon: None,
line_number: None,
preview_type,
}
}
pub fn with_value(mut self, value: String) -> Self {
self.value = Some(value);
self
}
pub fn with_name_match_ranges(
mut self,
name_match_ranges: &[(u32, u32)],
) -> Self {
self.name_match_ranges = Some(merge_ranges(name_match_ranges));
self
}
pub fn with_value_match_ranges(
mut self,
value_match_ranges: &[(u32, u32)],
) -> Self {
self.value_match_ranges = Some(merge_ranges(value_match_ranges));
self
}
pub fn with_icon(mut self, icon: FileIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn with_line_number(mut self, line_number: usize) -> Self {
self.line_number = Some(line_number);
self
}
pub fn stdout_repr(&self) -> String {
let mut repr = self.name.clone();
if PathBuf::from(&repr).exists()
&& repr.contains(|c| char::is_ascii_whitespace(&c))
{
repr.insert(0, '\'');
repr.push('\'');
}
if let Some(line_number) = self.line_number {
repr.push_str(&format!(":{line_number}"));
}
repr
}
}
pub const ENTRY_PLACEHOLDER: Entry = Entry {
name: String::new(),
value: None,
name_match_ranges: None,
value_match_ranges: None,
icon: None,
line_number: None,
preview_type: PreviewType::EnvVar,
};
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
pub struct PreviewCommand {
pub command: String,
pub delimiter: String,
}
impl PreviewCommand {
pub fn new(command: &str, delimiter: &str) -> Self {
Self {
command: command.to_string(),
delimiter: delimiter.to_string(),
}
}
}
impl Display for PreviewCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum PreviewType {
#[default]
Basic,
EnvVar,
Files,
#[strum(disabled)]
Command(PreviewCommand),
None,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_input() {
let ranges: Vec<(u32, u32)> = vec![];
assert_eq!(merge_ranges(&ranges), Vec::<(u32, u32)>::new());
}
#[test]
fn test_single_range() {
let ranges = vec![(1, 3)];
assert_eq!(merge_ranges(&ranges), vec![(1, 3)]);
}
#[test]
fn test_contiguous_ranges() {
let ranges = vec![(1, 2), (2, 3), (3, 4), (4, 5)];
assert_eq!(merge_ranges(&ranges), vec![(1, 5)]);
}
#[test]
fn test_non_contiguous_ranges() {
let ranges = vec![(1, 2), (3, 4), (5, 6)];
assert_eq!(merge_ranges(&ranges), vec![(1, 2), (3, 4), (5, 6)]);
}
}