summaryrefslogtreecommitdiffstats
path: root/src/print.rs
diff options
context:
space:
mode:
authorBarnaby Keene <accounts@southcla.ws>2019-10-09 02:43:28 +0100
committerMatan Kushner <hello@matchai.me>2019-10-09 10:43:28 +0900
commit9f365f84d126cb16eb55870a1026d8e737a87b75 (patch)
tree7e60bfa2909a676d5a4e68e55115034b5ebd3672 /src/print.rs
parent8058b51273e16795e8e5ee4741b2556b44700506 (diff)
refactor: Allow starship to be better used programmatically (#509)
Structure the prompt as a function that returns a string instead of writing directly to stdout. This makes it easier to embed Starship into other Rust programs such as shells written in Rust. It also decouples the arguments from the context for more programmatic initialization of the context.
Diffstat (limited to 'src/print.rs')
-rw-r--r--src/print.rs17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/print.rs b/src/print.rs
index 5089dfa8b..69a954fae 100644
--- a/src/print.rs
+++ b/src/print.rs
@@ -1,5 +1,6 @@
use clap::ArgMatches;
use rayon::prelude::*;
+use std::fmt::Write as FmtWrite;
use std::io::{self, Write};
use crate::context::Context;
@@ -9,14 +10,18 @@ use crate::modules;
pub fn prompt(args: ArgMatches) {
let context = Context::new(args);
- let config = context.config.get_root_config();
-
let stdout = io::stdout();
let mut handle = stdout.lock();
+ write!(handle, "{}", get_prompt(context)).unwrap();
+}
+
+pub fn get_prompt(context: Context) -> String {
+ let config = context.config.get_root_config();
+ let mut buf = String::new();
// Write a new line before the prompt
if config.add_newline {
- writeln!(handle).unwrap();
+ writeln!(buf).unwrap();
}
let mut prompt_order: Vec<&str> = Vec::new();
@@ -48,13 +53,15 @@ pub fn prompt(args: ArgMatches) {
// Skip printing the prefix of a module after the line_break
if print_without_prefix {
let module_without_prefix = module.to_string_without_prefix();
- write!(handle, "{}", module_without_prefix).unwrap()
+ write!(buf, "{}", module_without_prefix).unwrap()
} else {
- write!(handle, "{}", module).unwrap();
+ write!(buf, "{}", module).unwrap();
}
print_without_prefix = module.get_name() == "line_break"
}
+
+ buf
}
pub fn module(module_name: &str, args: ArgMatches) {