Skip to content

Commit 9982f60

Browse files
committed
Allow for choosing an outputter at runtime
1 parent 19a1070 commit 9982f60

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

modules/src/modules/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<T: fmt::Display> Module<T> {
8989
}
9090
}
9191

92-
pub fn output<O: Outputter>(&self, outputter: O) -> String {
92+
pub fn output(&self, outputter: &Box<dyn Outputter>) -> String {
9393
let icon = self.icon.0.map(|icon| icon.to_string()).unwrap_or_default();
9494
let style = self.style.0;
9595
format!(

modules/src/outputter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TODO: Should they stay as independent structs or be one enum?
2+
13
use crate::{Color, Style};
24

35
pub trait Outputter {
@@ -60,3 +62,11 @@ impl Outputter for TtyOutputter {
6062
ansi_style.prefix().to_string()
6163
}
6264
}
65+
66+
pub fn new_outputter(str: &str) -> Option<Box<dyn Outputter>> {
67+
match str.to_lowercase().as_str() {
68+
"tty" | "terminal" | "term" => Some(Box::new(TtyOutputter)),
69+
"tmux" => Some(Box::new(TmuxOutputter)),
70+
_ => None,
71+
}
72+
}

src/main.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod config;
33
use itertools::Itertools;
44
use modules::{
55
modules::ToModule,
6-
outputter::{self, Outputter},
6+
outputter::{self, Outputter, TmuxOutputter},
77
Module, Style,
88
};
99
use std::{
@@ -23,17 +23,12 @@ struct CacheEntry<'a> {
2323

2424
struct Cache<'a>(Vec<CacheEntry<'a>>);
2525

26-
#[cfg(not(debug_assertions))]
27-
const OUTPUTTER: outputter::TmuxOutputter = outputter::TmuxOutputter;
28-
#[cfg(debug_assertions)]
29-
const OUTPUTTER: outputter::TtyOutputter = outputter::TtyOutputter;
30-
3126
impl<'a> Cache<'a> {
3227
fn new(entries: Vec<CacheEntry<'a>>) -> Self {
3328
Self(entries)
3429
}
3530

36-
fn update(self) -> (Self, bool) {
31+
fn update(self, outputter: &Box<dyn Outputter>) -> (Self, bool) {
3732
let now = Instant::now();
3833
let mut rerender = false;
3934

@@ -55,7 +50,7 @@ impl<'a> Cache<'a> {
5550
.module
5651
.internal_set_mut_style(entry.module.content.style());
5752
entry.rerender_interval = entry.module.content.next_render_time();
58-
entry.cached_str = entry.module.output(OUTPUTTER);
53+
entry.cached_str = entry.module.output(outputter);
5954
entry.include = entry.module.content.include();
6055
}
6156
entry
@@ -77,12 +72,15 @@ impl From<&Cache<'_>> for String {
7772
}
7873

7974
fn main() {
75+
let outputter_arg = std::env::args().nth(1);
76+
let outputter = outputter_arg.and_then(|s| outputter::new_outputter(&s)).unwrap_or(Box::new(TmuxOutputter));
77+
8078
let mods = config::get_modules();
8179
let cached_modules: Vec<_> = mods
8280
.iter()
8381
.map(|module| {
8482
(
85-
module.output(OUTPUTTER),
83+
module.output(&outputter),
8684
module.content.next_render_time(),
8785
module.content.include(),
8886
)
@@ -109,7 +107,7 @@ fn main() {
109107
loop {
110108
let now = Instant::now();
111109

112-
let updated = cache.update();
110+
let updated = cache.update(&outputter);
113111
cache = updated.0;
114112

115113
if updated.1 || !first_render {
@@ -119,7 +117,7 @@ fn main() {
119117
config::PRE_MODULES,
120118
String::from(&cache),
121119
config::POST_MODULES,
122-
OUTPUTTER.prefix(Style::default())
120+
outputter.prefix(Style::default())
123121
);
124122
}
125123

0 commit comments

Comments
 (0)