Skip to content

Commit

Permalink
Add a status line element that shows just the basename of the file (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
theteachr authored and semin-park committed Jan 4, 2023
1 parent 0fe59c7 commit fc1dd02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ The following statusline elements can be configured:
| `mode` | The current editor mode (`mode.normal`/`mode.insert`/`mode.select`) |
| `spinner` | A progress spinner indicating LSP activity |
| `file-name` | The path/name of the opened file |
| `file-base-name` | The basename of the opened file |
| `file-encoding` | The encoding of the opened file if it differs from UTF-8 |
| `file-line-ending` | The file line endings (CRLF or LF) |
| `total-line-numbers` | The total line numbers of the opened file |
Expand Down
21 changes: 21 additions & 0 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ where
match element_id {
helix_view::editor::StatusLineElement::Mode => render_mode,
helix_view::editor::StatusLineElement::Spinner => render_lsp_spinner,
helix_view::editor::StatusLineElement::FileBaseName => render_file_base_name,
helix_view::editor::StatusLineElement::FileName => render_file_name,
helix_view::editor::StatusLineElement::FileEncoding => render_file_encoding,
helix_view::editor::StatusLineElement::FileLineEnding => render_file_line_ending,
Expand Down Expand Up @@ -426,6 +427,26 @@ where
write(context, title, None);
}

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,
if context.doc.is_modified() { "[+]" } else { "" }
)
};

write(context, title, None);
}

fn render_separator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
Expand Down
5 changes: 4 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ pub enum StatusLineElement {
/// The LSP activity spinner
Spinner,

/// The file nane/path, including a dirty flag if it's unsaved
/// The base file name, including a dirty flag if it's unsaved
FileBaseName,

/// The relative file path, including a dirty flag if it's unsaved
FileName,

/// The file encoding
Expand Down

0 comments on commit fc1dd02

Please sign in to comment.