Skip to content

Commit

Permalink
style: actually format using new rustfmt settings
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Oct 27, 2022
1 parent 343d503 commit 46cbe2b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"editor.rulers": [120],
"editor.rulers": [100],
"editor.formatOnSave": true,
"editor.inlayHints.enabled": "offUnlessPressed",
"rust-analyzer.rustfmt.extraArgs": ["+nightly"],
Expand Down
4 changes: 3 additions & 1 deletion src/app/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ pub struct AppError(pub String);

impl fmt::Debug for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AppError").field("message", &self.0).finish()
f.debug_struct("AppError")
.field("message", &self.0)
.finish()
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/app/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pub fn execute_script(pm: PackageManager, script: Script) -> Result<ExitStatus,
if let Ok(mut child) = cmd.spawn() {
match child.wait() {
| Ok(exit_status) => Ok(exit_status),
| Err(_) => Err(AppError(
"Wasn't able to wait for child process to exit completely.".to_string(),
)),
| Err(_) => {
Err(AppError(
"Wasn't able to wait for child process to exit completely.".to_string(),
))
},
}
} else {
let pm = pm.as_str();
Expand Down
18 changes: 13 additions & 5 deletions src/app/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use super::{AppError, Script};
fn resolve_pkg_path() -> Result<PathBuf, AppError> {
match env::current_dir() {
| Ok(dir) => Ok(dir.join("package.json")),
| Err(_) => Err(AppError("Couldn't resolve the current working directory.".to_string())),
| Err(_) => {
Err(AppError(
"Couldn't resolve the current working directory.".to_string(),
))
},
}
}

Expand All @@ -23,15 +27,19 @@ fn produce_scripts(scripts_map: &Map<String, Value>) -> Vec<Script> {
.map(|(id, (key, value))| {
let script = key.to_string();

// Raw conversion to string leaves JSON representation, so strings are in quotes. This piece works that around.
// Also we gracefully default to an empty string if command in JSON wasn't actually a string, and instead it was
// `null` or something else.
// Raw conversion to string leaves JSON representation, so strings are in quotes. This piece
// works that around. Also we gracefully default to an empty string if command in JSON wasn't
// actually a string, and instead it was `null` or something else.
let command = match value.as_str() {
| Some(command) => command.to_string(),
| None => String::new(),
};

Script { id, script, command }
Script {
id,
script,
command,
}
})
.collect::<Vec<Script>>()
}
Expand Down
15 changes: 11 additions & 4 deletions src/app/runner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::process;

use super::{AppError, Message, PackageManager, Script};
use crate::app;
use crate::prompt::*;
use super::{AppError, Message, PackageManager, Script};

/// Runs a prompt to select a script interactively, and then execute it.
pub fn run_interactive(pm: PackageManager, options: Vec<Script>) -> Result<(), AppError> {
Expand All @@ -28,8 +28,13 @@ pub fn run_interactive(pm: PackageManager, options: Vec<Script>) -> Result<(), A
run((pm, script))
}

/// Checks if provided script name is present in scripts loaded from `package.json`, and then executes it.
pub fn run_direct(pm: PackageManager, options: Vec<Script>, selection: String) -> Result<(), AppError> {
/// Checks if provided script name is present in scripts loaded from `package.json`, and then
/// executes it.
pub fn run_direct(
pm: PackageManager,
options: Vec<Script>,
selection: String,
) -> Result<(), AppError> {
options
.iter()
.find(|&Script { script, .. }| script == &selection)
Expand All @@ -38,7 +43,9 @@ pub fn run_direct(pm: PackageManager, options: Vec<Script>, selection: String) -
}

fn run((pm, selection): (PackageManager, Script)) -> Result<(), AppError> {
let Script { script, command, .. } = &selection;
let Script {
script, command, ..
} = &selection;

println!(
"Executing: {script} {separator} {command}",
Expand Down
2 changes: 1 addition & 1 deletion src/app/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt::{Display, Formatter, Result};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};

use crate::prompt::Symbols;
use super::PackageManager;
use crate::prompt::Symbols;

#[derive(Clone, Debug)]
pub struct Message {
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ impl Cli {
});

match pm_matrix {
| (true, _, _) => PackageManager::Npm,
| (true, ..) => PackageManager::Npm,
| (_, true, _) => PackageManager::Pnpm,
| (_, _, true) => PackageManager::Yarn,
| (_, _, _) => pm_from_env,
| (..) => pm_from_env,
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ mod prompt;

use std::process;

use cli::Cli;
use app::AppError;
use cli::Cli;

fn main() {
let cli = Cli::new();
Expand Down
8 changes: 5 additions & 3 deletions src/prompt/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::clone::Clone;
use std::fmt::{self, Debug, Display, Formatter};
use std::io::{self, Write};

use crossterm::{cursor, queue};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::style::{style, Print, PrintStyledContent, Stylize};
use crossterm::terminal::{self, Clear, ClearType};
use crossterm::{cursor, queue};

use super::{Prompt, State, Symbols};
use super::utils;
use super::{Prompt, State, Symbols};

pub struct SelectPrompt<T> {
message: String,
Expand Down Expand Up @@ -157,7 +157,9 @@ impl<T: Clone + Display> Prompt<T> for SelectPrompt<T> {
| KeyCode::Home => self.current = 0,
| KeyCode::End => self.current = self.items.len() - 1,
| KeyCode::Char('k') | KeyCode::Up => self.current = self.current.saturating_sub(1),
| KeyCode::Char('j') | KeyCode::Down => self.current = std::cmp::min(self.current + 1, self.items.len() - 1),
| KeyCode::Char('j') | KeyCode::Down => {
self.current = std::cmp::min(self.current + 1, self.items.len() - 1)
},
| _ => {},
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/prompt/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pub fn is_event_abortable(event: KeyEvent) -> bool {
}

pub fn calculate_limit_indexes(current: usize, total: usize, limit: usize) -> (usize, usize) {
let start_index = std::cmp::min(total.saturating_sub(limit), current.saturating_sub(limit / 2));
let start_index = std::cmp::min(
total.saturating_sub(limit),
current.saturating_sub(limit / 2),
);

let end_index = std::cmp::min(start_index + limit, total);

(start_index, end_index)
Expand Down

0 comments on commit 46cbe2b

Please sign in to comment.