Skip to content

Commit

Permalink
Remove redundant Spans::new and Spans::single
Browse files Browse the repository at this point in the history
  • Loading branch information
hamrik committed Aug 11, 2023
1 parent d8214f8 commit 48d8d2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
69 changes: 38 additions & 31 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ui::ProgressSpinners;

use helix_view::editor::StatusLineElement as StatusLineElementID;
use tui::buffer::Buffer as Surface;
use tui::text::Spans;
use tui::text::{Span, Spans};

pub struct RenderContext<'a> {
pub editor: &'a Editor,
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface

let spacer = {
let spacer_str = &config.statusline.spacer;
Spans::single(spacer_str.clone(), context.base_style)
Spans::from(Span::styled(spacer_str.clone(), context.base_style))
};

context.parts.left = Spans::join(
Expand Down Expand Up @@ -168,19 +168,20 @@ fn render_mode<'a>(context: &RenderContext) -> Spans<'a> {

if context.focused {
if config.color_modes {
Spans::single(
Span::styled(
format!(" {} ", mode_str),
match context.editor.mode() {
Mode::Normal => context.editor.theme.get("ui.statusline.normal"),
Mode::Select => context.editor.theme.get("ui.statusline.select"),
Mode::Insert => context.editor.theme.get("ui.statusline.insert"),
},
)
.into()
} else {
Spans::single(String::from(mode_str), context.base_style)
Span::styled(String::from(mode_str), context.base_style).into()
}
} else {
Spans::new()
Spans::default()
}
}

Expand All @@ -194,9 +195,9 @@ fn render_lsp_spinner<'a>(context: &RenderContext) -> Spans<'a> {
});

if let Some(frame) = spinner {
Spans::single(String::from(frame), context.base_style)
Span::styled(String::from(frame), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}

Expand All @@ -213,7 +214,7 @@ fn render_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
_ => (warnings, errors),
});

let mut spans = Spans::new();
let mut spans = Spans::default();

if warnings > 0 {
spans.push("●", context.editor.theme.get("warning"));
Expand Down Expand Up @@ -243,7 +244,7 @@ fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
},
);

let mut spans = Spans::new();
let mut spans = Spans::default();

if warnings > 0 || errors > 0 {
spans.push("W", context.base_style);
Expand All @@ -267,19 +268,21 @@ fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
fn render_selections<'a>(context: &RenderContext) -> Spans<'a> {
let count = context.doc.selection(context.view.id).len();

Spans::single(
Span::styled(
format!("{} sel{}", count, if count == 1 { "" } else { "s" }),
context.base_style,
)
.into()
}

fn render_primary_selection_length<'a>(context: &RenderContext) -> Spans<'a> {
let tot_sel = context.doc.selection(context.view.id).primary().len();

Spans::single(
Span::styled(
format!("{} char{}", tot_sel, if tot_sel == 1 { "" } else { "s" }),
context.base_style,
)
.into()
}

fn get_position(context: &RenderContext) -> Position {
Expand All @@ -296,36 +299,39 @@ fn get_position(context: &RenderContext) -> Position {
fn render_position<'a>(context: &RenderContext) -> Spans<'a> {
let position = get_position(context);

Spans::single(
Span::styled(
format!("{}:{}", position.row + 1, position.col + 1),
context.base_style,
)
.into()
}

fn render_total_line_numbers<'a>(context: &RenderContext) -> Spans<'a> {
Spans::single(
Span::styled(
context.doc.text().len_lines().to_string(),
context.base_style,
)
.into()
}

fn render_position_percentage<'a>(context: &RenderContext) -> Spans<'a> {
let position = get_position(context);
let maxrows = context.doc.text().len_lines();

Spans::single(
Span::styled(
format!("{}%", (position.row + 1) * 100 / maxrows),
context.base_style,
)
.into()
}

fn render_file_encoding<'a>(context: &RenderContext) -> Spans<'a> {
let enc = context.doc.encoding();

if enc != encoding::UTF_8 {
Spans::single(enc.name(), context.base_style)
Span::styled(enc.name(), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}

Expand All @@ -348,13 +354,13 @@ fn render_file_line_ending<'a>(context: &RenderContext) -> Spans<'a> {
PS => "PS", // U+2029 -- ParagraphSeparator
};

Spans::single(line_ending, context.base_style)
Span::styled(line_ending, context.base_style).into()
}

fn render_file_type<'a>(context: &RenderContext) -> Spans<'a> {
let file_type = context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME);

Spans::single(String::from(file_type), context.base_style)
Span::styled(String::from(file_type), context.base_style).into()
}

fn render_file_name<'a>(context: &RenderContext) -> Spans<'a> {
Expand All @@ -367,22 +373,22 @@ fn render_file_name<'a>(context: &RenderContext) -> Spans<'a> {
format!("{}", path)
};

Spans::single(title, context.base_style)
Span::styled(title, context.base_style).into()
}

fn render_file_modification_indicator<'a>(context: &RenderContext) -> Spans<'a> {
if context.doc.is_modified() {
Spans::single(String::from("[+]"), context.base_style)
Span::styled(String::from("[+]"), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}

fn render_read_only_indicator<'a>(context: &mut RenderContext) -> Spans<'a> {
fn render_read_only_indicator<'a>(context: &RenderContext) -> Spans<'a> {
if context.doc.readonly {
Spans::single("[readonly]", context.base_style.clone())
Span::styled(String::from("[readonly]"), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}

Expand All @@ -396,32 +402,33 @@ fn render_file_base_name<'a>(context: &RenderContext) -> Spans<'a> {
String::from(path)
};

Spans::single(title, context.base_style)
Span::styled(title, context.base_style).into()
}

fn render_separator<'a>(context: &RenderContext) -> Spans<'a> {
Spans::single(
Span::styled(
context.editor.config().statusline.separator.to_string(),
context.editor.theme.get("ui.statusline.separator"),
)
.into()
}

fn render_spacer<'a>(context: &RenderContext) -> Spans<'a> {
Spans::single("", context.base_style)
Span::styled("", context.base_style).into()
}

fn render_version_control<'a>(context: &RenderContext) -> Spans<'a> {
if let Some(head) = context.doc.version_control_head() {
Spans::single(head.to_string(), context.base_style)
Span::styled(head.to_string(), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}

fn render_register<'a>(context: &RenderContext) -> Spans<'a> {
if let Some(reg) = context.editor.selected_register {
Spans::single(format!("reg={}", reg), context.base_style)
Span::styled(format!("reg={}", reg), context.base_style).into()
} else {
Spans::new()
Spans::default()
}
}
14 changes: 0 additions & 14 deletions helix-tui/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,6 @@ impl<'a> Spans<'a> {
self.0.push(Span::styled(string, style));
}

/// Creates a new, empty `Spans`.
pub fn new() -> Spans<'a> {
Spans(vec![])
}

/// Constructs a single, styled `Span` with the given `string` and `style`
/// and wraps it in a `Spans`.
pub fn single<T>(string: T, style: Style) -> Spans<'a>
where
T: Into<Cow<'a, str>>,
{
Spans(vec![Span::styled(string, style)])
}

/// Intersperses `sep` between items of the given iterator then returns the concatenated Spans.
///
/// ## Examples
Expand Down

0 comments on commit 48d8d2a

Please sign in to comment.