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 percentage through the file in the statusline/modeline #246

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions src/presenters/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ pub fn display(workspace: &mut Workspace, view: &mut View, error: &Error) {
let _ = presenter.print_buffer(buffer, &data, None, None);
}

presenter.print_status_line(&[StatusLineData {
content: error.description().to_string(),
style: Style::Bold,
colors: Colors::Warning,
}]);
presenter.print_status_line(
&[
StatusLineData {
content: error.description().to_string(),
style: Style::Bold,
colors: Colors::Warning,
}
],
&[],
);

presenter.present();
}
32 changes: 24 additions & 8 deletions src/presenters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ fn path_as_title(path: &Path) -> String {
}

fn current_buffer_status_line_data(workspace: &mut Workspace) -> StatusLineData {
let modified = workspace.current_buffer().map(|b| b.modified()).unwrap_or(false);
let buffer = workspace.current_buffer();
let modified = buffer.as_ref().map(|b| b.modified()).unwrap_or(false);

let (content, style) = workspace.current_buffer_path().map(|path| {
// Determine buffer title styles based on its modification status.
let mut title = path_as_title(path);
let mut style = Style::Default;

if modified {
// Use an emboldened path with an asterisk.
let mut title = path_as_title(path);
title.push('*');

(title, Style::Bold)
} else {
(path_as_title(path), Style::Default)
style = Style::Bold;
}

(format!(" {}", title), style)
}).unwrap_or((String::new(), Style::Default));

StatusLineData {
Expand All @@ -33,6 +33,21 @@ fn current_buffer_status_line_data(workspace: &mut Workspace) -> StatusLineData
}
}

fn percentage_cursor_indicator_line_data(workspace: &mut Workspace) -> StatusLineData {
let content = workspace.current_buffer().map(|b| {
let line_total = b.line_count();
let line_at = b.cursor.position.line + 1;
let line_perc = (line_at * 100) / line_total;
format!(" [{:2}%]", line_perc)
}).unwrap_or(String::new());

StatusLineData {
content,
style: Style::Default,
colors: Colors::Focused,
}
}

fn git_status_line_data(repo: &Option<Repository>, path: &Option<PathBuf>) -> StatusLineData {
// Build a display value for the current buffer's git status.
let mut content = String::new();
Expand All @@ -54,6 +69,7 @@ fn git_status_line_data(repo: &Option<Repository>, path: &Option<PathBuf>) -> St
colors: Colors::Focused,
}
}

