Skip to content

Commit

Permalink
style: simplify string formatting for readability (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamirmahal authored Oct 26, 2024
1 parent 7cba26e commit ebbeeba
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 40 deletions.
3 changes: 1 addition & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ fn write_command(
output
.write(
format!(
" commands.insert(\"{}::{}\", {}::{});\n",
module_name, function_name, module_name, function_name
" commands.insert(\"{module_name}::{function_name}\", {module_name}::{function_name});\n"
)
.as_bytes(),
)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub fn display_available_commands(app: &mut Application) -> Result {
command_keys.sort();
command_keys.reverse();
for key in command_keys {
buffer.insert(format!("{}\n", key));
buffer.insert(format!("{key}\n"));
}
}

Expand All @@ -287,11 +287,11 @@ pub fn display_last_error(app: &mut Application) -> Result {
let scope_display_buffer = {
let mut error_buffer = Buffer::new();
// Add the proximate/contextual error.
error_buffer.insert(format!("{}\n", error));
error_buffer.insert(format!("{error}\n"));

// Print the chain of other errors that led to the proximate error.
for err in error.iter().skip(1) {
error_buffer.insert(format!("caused by: {}", err));
error_buffer.insert(format!("caused by: {err}"));
}

error_buffer
Expand Down
6 changes: 3 additions & 3 deletions src/commands/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn merge_next_line(app: &mut Application) -> Result {
.take(2)
.map(|(index, line)| {
if index == current_line {
format!("{} ", line)
format!("{line} ")
} else {
line.trim_start().to_string()
}
Expand Down Expand Up @@ -357,7 +357,7 @@ pub fn display_current_scope(app: &mut Application) -> Result {
// Open a buffer with a displayable version of the scope stack.
let mut scope_display_buffer = Buffer::new();
for scope in scope_stack.iter() {
scope_display_buffer.insert(format!("{}\n", scope));
scope_display_buffer.insert(format!("{scope}\n"));
}

scope_display_buffer
Expand Down Expand Up @@ -761,7 +761,7 @@ pub fn paste(app: &mut Application) -> Result {
line,
offset: line_content.len(),
});
buffer.insert(format!("\n{}", content));
buffer.insert(format!("\n{content}"));
buffer.cursor.move_to(original_cursor_position);
} else {
// We're on a trailing newline, which doesn't
Expand Down
6 changes: 3 additions & 3 deletions src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ pub fn copy_remote_url(app: &mut Application) -> Result {
let line_2 = s.anchor + 1;

match line_1.cmp(&line_2) {
Ordering::Less => format!("#L{}-L{}", line_1, line_2),
Ordering::Greater => format!("#L{}-L{}", line_2, line_1),
Ordering::Equal => format!("#L{}", line_1),
Ordering::Less => format!("#L{line_1}-L{line_2}"),
Ordering::Greater => format!("#L{line_2}-L{line_1}"),
Ordering::Equal => format!("#L{line_1}"),
}
}
_ => String::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn move_to_current_result(app: &mut Application) -> Result {
.as_mut()
.ok_or(NO_SEARCH_RESULTS)?
.selection()
.ok_or_else(|| format!("No matches found for \"{}\"", query))?;
.ok_or_else(|| format!("No matches found for \"{query}\""))?;
buffer.cursor.move_to(result.start());
} else {
bail!("Can't move to search result outside of search mode");
Expand Down
21 changes: 10 additions & 11 deletions src/input/key_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl KeyMap {
.as_str()
.ok_or_else(|| "A mode key couldn't be parsed as a string".to_string())?;
let key_bindings = parse_mode_key_bindings(yaml_key_bindings, &commands)
.chain_err(|| format!("Failed to parse keymaps for \"{}\" mode", mode))?;
.chain_err(|| format!("Failed to parse keymaps for \"{mode}\" mode"))?;

keymap.insert(mode.to_string(), key_bindings);
}
Expand Down Expand Up @@ -146,21 +146,20 @@ fn parse_mode_key_bindings(
Yaml::String(ref command) => {
let command_string = command.as_str();

key_commands.push(*commands.get(&command_string).ok_or_else(|| {
format!("Keymap command \"{}\" doesn't exist", command_string)
})?);
key_commands.push(
*commands.get(&command_string).ok_or_else(|| {
format!("Keymap command \"{command_string}\" doesn't exist")
})?,
);
}
Yaml::Array(ref command_array) => {
for command in command_array {
let command_string = command.as_str().ok_or_else(|| {
format!(
"Keymap command \"{:?}\" couldn't be parsed as a string",
command
)
format!("Keymap command \"{command:?}\" couldn't be parsed as a string")
})?;

key_commands.push(*commands.get(command_string).ok_or_else(|| {
format!("Keymap command \"{}\" doesn't exist", command_string)
format!("Keymap command \"{command_string}\" doesn't exist")
})?);
}
}
Expand Down Expand Up @@ -194,7 +193,7 @@ fn parse_key(data: &str) -> Result<Key> {
let key_char = key
.chars()
.next()
.ok_or_else(|| format!("Keymap key \"{}\" is invalid", key))?;
.ok_or_else(|| format!("Keymap key \"{key}\" is invalid"))?;

// Find the variant for the specified modifier.
match component {
Expand Down Expand Up @@ -225,7 +224,7 @@ fn parse_key(data: &str) -> Result<Key> {
component
.chars()
.next()
.ok_or_else(|| format!("Keymap key \"{}\" is invalid", component))?,
.ok_or_else(|| format!("Keymap key \"{component}\" is invalid"))?,
),
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ fn main() {

fn handle_error(error: &Error) {
// Print the proximate/contextual error.
eprintln!("error: {}", error);
eprintln!("error: {error}");

// Print the chain of other errors that led to the proximate error.
for e in error.iter().skip(1) {
eprintln!("caused by: {}", e);
eprintln!("caused by: {e}");
}

// Print the backtrace, if available.
if let Some(backtrace) = error.backtrace() {
eprintln!("backtrace: {:?}", backtrace);
eprintln!("backtrace: {backtrace:?}");
}

// Exit with an error code.
Expand Down
2 changes: 1 addition & 1 deletion src/models/application/modes/open/exclusions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn parse(exclusion_data: &[Yaml]) -> Result<Vec<ExclusionPattern>> {
if let Yaml::String(ref pattern) = *exclusion {
mapped_exclusions.push(
ExclusionPattern::new(pattern)
.chain_err(|| format!("Failed to parse exclusion pattern: {}", pattern))?,
.chain_err(|| format!("Failed to parse exclusion pattern: {pattern}"))?,
);
} else {
bail!("Found a non-string exclusion that can't be parsed.");
Expand Down
8 changes: 4 additions & 4 deletions src/presenters/modes/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn display(workspace: &mut Workspace, mode: &mut OpenMode, view: &mut View)

presenter.print_status_line(&[
StatusLineData {
content: format!(" {} ", mode),
content: format!(" {mode} "),
style: Style::Default,
colors: Colors::Inverted,
},
Expand All @@ -45,11 +45,11 @@ pub fn display(workspace: &mut Workspace, mode: &mut OpenMode, view: &mut View)

for (line, result) in mode.results().enumerate() {
let (content, colors, style) = if line == mode.selected_index() {
(format!("> {}", result), Colors::Focused, Style::Bold)
(format!("> {result}"), Colors::Focused, Style::Bold)
} else if selected_indices.contains(&line) {
(format!(" {}", result), Colors::Focused, Style::Bold)
(format!(" {result}"), Colors::Focused, Style::Bold)
} else {
(format!(" {}", result), Colors::Default, Style::Default)
(format!(" {result}"), Colors::Default, Style::Default)
};

// Ensure content doesn't exceed the screen width
Expand Down
2 changes: 1 addition & 1 deletion src/presenters/modes/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn display(workspace: &mut Workspace, mode: &PathMode, view: &mut View) -> R
let data = buffer.data();
presenter.print_buffer(buffer, &data, &workspace.syntax_set, None, None)?;

let mode_display = format!(" {} ", mode);
let mode_display = format!(" {mode} ");
let search_input = format!(" {}", mode.input);

let cursor_offset = mode_display.graphemes(true).count() + search_input.graphemes(true).count();
Expand Down
2 changes: 1 addition & 1 deletion src/presenters/modes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn display(workspace: &mut Workspace, mode: &SearchMode, view: &mut View) ->
None,
)?;

let mode_display = format!(" {} ", mode);
let mode_display = format!(" {mode} ");
let search_input = format!(" {}", mode.input.as_ref().unwrap_or(&String::new()));
let result_display = if mode.insert {
String::new()
Expand Down
6 changes: 3 additions & 3 deletions src/presenters/modes/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn display<T: SearchSelectMode + Display>(

presenter.print_status_line(&[
StatusLineData {
content: format!(" {} ", mode),
content: format!(" {mode} "),
style: Style::Default,
colors: Colors::Inverted,
},
Expand All @@ -48,9 +48,9 @@ pub fn display<T: SearchSelectMode + Display>(
// Draw the list of search results.
for (line, result) in mode.results().enumerate() {
let (content, colors, style) = if line == mode.selected_index() {
(format!("> {}", result), Colors::Focused, Style::Bold)
(format!("> {result}"), Colors::Focused, Style::Bold)
} else {
(format!(" {}", result), Colors::Default, Style::Default)
(format!(" {result}"), Colors::Default, Style::Default)
};

// Ensure content doesn't exceed the screen width
Expand Down
2 changes: 1 addition & 1 deletion src/view/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'p> Presenter<'p> {
.theme_set
.themes
.get(theme_name)
.ok_or_else(|| format!("Couldn't find \"{}\" theme", theme_name))?;
.ok_or_else(|| format!("Couldn't find \"{theme_name}\" theme"))?;
theme.clone()
};

Expand Down
4 changes: 2 additions & 2 deletions src/view/terminal/termion_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl TermionTerminal {

// Adding new styles are easy; write it and return early.
if let Some(mapped_style) = map_style(new_style) {
let _ = write!(output, "{}", mapped_style);
let _ = write!(output, "{mapped_style}");

return Ok(());
}
Expand Down Expand Up @@ -293,7 +293,7 @@ impl Terminal for TermionTerminal {

// Now that style, color, and position have been
// addressed, print the content.
let _ = write!(output, "{}", content);
let _ = write!(output, "{content}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/view/terminal/test_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Terminal for TestTerminal {
}

let mut data = self.data.lock().unwrap();
let string_content = format!("{}", content);
let string_content = format!("{content}");

for (i, c) in string_content.chars().enumerate() {
// Ignore characters beyond visible width.
Expand Down

0 comments on commit ebbeeba

Please sign in to comment.