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

add position-percentage as a statusline indicator #3168

Merged
merged 5 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -78,6 +78,7 @@ The following elements can be configured:
| `diagnostics` | The number of warnings and/or errors |
| `selections` | The number of active selections |
| `position` | The cursor position |
| `position-pct` | The cursor position as a percentage of the total number of lines |
sbromberger marked this conversation as resolved.
Show resolved Hide resolved

### `[editor.lsp]` Section

Expand Down
31 changes: 24 additions & 7 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use helix_core::{coords_at_pos, encoding};
use helix_core::{coords_at_pos, encoding, Position};
use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME},
graphics::Rect,
Expand Down Expand Up @@ -143,6 +143,7 @@ where
helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
helix_view::editor::StatusLineElement::Selections => render_selections,
helix_view::editor::StatusLineElement::Position => render_position,
helix_view::editor::StatusLineElement::PositionPct => render_position_pct,
}
}

Expand Down Expand Up @@ -250,26 +251,42 @@ where
);
}

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

fn render_position<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let position = get_position(context);
write(
context,
format!(" {}:{} ", position.row + 1, position.col + 1),
None,
);
}

fn render_position_pct<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let position = get_position(context);
let maxrows = context.doc.text().len_lines();
write(
context,
format!("{}%", (position.row + 1) * 100 / maxrows),
None,
);
}

fn render_file_encoding<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 @@ -246,6 +246,9 @@ pub enum StatusLineElement {

/// The cursor position
Position,

/// The cursor position as a percent of the total file
PositionPct,
}

// Cursor shape is read and used on every rendered frame and so needs
Expand Down