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

Show code point in statusline #5860

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ The following statusline elements can be configured:
| `primary-selection-length` | The number of characters currently in primary selection |
| `position` | The cursor position |
| `position-percentage` | The cursor position as a percentage of the total number of lines |
| `code-point` | The code point under the cursor |
| `separator` | The string defined in `editor.statusline.separator` (defaults to `"│"`) |
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |

Expand Down
39 changes: 39 additions & 0 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ where
helix_view::editor::StatusLineElement::Position => render_position,
helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
helix_view::editor::StatusLineElement::TotalLineNumbers => render_total_line_numbers,
helix_view::editor::StatusLineElement::CodePoint => render_code_point,
helix_view::editor::StatusLineElement::Separator => render_separator,
helix_view::editor::StatusLineElement::Spacer => render_spacer,
}
Expand Down Expand Up @@ -447,6 +448,22 @@ where
write(context, title, None);
}

fn render_code_point<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let text = context.doc.text();
let cursor = context
.doc
.selection(context.view.id)
.primary()
.cursor(text.slice(..));

if let Some(c) = text.get_char(cursor) {
write(context, char_to_code_point(c), None)
}
}

fn render_separator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
Expand All @@ -466,3 +483,25 @@ where
{
write(context, String::from(" "), None);
}

fn char_to_code_point(c: char) -> String {
// Uses "U+" notation as described here:
// https://en.wikipedia.org/wiki/Unicode#Architecture_and_terminology
format!("U+{:04X}", c as u32)
}

#[cfg(test)]
mod tests {
use super::char_to_code_point;

#[test]
fn char_to_code_point_examples() {
assert_eq!("U+0000", &char_to_code_point('\u{0}'));
assert_eq!("U+000F", &char_to_code_point('\u{F}'));
assert_eq!("U+00FF", &char_to_code_point('\u{FF}'));
assert_eq!("U+0FFF", &char_to_code_point('\u{FFF}'));
assert_eq!("U+FFFF", &char_to_code_point('\u{FFFF}'));
assert_eq!("U+FFFFF", &char_to_code_point('\u{FFFFF}'));
assert_eq!("U+10FFFF", &char_to_code_point('\u{10FFFF}'));
}
}
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ pub enum StatusLineElement {
/// The total line numbers of the current file
TotalLineNumbers,

/// The code point under the cursor
CodePoint,

/// A single space
Spacer,
}
Expand Down