aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/mod.rs
blob: f9480dd4a645cebc85db4ca86020b4b1c0d84c9b (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
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
mod editor;
mod picker;
mod prompt;

pub use editor::EditorView;
pub use picker::Picker;
pub use prompt::{Prompt, PromptEvent};

pub use tui::layout::Rect;
pub use tui::style::{Color, Modifier, Style};

// TODO: temp
#[inline(always)]
pub fn text_color() -> Style {
    Style::default().fg(Color::Rgb(219, 191, 239)) // lilac
}

use std::path::{Path, PathBuf};
pub fn file_picker(root: &str) -> Picker<PathBuf> {
    use ignore::Walk;
    // TODO: determine root based on git root
    let files = Walk::new(root).filter_map(|entry| match entry {
        Ok(entry) => {
            // filter dirs, but we might need special handling for symlinks!
            if !entry.file_type().unwrap().is_dir() {
                Some(entry.into_path())
            } else {
                None
            }
        }
        Err(_err) => None,
    });

    const MAX: usize = 1024;

    use helix_view::Editor;
    Picker::new(
        files.take(MAX).collect(),
        |path: &PathBuf| {
            // format_fn
            path.strip_prefix("./").unwrap().to_str().unwrap()
        },
        |editor: &mut Editor, path: &PathBuf| {
            let size = editor.view().unwrap().size;
            editor.open(path.into(), size);
        },
    )
}

use helix_view::View;
pub fn buffer_picker(views: &[View], current: usize) -> Picker<(Option<PathBuf>, usize)> {
    use helix_view::Editor;
    Picker::new(
        views
            .iter()
            .enumerate()
            .map(|(i, view)| (view.doc.relative_path().map(Path::to_path_buf), i))
            .collect(),
        |(path, index): &(Option<PathBuf>, usize)| {
            // format_fn
            match path {
                Some(path) => path.to_str().unwrap(),
                None => "[NEW]",
            }
        },
        |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| {
            if index < editor.views.len() {
                editor.focus = index;
            }
        },
    )
}