on master help sign in
add position-percentage as a statusline indicator (#3168)
3dd2196Seth Bromberger
book/src/configuration.md+1
@@ -78,6 +78,7 @@ The following elements can be configured:
78| `diagnostics` | The number of warnings and/or errors |
78| `diagnostics` | The number of warnings and/or errors |
79| `selections` | The number of active selections |
79| `selections` | The number of active selections |
80| `position` | The cursor position |
80| `position` | The cursor position |
81+| `position-percentage` | The cursor position as a percentage of the total number of lines |
81| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
82| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
82
83
83### `[editor.lsp]` Section
84### `[editor.lsp]` Section
helix-term/src/ui/statusline.rs+24−7
@@ -1,4 +1,4 @@
1−use helix_core::{coords_at_pos, encoding};
1+use helix_core::{coords_at_pos, encoding, Position};
2use helix_view::{
2use helix_view::{
3 document::{Mode, SCRATCH_BUFFER_NAME},
3 document::{Mode, SCRATCH_BUFFER_NAME},
4 graphics::Rect,
4 graphics::Rect,
@@ -143,6 +143,7 @@ where
143 helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
143 helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
144 helix_view::editor::StatusLineElement::Selections => render_selections,
144 helix_view::editor::StatusLineElement::Selections => render_selections,
145 helix_view::editor::StatusLineElement::Position => render_position,
145 helix_view::editor::StatusLineElement::Position => render_position,
146+ helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
146 helix_view::editor::StatusLineElement::Spacer => render_spacer,
147 helix_view::editor::StatusLineElement::Spacer => render_spacer,
147 }
148 }
148}
149}
@@ -251,19 +252,22 @@ where
251 );
252 );
252}
253}
253
254
254−fn render_position<F>(context: &mut RenderContext, write: F)
255+fn get_position(context: &RenderContext) -> Position {
255−where
256− F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
257−{
258− let position = coords_at_pos(
256+ coords_at_pos(
259 context.doc.text().slice(..),
257 context.doc.text().slice(..),
260 context
258 context
261 .doc
259 .doc
262 .selection(context.view.id)
260 .selection(context.view.id)
263 .primary()
261 .primary()
264 .cursor(context.doc.text().slice(..)),
262 .cursor(context.doc.text().slice(..)),
265− );
263+ )
264+}
266
265
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);
267 write(
271 write(
268 context,
272 context,
269 format!(" {}:{} ", position.row + 1, position.col + 1),
273 format!(" {}:{} ", position.row + 1, position.col + 1),
@@ -271,6 +275,19 @@ where
271 );
275 );
272}
276}
273
277
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+
274fn render_file_encoding<F>(context: &mut RenderContext, write: F)
291fn render_file_encoding<F>(context: &mut RenderContext, write: F)
275where
292where
276 F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
293 F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
helix-view/src/editor.rs+2
@@ -247,6 +247,8 @@ pub enum StatusLineElement {
247 /// The cursor position
247 /// The cursor position
248 Position,
248 Position,
249
249
250+ /// The cursor position as a percent of the total file
251+ PositionPercentage,
250 /// A single space
252 /// A single space
251 Spacer,
253 Spacer,
252}
254}