aboutsummaryrefslogtreecommitdiffstats
path: root/src/completion.rs
blob: 63ac8667a4703686d3265f1e6e39ced12d428bbc (plain) (blame)
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
use crate::*;
use std::fs;

pub struct Suggestion {
    pub display: BString,
    full: BString,
}

pub fn _path_completion(mut prefix: BString) -> io::Result<Vec<Suggestion>> {
    let mut partial_entry = BString::new();
    while let Some(c) = prefix.last().cloned() {
        if c == b'/' {
            break;
        }
        partial_entry.push(c);
        prefix.pop();
    }
    partial_entry.reverse();

    let mut sugs = Vec::new();

    for entry in fs::read_dir(OsStr::from_bytes(&prefix))? {
        let entry = entry?;
        let name = entry.file_name().as_bytes().to_vec();
        if name.starts_with(&partial_entry) {
            sugs.push(Suggestion {
                display: name,
                full: entry.path().as_os_str().as_bytes().to_vec(),
            });
        }
    }

    Ok(sugs)
}