Skip to content

Commit

Permalink
multi color asciis
Browse files Browse the repository at this point in the history
  • Loading branch information
xynxynxyn committed Dec 14, 2018
1 parent 0b7297a commit 1fc91ba
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 71 deletions.
44 changes: 22 additions & 22 deletions resources/c.ascii
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

`-/++/-`
`.:++++++++++:.`
.-/++++++++++++++++/-.
`-:++++++++++++++++++++++++:-`
.:/++++++++++++++++++++++++++++/:-.`
-++++++++++++++/:--...-:/++++++/:::::::.
++++++++++++/. ./+/::::::::::
+++++++++++. `:::::::::::
++++++++++` -/+++:` `::::::::::
+++++++++. /++++++:``````-:::::::::
+++++++++ `++++/:::::::::::::::::::
+++++++++ `++/:::::::::::::::::::::
+++++++++` ::::::::......::::::::::
+++++++++/ -:::::. `::::::::::
++++++++++/` `` -::::::::::
++++++++++/:.` .::::::::::::
-+++++++/::::::-.``````..-:::::::::::::.
`-/++/:::::::::::::::::::::::::::::-.
`.-::::::::::::::::::::::::-.`
.-::::::::::::::::::-.
`.-::::::::::-.`
`-::::-`
{0} `-/++/-`
{0} `.:++++++++++:.`
{0} .-/++++++++++++++++/-.
{0} `-:++++++++++++++++++++++++:-`
{0} .:/++++++++++++++++++++++++++++/:-.`
{0}-++++++++++++++/:--...-:/++++++/{1}:::::::.
{0}++++++++++++/. ./+/{1}::::::::::
{0}+++++++++++. `{1}:::::::::::
{0}++++++++++` -/+++:` {1}`::::::::::
{0}+++++++++. /++++++{1}:``````-:::::::::
{0}+++++++++ `++++/{1}:::::::::::::::::::
{0}+++++++++ `++/{1}:::::::::::::::::::::
{0}+++++++++` {1}::::::::......::::::::::
{0}+++++++++/ {1} -:::::. `::::::::::
{0}++++++++++/` {1} `` -::::::::::
{0}++++++++++/{1}:.` .::::::::::::
{0}-+++++++/{1}::::::-.``````..-:::::::::::::.
{0} `-/++/{1}:::::::::::::::::::::::::::::-.
{0} {1}`.-::::::::::::::::::::::::-.`
{0} {1} .-::::::::::::::::::-.
{0} {1} `.-::::::::::-.`
{0} {1} `-::::-`
41 changes: 20 additions & 21 deletions resources/rust.ascii
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@