fn presentable_status(status: &Status) -> &str {
if status.contains(git2::Status::WT_NEW) {
if status.contains(git2::Status::INDEX_NEW) {
Expand Down
17 changes: 10 additions & 7 deletions src/presenters/modes/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {

// Draw the status line as a search prompt.
let confirmation = "Are you sure? (y/n)".to_string();
presenter.print_status_line(&[
StatusLineData {
content: confirmation,
style: Style::Bold,
colors: Colors::Warning,
}
]);
presenter.print_status_line(
&[
StatusLineData {
content: confirmation,
style: Style::Bold,
colors: Colors::Warning,
}
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, None, None)?;

presenter.print_status_line(&[
StatusLineData {
content: " INSERT ".to_string(),
style: Style::Default,
colors: Colors::Insert,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " INSERT ".to_string(),
style: Style::Default,
colors: Colors::Insert,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub fn display(workspace: &mut Workspace, mode: &mut JumpMode, view: &mut View)
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, None, Some(mode))?;

presenter.print_status_line(&[
StatusLineData {
content: " JUMP ".to_string(),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " JUMP ".to_string(),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
],
&[],
);

// Don't display a cursor.
presenter.set_cursor(None);
Expand Down
17 changes: 10 additions & 7 deletions src/presenters/modes/line_jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ pub fn display(workspace: &mut Workspace, mode: &LineJumpMode, view: &mut View)
// Draw the status line as an input prompt.
let input_prompt = format!("Go to line: {}", mode.input);
let input_prompt_len = input_prompt.len();
presenter.print_status_line(&[
StatusLineData {
content: input_prompt,
style: Style::Default,
colors: Colors::Default,
}
]);
presenter.print_status_line(
&[
StatusLineData {
content: input_prompt,
style: Style::Default,
colors: Colors::Default,
}
],
&[],
);

// Move the cursor to the end of the search query input.
let cursor_line = presenter.height() - 1;
Expand Down
38 changes: 28 additions & 10 deletions src/presenters/modes/normal.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use crate::errors::*;
use scribe::Workspace;
use scribe::buffer::Position;
use crate::presenters::{current_buffer_status_line_data, git_status_line_data};
use crate::presenters::{
current_buffer_status_line_data,
git_status_line_data,
percentage_cursor_indicator_line_data
};
use git2::Repository;
use crate::view::{Colors, StatusLineData, Style, View};

pub fn display(workspace: &mut Workspace, view: &mut View, repo: &Option<Repository>) -> Result<()> {
let height = view.height();
let mut presenter = view.build_presenter()?;
let buffer_status = current_buffer_status_line_data(workspace);

if let Some(buf) = workspace.current_buffer() {
let line_count = buf.line_count();

// Draw the visible set of tokens to the terminal.
let data = buf.data();
presenter.print_buffer(buf, &data, None, None)?;
Expand All @@ -21,16 +28,27 @@ pub fn display(workspace: &mut Workspace, view: &mut View, repo: &Option<Reposit
Colors::Inverted
};

let mut right_widgets = vec![
git_status_line_data(&repo, &buf.path),
percentage_cursor_indicator_line_data(workspace),
];

if line_count <= height {
right_widgets.pop();
}

// Build the status line mode and buffer title display.
presenter.print_status_line(&[
StatusLineData {
content: " NORMAL ".to_string(),
style: Style::Default,
colors,
},
buffer_status,
git_status_line_data(&repo, &buf.path)
]);
presenter.print_status_line(
&[
StatusLineData {
content: " NORMAL ".to_string(),
style: Style::Default,
colors,
},
buffer_status,
],
&right_widgets,
);

presenter.present();
} else {
Expand Down
27 changes: 15 additions & 12 deletions src/presenters/modes/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ pub fn display(workspace: &mut Workspace, mode: &PathMode, view: &mut View) -> R
mode_display.graphemes(true).count() +
search_input.graphemes(true).count();

presenter.print_status_line(&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::PathMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
]);
presenter.print_status_line(
&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::PathMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
],
&[],
);

// Move the cursor to the end of the search query input.
{
Expand Down
38 changes: 21 additions & 17 deletions src/presenters/modes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@ pub fn display(workspace: &mut Workspace, mode: &SearchMode, view: &mut View) ->
mode_display.graphemes(true).count() +
search_input.graphemes(true).count();

presenter.print_status_line(&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::SearchMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
StatusLineData {
content: result_display,
style: Style::Default,
colors: Colors::Focused,
},
]);
presenter.print_status_line(
&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::SearchMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
],
&[
StatusLineData {
content: result_display,
style: Style::Default,
colors: Colors::Focused,
},
],
);

// Move the cursor to the end of the search query input.
if mode.insert {
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ pub fn display<T: Display>(workspace: &mut Workspace, mode: &mut dyn SearchSelec
data = buf.data();
presenter.print_buffer(buf, &data, None, None)?;

presenter.print_status_line(&[
StatusLineData {
content: format!(" {} ", mode),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: format!(" {} ", mode),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
],
&[],
);
}

if let Some(message) = mode.message() {
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub fn display(workspace: &mut Workspace, mode: &SelectMode, view: &mut View) ->
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, Some(&[selected_range]), None)?;

presenter.print_status_line(&[
StatusLineData {
content: " SELECT ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " SELECT ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/select_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ pub fn display(workspace: &mut Workspace, mode: &SelectLineMode, view: &mut View
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, Some(&[selected_range]), None)?;

presenter.print_status_line(&[
StatusLineData {
content: " SELECT LINE ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " SELECT LINE ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
Loading