Refactor segments into modules (#40)

This commit is contained in:
Matan Kushner
2019-05-01 16:34:24 -04:00
committed by GitHub
parent d945b03093
commit c6ee5c6ac1
18 changed files with 423 additions and 278 deletions
+16 -6
View File
@@ -2,6 +2,7 @@ use clap::ArgMatches;
use std::io::{self, Write};
use crate::context::Context;
use crate::module::Module;
use crate::modules;
pub fn prompt(args: ArgMatches) {
@@ -27,11 +28,20 @@ pub fn prompt(args: ArgMatches) {
// Write a new line before the prompt
writeln!(handle).unwrap();
prompt_order
let modules = prompt_order
.iter()
.map(|module| modules::handle(module, &context)) // Compute segments
.flatten() // Remove segments set to `None`
.enumerate() // Turn segment into tuple with index
.map(|(index, segment)| segment.output_index(index)) // Generate string outputs
.for_each(|segment_string| write!(handle, "{}", segment_string).unwrap());
.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());
}