Skip to content

Commit

Permalink
Find file add dir as prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed May 29, 2022
1 parent 7c833c1 commit 62a1ee9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 12 additions & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl FindFilePicker {
Err(_) => Vec::new(),
};
let dir1 = dir.clone();
let picker = FilePicker::new(
let mut picker = FilePicker::new(
files,
move |path| {
let suffix = if path.is_dir() { "/" } else { "" };
Expand All @@ -101,6 +101,17 @@ impl FindFilePicker {
|_cx, _path, _action| {}, // we use custom callback_fn
|_editor, path| Some((path.clone(), None)),
);
// TODO: truncate prompt dir if prompt area too small and current dir too long
let current_dir = std::env::current_dir().expect("couldn't determine current directory");
let dir1 = dir.clone();
let prompt = dir1
.strip_prefix(current_dir)
.unwrap_or(&dir1)
.to_string_lossy()
.into_owned()
+ "/";
*picker.picker.prompt.prompt_mut() = Cow::Owned(prompt);
picker.picker.prompt.prompt_style_fn = Box::new(|theme| Some(theme.get("blue")));
FindFilePicker { picker, dir }
}
}
Expand Down
15 changes: 11 additions & 4 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use helix_core::{
unicode::segmentation::GraphemeCursor, unicode::width::UnicodeWidthStr, Position,
};
use helix_view::{
graphics::{CursorKind, Margin, Rect},
Editor,
graphics::{CursorKind, Margin, Rect, Style},
Editor, Theme,
};

pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
Expand All @@ -29,6 +29,7 @@ pub struct Prompt {
completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
pub doc_fn: Box<dyn Fn(&str) -> Option<Cow<str>>>,
pub prompt_style_fn: Box<dyn Fn(&Theme) -> Option<Style>>,
next_char_handler: Option<PromptCharHandler>,
}

Expand Down Expand Up @@ -80,6 +81,7 @@ impl Prompt {
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
doc_fn: Box::new(|_| None),
prompt_style_fn: Box::new(|_| None),
next_char_handler: None,
}
}
Expand Down Expand Up @@ -332,14 +334,19 @@ impl Prompt {
pub fn exit_selection(&mut self) {
self.selection = None;
}

pub fn prompt_mut(&mut self) -> &mut Cow<'static, str> {
&mut self.prompt
}
}

const BASE_WIDTH: u16 = 30;

impl Prompt {
pub fn render_prompt(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
let theme = &cx.editor.theme;
let prompt_color = theme.get("ui.text");
let prompt_input_color = theme.get("ui.text");
let prompt_color = (self.prompt_style_fn)(theme).unwrap_or(prompt_input_color);
let completion_color = theme.get("ui.menu");
let selected_color = theme.get("ui.menu.selected");
// completion
Expand Down Expand Up @@ -446,7 +453,7 @@ impl Prompt {
area.x + self.prompt.len() as u16,
area.y + line,
&self.line,
prompt_color,
prompt_input_color,
);
}
}
Expand Down

0 comments on commit 62a1ee9

Please sign in to comment.