summaryrefslogtreecommitdiffstats
path: root/src/print.rs
blob: 53442f23fd0ae76ddb793f37892ac0161f4b1dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use clap::ArgMatches;
use rayon::prelude::*;
use std::io::{self, Write};

use crate::context::Context;
use crate::module::Module;
use crate::modules;

pub fn prompt(args: ArgMatches) {
    let prompt_order = vec![
        "battery",
        "username",
        "directory",
        "git_branch",
        "git_status",
        "package",
        "nodejs",
        "rust",
        "python",
        "go",
        "line_break",
        "character",
    ];
    let context = Context::new(args);

    let stdout = io::stdout();
    let mut handle = stdout.lock();

    // Write a new line before the prompt
    writeln!(handle).unwrap();

    let modules = prompt_order
        .par_iter()
        .map(|module| modules::handle(module, &context)) // Compute modules
        .flatten()
        .collect::<Vec<Module>>(); // Remove segments set to `None`

    let mut printable = modules.iter();

    // Print the first module without its prefix
    if let Some(first_module) = printable.next() {
        let module_without_prefix = first_module.to_string_without_prefix();
        write!(handle, "{}", module_without_prefix).unwrap()
    }

    // Print all remaining modules
    printable.for_each(|module| write!(handle, "{}", module).unwrap());
}