sl_liner/
complete.rs

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use super::event::Event;
use std::path::PathBuf;

pub trait Completer {
    fn completions(&mut self, start: &str) -> Vec<String>;
    fn on_event(&mut self, _event: Event) {}
}

/// Completer with no completions
pub struct EmptyCompleter {
    empty: Vec<String>,
}

impl EmptyCompleter {
    pub fn new() -> EmptyCompleter {
        EmptyCompleter {
            empty: Vec::with_capacity(0),
        }
    }
}

impl Default for EmptyCompleter {
    fn default() -> Self {
        Self::new()
    }
}

impl Completer for EmptyCompleter {
    fn completions(&mut self, _start: &str) -> Vec<String> {
        self.empty.clone()
    }
}

/// Completer that can be seeded with a list of prefixes..
pub struct BasicCompleter {
    prefixes: Vec<String>,
}

impl BasicCompleter {
    pub fn new<T: Into<String>>(prefixes: Vec<T>) -> BasicCompleter {
        BasicCompleter {
            prefixes: prefixes.into_iter().map(|s| s.into()).collect(),
        }
    }
}

impl Completer for BasicCompleter {
    fn completions(&mut self, start: &str) -> Vec<String> {
        self.prefixes
            .iter()
            .filter(|s| s.starts_with(start))
            .cloned()
            .collect()
    }
}

/// Completer for filenames in the current working_dir
pub struct FilenameCompleter {
    working_dir: Option<PathBuf>,
    case_sensitive: bool,
}

impl FilenameCompleter {
    pub fn new<T: Into<PathBuf>>(working_dir: Option<T>) -> Self {
        FilenameCompleter {
            working_dir: working_dir.map(|p| p.into()),
            case_sensitive: true,
        }
    }

    pub fn with_case_sensitivity<T: Into<PathBuf>>(
        working_dir: Option<T>,
        case_sensitive: bool,
    ) -> Self {
        FilenameCompleter {
            working_dir: working_dir.map(|p| p.into()),
            case_sensitive,
        }
    }
}

impl Completer for FilenameCompleter {
    fn completions(&mut self, mut start: &str) -> Vec<String> {
        // XXX: this function is really bad, TODO rewrite

        let start_owned: String = if start.starts_with('\"') || start.starts_with('\'') {
            start = &start[1..];
            if !start.is_empty() {
                start = &start[..start.len() - 1];
            }
            start.into()
        } else {
            start.replace(r"\ ", " ")
        };

        let start_path = PathBuf::from(start_owned.as_str());

        let full_path = match self.working_dir {
            Some(ref wd) => {
                let mut fp = PathBuf::from(wd);
                fp.push(start_owned.as_str());
                fp
            }
            None => PathBuf::from(start_owned.as_str()),
        };

        let p;
        let mut start_name = None;
        let completing_dir;
        match full_path.parent() {
            // XXX non-unix separator
            Some(parent)
                if !start.is_empty()
                    && !start_owned.ends_with('/')
                    && !full_path.ends_with("..") =>
            {
                p = parent;
                if let Some(file_name) = full_path.file_name() {
                    let sn = file_name.to_string_lossy();
                    start_name = {
                        if !self.case_sensitive {
                            let _ = sn.to_lowercase();
                        };
                        Some(sn)
                    }
                }
                completing_dir = false;
            }
            _ => {
                p = full_path.as_path();
                start_name = Some("".into());
                completing_dir =
                    start.is_empty() || start.ends_with('/') || full_path.ends_with("..");
            }
        }

        let read_dir = match p.read_dir() {
            Ok(x) => x,
            Err(_) => return vec![],
        };

        let mut matches = vec![];
        for dir in read_dir {
            let dir = match dir {
                Ok(x) => x,
                Err(_) => continue,
            };
            let file_name = dir.file_name();
            let file_name = if self.case_sensitive {
                file_name.to_string_lossy().to_string()
            } else {
                file_name.to_string_lossy().to_lowercase()
            };

            if let Some(start_name) = &start_name {
                if file_name.starts_with(&**start_name) {
                    let mut a = start_path.clone();
                    if !a.is_absolute() {
                        a = PathBuf::new();
                    } else if !completing_dir && !a.pop() {
                        return vec![];
                    }

                    a.push(dir.file_name());
                    let mut s = a.to_string_lossy();
                    if dir.path().is_dir() {
                        let mut string = s.into_owned();
                        string.push('/');
                        s = string.into();
                    }

                    let mut b = PathBuf::from(&start_owned);
                    if !completing_dir {
                        b.pop();
                    }
                    b.push(s.as_ref());

                    matches.push(b.to_string_lossy().replace(' ', r"\ "));
                }
            }
        }

        matches
    }
}