` :y.`yy`.y: `
-``MNsNMMNNNNMMNsNM``-
` -MMNMMMMNNm``NNNMMMMNMM- `
`NNNMMMdo:` `+md/ `:odMMMNNN`
-ssNMMNo. .oNMMNss-
`mMMMMNmmmmmmmmmmmmmmmdy+` `sMMMm`
`mMMMMMMMMMMMMMMMMMMMMMMMMMN/ hMMMMm`
-oMN-:Ny:mMMMMMm oNMMMMMm oN::MMo-
.yMMMhhh+ dMMMMMd:::::+mMMMMN/ odyhMMMy.
-sNMMy dMMMMMMMMMMMMMMMMs` `yMMNs-
-sNMMy dMMMMMNyyyydMMMMMMy .odMMNs-
.yMMMm dMMMMMh +MMMMMM+ sMMMMMy.
-oMMMMMMMMMMMMMMMMM+ mMMMMMMMMMMMMMo-
`mMMMMMMMMMMMMMMMMM+ :NMMMMMMMMMMMMm`
`mMMMm `-:o+:/mMMMm`
-ssNMMMyomo smohMMMNss-
`NNNMs+mN/-` `-/Nd/yMNNN`
` -MMNMMMMMNmmmmNMMMMMNMM- `
-``MNsNMMNMMNMMNsNM``-
` :y.`yy`.y: `
{0} ` :y.`yy`.y: `
{0} -``MNsNMMNNNNMMNsNM``-
{0} ` -MMNMMMMNNm``NNNMMMMNMM- `
{0} `NNNMMMdo:` `+md/ `:odMMMNNN`
{0} -ssNMMNo. .oNMMNss-
{0} `mMM{1}MMNmmmmmmmmmmmmmmmdy+`{0} `sMMMm`
{0} `mMMM{1}MMMMMMMMMMMMMMMMMMMMMMN/{0} hMMMMm`
{0} -oMN-:Ny:{1}mMMMMMm oNMMMMMm{0} oN::MMo-
{0}.yMMMhhh+ {1}dMMMMMd:::::+mMMMMN/{0} odyhMMMy.
{0}-sNMMy {1}dMMMMMMMMMMMMMMMMs`{0} `yMMNs-
{0}-sNMMy {1}dMMMMMNyyyydMMMMMMy{0} .odMMNs-
{0}.yMMMm {1}dMMMMMh +MMMMMM+{0} sMMMMMy.
{0} -oMMM{1}MMMMMMMMMMMMMM+ mMMMMMMMMMM{0}MMMo-
{0} `mMMM{1}MMMMMMMMMMMMMM+ :NMMMMMMMMM{0}MMMm`
{0} `mMMMm `-:o+:/mMMMm`
{0} -ssNMMMyomo smohMMMNss-
{0} `NNNMs+mN/-` `-/Nd/yMNNN`
{0} ` -MMNMMMMMNmmmmNMMMMMNMM- `
{0} -``MNsNMMNMMNMMNsNM``-
{0} ` :y.`yy`.y: `
117 changes: 89 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ struct Info {
impl fmt::Display for Info {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buffer = String::new();
let color = self.color();
let color = match self.colors().get(0) {
Some(&c) => c,
None => Color::White,
};

writeln!(
buffer,
Expand Down Expand Up @@ -79,27 +82,85 @@ impl fmt::Display for Info {

let logo = self.get_ascii();
let mut lines = buffer.lines();
let left_pad = logo.lines().map(|l| l.len()).max().unwrap_or(0);
let left_pad = logo.lines().map(|l| true_len(l)).max().unwrap_or(0);

for a in logo.lines() {
let b = match lines.next() {
for logo_line in logo.lines() {
let info_line = match lines.next() {
Some(line) => line,
None => "",
};

writeln!(
f,
"{:width$} {}",
a.color(color).bold(),
b,
width = left_pad
)?;
let (logo_line, extra_pad) = colorize_str(logo_line, self.colors());
// If the string is empty the extra padding should not be added
let pad = if logo_line.is_empty() {
left_pad
} else {
left_pad + extra_pad
};
writeln!(f, "{:<width$} {:^}", logo_line, info_line, width = pad,)?;
}

Ok(())
}
}

/// Transforms a string with color format into one with proper
/// escape characters for color display.
///
/// Colors are specified with {0}, {1}... where the number represents
/// the nth element in the colors Vec provided to the function.
/// If there are more colors in the ascii than in the Vec it
/// defaults to white.
/// The usize in the tuple refers to the extra padding needed
/// which comes from the added escape characters.
fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
// Extract colors from string coded with {n}
let mut colors_in_str: Vec<Color> = line.split("{").fold(Vec::new(), |mut acc, s| {
if s.len() > 2 {
let i = s.chars().nth(0).unwrap_or('0').to_digit(10).unwrap_or(0);
acc.push(*colors.iter().nth(i as usize).unwrap_or(&Color::White));
}
acc
});

if colors_in_str.is_empty() {
colors_in_str.push(match colors.get(0) {
Some(&c) => c,
None => Color::White,
});
}

let mut colors_iter = colors_in_str.iter();

let out_str = line.split("{").fold(String::new(), |mut acc, s| {
if s.len() > 2 {
let s: String = s.chars().skip(2).collect();
let c = match colors_iter.next() {
Some(&c) => c,
None => Color::White,
};
acc.push_str(&format!("{}", s.color(c)));
}
acc
});
(out_str, colors_in_str.len() * 9)
}

/// Returns the true length of a string after substracting the {n}
/// color declarations.
fn true_len(line: &str) -> usize {
line.split("{")
.fold(String::new(), |mut acc, s| {
if s.len() > 2 {
acc.push_str(&s.chars().skip(2).collect::<String>());
} else {
acc.push_str(s);
}
acc
})
.len()
}

enum Language {
C,
Clojure,
Expand Down Expand Up @@ -378,24 +439,24 @@ impl Info {
}
}

fn color(&self) -> Color {
fn colors(&self) -> Vec<Color> {
match self.language {
Language::C => Color::Cyan,
Language::Clojure => Color::Cyan,
Language::Cpp => Color::Yellow,
Language::Csharp => Color::White,
Language::Go => Color::White,
Language::Haskell => Color::Cyan,
Language::Java => Color::Green,
Language::Lisp => Color::Yellow,
Language::Lua => Color::Blue,
Language::Python => Color::Magenta,
Language::R => Color::Blue,
Language::Ruby => Color::Magenta,
Language::Rust => Color::Cyan,
Language::Scala => Color::Blue,
Language::Shell => Color::Green,
Language::TypeScript => Color::Cyan,
Language::C => vec![Color::BrightBlue, Color::Blue],
Language::Clojure => vec![Color::Cyan],
Language::Cpp => vec![Color::Yellow],
Language::Csharp => vec![Color::White],
Language::Go => vec![Color::White],
Language::Haskell => vec![Color::Cyan],
Language::Java => vec![Color::Green],
Language::Lisp => vec![Color::Yellow],
Language::Lua => vec![Color::Blue],
Language::Python => vec![Color::Magenta],
Language::R => vec![Color::Blue],
Language::Ruby => vec![Color::Magenta],
Language::Rust => vec![Color::White, Color::Red],
Language::Scala => vec![Color::Blue],
Language::Shell => vec![Color::Green],
Language::TypeScript => vec![Color::Cyan],
}
}
}
Expand Down

0 comments on commit 1fc91ba

Please sign in to comment.