mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-26 03:27:30 +00:00
Parallelize prompt modules (#46)
This commit is contained in:
parent
c6ee5c6ac1
commit
8b5055d510
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -689,6 +689,7 @@ dependencies = [
|
||||
"criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -11,6 +11,7 @@ dirs = "1.0.5"
|
||||
git2 = "0.8.0"
|
||||
toml = "0.5.0"
|
||||
serde_json = "1.0.39"
|
||||
rayon = "1.0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.0.7"
|
||||
|
@ -6,6 +6,8 @@ use criterion::Criterion;
|
||||
use clap::{App, Arg};
|
||||
use starship::context::Context;
|
||||
use starship::modules;
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn char_segment(c: &mut Criterion) {
|
||||
let args = App::new("starship")
|
||||
@ -40,5 +42,28 @@ fn line_break_segment(c: &mut Criterion) {
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, dir_segment, char_segment, line_break_segment);
|
||||
fn git_branch_segment(c: &mut Criterion) {
|
||||
let tmp_dir = TempDir::new().unwrap();
|
||||
let repo_dir = tmp_dir.path().join("rocket-controls");
|
||||
fs::create_dir(&repo_dir).unwrap();
|
||||
|
||||
git2::Repository::init(&repo_dir).unwrap();
|
||||
|
||||
let args = App::new("starship")
|
||||
.arg(Arg::with_name("status_code"))
|
||||
.get_matches_from(vec!["starship", "0"]);
|
||||
let context = Context::new_with_dir(args, "~");
|
||||
|
||||
c.bench_function("git_branch segment", move |b| {
|
||||
b.iter(|| modules::handle("git_branch", &context))
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches,
|
||||
char_segment,
|
||||
dir_segment,
|
||||
line_break_segment,
|
||||
git_branch_segment
|
||||
);
|
||||
criterion_main!(benches);
|
||||
|
@ -8,7 +8,7 @@ pub struct Context<'a> {
|
||||
pub current_dir: PathBuf,
|
||||
pub dir_files: Vec<PathBuf>,
|
||||
pub arguments: ArgMatches<'a>,
|
||||
pub repository: Option<Repository>,
|
||||
pub repo_root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
@ -36,13 +36,15 @@ impl<'a> Context<'a> {
|
||||
.map(|entry| entry.path())
|
||||
.collect::<Vec<PathBuf>>();
|
||||
|
||||
let repository: Option<Repository> = Repository::discover(¤t_dir).ok();
|
||||
let repo_root: Option<PathBuf> = Repository::discover(¤t_dir)
|
||||
.ok()
|
||||
.and_then(|repo| repo.workdir().map(|repo| repo.to_path_buf()));
|
||||
|
||||
Context {
|
||||
arguments,
|
||||
current_dir,
|
||||
dir_files,
|
||||
repository,
|
||||
repo_root,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,9 +23,8 @@ pub fn segment(context: &Context) -> Option<Module> {
|
||||
let current_dir = &context.current_dir;
|
||||
|
||||
let dir_string;
|
||||
if let Some(repo) = &context.repository {
|
||||
if let Some(repo_root) = &context.repo_root {
|
||||
// Contract the path to the git repo root
|
||||
let repo_root = repo.workdir().unwrap();
|
||||
let repo_folder_name = repo_root.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
dir_string = contract_path(¤t_dir, repo_root, repo_folder_name);
|
||||
|
@ -7,12 +7,10 @@ use super::{Context, Module};
|
||||
///
|
||||
/// Will display the branch name if the current directory is a git repo
|
||||
pub fn segment(context: &Context) -> Option<Module> {
|
||||
if context.repository.is_none() {
|
||||
return None;
|
||||
}
|
||||
let repo_root = context.repo_root.as_ref()?;
|
||||
let repository = Repository::open(repo_root).ok()?;
|
||||
|
||||
let repository = context.repository.as_ref().unwrap();
|
||||
match get_current_branch(repository) {
|
||||
match get_current_branch(&repository) {
|
||||
Ok(branch_name) => {
|
||||
const GIT_BRANCH_CHAR: &str = " ";
|
||||
let segment_color = Color::Purple.bold();
|
||||
|
@ -1,4 +1,5 @@
|
||||
use clap::ArgMatches;
|
||||
use rayon::prelude::*;
|
||||
use std::io::{self, Write};
|
||||
|
||||
use crate::context::Context;
|
||||
@ -29,7 +30,7 @@ pub fn prompt(args: ArgMatches) {
|
||||
writeln!(handle).unwrap();
|
||||
|
||||
let modules = prompt_order
|
||||
.iter()
|
||||
.par_iter()
|
||||
.map(|module| modules::handle(module, &context)) // Compute modules
|
||||
.flatten()
|
||||
.collect::<Vec<Module>>(); // Remove segments set to `None`
|
||||
|
Loading…
Reference in New Issue
Block a user