1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::collections::BTreeSet;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};

use log::info;

use crate::constant_strings_paths::MARKS_FILEPATH;
use crate::fm_error::{FmError, FmResult};
use crate::impl_selectable_content;
use crate::utils::read_lines;

/// Holds the marks created by the user.
/// It's an ordered map between any char (except :) and a PathBuf.
#[derive(Clone)]
pub struct Marks {
    save_path: PathBuf,
    content: Vec<(char, PathBuf)>,
    /// The currently selected shortcut
    pub index: usize,
    used_chars: BTreeSet<char>,
}

impl Marks {
    /// True if there's no marks yet
    pub fn is_empty(&self) -> bool {
        self.content.is_empty()
    }

    /// The number of saved marks
    pub fn len(&self) -> usize {
        self.content.len()
    }

    /// Reads the marks stored in the config file (~/.config/fm/marks.cfg).
    /// If an invalid marks is read, only the valid ones are kept
    /// and the file is saved again.
    pub fn read_from_config_file() -> Self {
        let path = PathBuf::from(shellexpand::tilde(&MARKS_FILEPATH).to_string());
        Self::read_from_file(path)
    }

    fn read_from_file(save_path: PathBuf) -> Self {
        let mut content = vec![];
        let mut must_save = false;
        let mut used_chars = BTreeSet::new();
        if let Ok(lines) = read_lines(&save_path) {
            for line in lines {
                if let Ok((ch, path)) = Self::parse_line(line) {
                    if !used_chars.contains(&ch) {
                        content.push((ch, path));
                        used_chars.insert(ch);
                    }
                } else {
                    must_save = true;
                }
            }
        }
        let marks = Self {
            save_path,
            content,
            index: 0,
            used_chars,
        };
        if must_save {
            info!("Wrong marks found, will save it again");
            let _ = marks.save_marks();
        }
        marks
    }

    /// Returns an optional marks associated to a char bind.
    pub fn get(&self, key: char) -> Option<PathBuf> {
        for (ch, dest) in self.content.iter() {
            if &key == ch {
                return Some(dest.clone());
            }
        }
        None
    }

    fn parse_line(line: Result<String, io::Error>) -> FmResult<(char, PathBuf)> {
        let line = line?;
        let sp: Vec<&str> = line.split(':').collect();
        if sp.len() <= 1 {
            return Err(FmError::custom(
                "marks: parse_line",
                &format!("Invalid mark line: {line}"),
            ));
        }
        if let Some(ch) = sp[0].chars().next() {
            let path = PathBuf::from(sp[1]);
            Ok((ch, path))
        } else {
            Err(FmError::custom(
                "marks: parse line",
                &format!("Invalid first character in: {line}"),
            ))
        }
    }

    /// Store a new mark in the config file.
    /// If an update is done, the marks are saved again.
    pub fn new_mark(&mut self, ch: char, path: PathBuf) -> FmResult<()> {
        if ch == ':' {
            return Err(FmError::custom("new_mark", "':' can't be used as a mark"));
        }
        if self.used_chars.contains(&ch) {
            let mut found_index = None;
            for (index, (k, _)) in self.content.iter().enumerate() {
                if *k == ch {
                    found_index = Some(index);
                    break;
                }
            }
            let Some(found_index) = found_index else {return Ok(())};
            self.content[found_index] = (ch, path);
        } else {
            self.content.push((ch, path))
        }

        self.save_marks()
    }

    fn save_marks(&self) -> FmResult<()> {
        let file = std::fs::File::create(&self.save_path)?;
        let mut buf = BufWriter::new(file);
        for (ch, path) in self.content.iter() {
            writeln!(buf, "{}:{}", ch, Self::path_as_string(path)?)?;
        }
        Ok(())
    }

    fn path_as_string(path: &Path) -> FmResult<String> {
        Ok(path
            .to_str()
            .ok_or_else(|| FmError::custom("path_as_string", "Unreadable path"))?
            .to_owned())
    }

    /// Returns a vector of strings like "d: /dev" for every mark.
    pub fn as_strings(&self) -> Vec<String> {
        self.content
            .iter()
            .map(|(ch, path)| Self::format_mark(ch, path))
            .collect()
    }

    fn format_mark(ch: &char, path: &Path) -> String {
        format!("{}    {}", ch, path.to_string_lossy())
    }
}

type Pair = (char, PathBuf);
impl_selectable_content!(Pair, Marks);