Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split modification indicator from file statusline elements #4731

Merged
merged 12 commits into from
Feb 10, 2023
43 changes: 32 additions & 11 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,27 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface
append(&mut context.parts.right, text, &base_style, style)
};

let statusline = &context.editor.config().statusline;
let file_modified = &helix_view::editor::StatusLineElement::FileModificationIndicator;

let has_modified_element = statusline.left.contains(file_modified)
|| statusline.center.contains(file_modified)
|| statusline.right.contains(file_modified);

let default_file_modified_element = |element_ids: &Vec<StatusLineElementID>| {
let mut ret_val = element_ids.to_vec();
let file_name_index = element_ids
.iter()
.position(|id| id == &helix_view::editor::StatusLineElement::FileName);
if !has_modified_element && file_name_index != None {
ret_val.insert(file_name_index.unwrap() + 1, *file_modified);
}
return ret_val;
};

// Left side of the status line.
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved

let element_ids = &context.editor.config().statusline.left;
element_ids
default_file_modified_element(&statusline.left)
.iter()
.map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_left));
Expand All @@ -83,8 +100,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface

// Right side of the status line.

let element_ids = &context.editor.config().statusline.right;
element_ids
default_file_modified_element(&statusline.right)
.iter()
.map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_right));
Expand All @@ -101,8 +117,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface

// Center of the status line.

let element_ids = &context.editor.config().statusline.center;
element_ids
default_file_modified_element(&statusline.center)
.iter()
.map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_center));
Expand Down Expand Up @@ -137,6 +152,7 @@ where
helix_view::editor::StatusLineElement::Mode => render_mode,
helix_view::editor::StatusLineElement::Spinner => render_lsp_spinner,
helix_view::editor::StatusLineElement::FileName => render_file_name,
helix_view::editor::StatusLineElement::FileModificationIndicator => render_file_dirty,
helix_view::editor::StatusLineElement::FileEncoding => render_file_encoding,
helix_view::editor::StatusLineElement::FileLineEnding => render_file_line_ending,
helix_view::editor::StatusLineElement::FileType => render_file_type,
Expand Down Expand Up @@ -354,16 +370,21 @@ where
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(
" {}{} ",
path,
if context.doc.is_modified() { "[+]" } else { "" }
)
format!(" {} ", path)
};

write(context, title, None);
}

fn render_file_dirty<F>(context: &mut RenderContext, write: F)
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = format!(" {} ", if context.doc.is_modified() { "[+]" } else { "" });
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved

write(context, title, None);
}

fn render_separator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ pub enum StatusLineElement {
/// The file nane/path, including a dirty flag if it's unsaved
FileName,

// The file modification indicator
FileModificationIndicator,

/// The file encoding
FileEncoding,

Expand Down