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

Dry up rendering code #5769

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use helix_view::editor::StatusLineElement as StatusLineElementID;
use tui::buffer::Buffer as Surface;
use tui::text::{Span, Spans};

use std::path::PathBuf;

pub struct RenderContext<'a> {
pub editor: &'a Editor,
pub doc: &'a Document,
Expand Down Expand Up @@ -411,20 +413,25 @@ where
write(context, format!(" {} ", file_type), None);
}

fn render_file_name<F>(context: &mut RenderContext, write: F)
fn render_path<F>(context: &mut RenderContext, write: F, path: Option<PathBuf>)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = {
let rel_path = context.doc.relative_path();
let path = rel_path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(" {} ", path)
};
let path = path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());

write(context, title, None);
let title = format!(" {} ", path);

write(context, title, None)
}

fn render_file_name<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
render_path(context, write, context.doc.relative_path())
}

fn render_file_modification_indicator<F>(context: &mut RenderContext, write: F)
Expand All @@ -445,16 +452,12 @@ fn render_file_base_name<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = {
let rel_path = context.doc.relative_path();
let path = rel_path
.as_ref()
.and_then(|p| p.as_path().file_name().map(|s| s.to_string_lossy()))
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(" {} ", path)
};
let path = context
.doc
.relative_path()
.and_then(|p| p.file_name().map(PathBuf::from));

write(context, title, None);
render_path(context, write, path)
}

fn render_separator<F>(context: &mut RenderContext, write: F)
Expand Down