Skip to content

Commit 3dd2196

Browse files
authored
add position-percentage as a statusline indicator (#3168)
* added position-pct as a statusline indicator * removed unnecessary mutable reference * pct -> percent * percent -> percentage
1 parent de8ade8 commit 3dd2196

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

book/src/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The following elements can be configured:
7878
| `diagnostics` | The number of warnings and/or errors |
7979
| `selections` | The number of active selections |
8080
| `position` | The cursor position |
81+
| `position-percentage` | The cursor position as a percentage of the total number of lines |
8182
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
8283

8384
### `[editor.lsp]` Section

helix-term/src/ui/statusline.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use helix_core::{coords_at_pos, encoding};
1+
use helix_core::{coords_at_pos, encoding, Position};
22
use helix_view::{
33
document::{Mode, SCRATCH_BUFFER_NAME},
44
graphics::Rect,
@@ -143,6 +143,7 @@ where
143143
helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
144144
helix_view::editor::StatusLineElement::Selections => render_selections,
145145
helix_view::editor::StatusLineElement::Position => render_position,
146+
helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
146147
helix_view::editor::StatusLineElement::Spacer => render_spacer,
147148
}
148149
}
@@ -251,26 +252,42 @@ where
251252
);
252253
}
253254

254-
fn render_position<F>(context: &mut RenderContext, write: F)
255-
where
256-
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
257-
{
258-
let position = coords_at_pos(
255+
fn get_position(context: &RenderContext) -> Position {
256+
coords_at_pos(
259257
context.doc.text().slice(..),
260258
context
261259
.doc
262260
.selection(context.view.id)
263261
.primary()
264262
.cursor(context.doc.text().slice(..)),
265-
);
263+
)
264+
}
266265

266+
fn render_position<F>(context: &mut RenderContext, write: F)
267+
where
268+
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
269+
{
270+
let position = get_position(context);
267271
write(
268272
context,
269273
format!(" {}:{} ", position.row + 1, position.col + 1),
270274
None,
271275
);
272276
}
273277

278+
fn render_position_percentage<F>(context: &mut RenderContext, write: F)
279+
where
280+
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
281+
{
282+
let position = get_position(context);
283+
let maxrows = context.doc.text().len_lines();
284+
write(
285+
context,
286+
format!("{}%", (position.row + 1) * 100 / maxrows),
287+
None,
288+
);
289+
}
290+
274291
fn render_file_encoding<F>(context: &mut RenderContext, write: F)
275292
where
276293
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,

helix-view/src/editor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ pub enum StatusLineElement {
247247
/// The cursor position
248248
Position,
249249

250+
/// The cursor position as a percent of the total file
251+
PositionPercentage,
250252
/// A single space
251253
Spacer,
252254
}

0 commit comments

Comments
 (0)