smokestack/main: colorize according to language

This commit is contained in:
Astro 2022-11-18 17:24:36 +01:00
parent fa6c9e2b6d
commit a1e94843b4
1 changed files with 27 additions and 2 deletions

View File

@ -5,7 +5,7 @@ use std::{
RwLock,
},
};
use ansi_term::Colour::*;
use ansi_term::Colour::{self, *};
use futures::{Stream, StreamExt};
use tokio::{
io::AsyncWriteExt,
@ -54,6 +54,31 @@ fn html_to_text(html: &str) -> String {
result
}
fn language_colour(language: &str) -> Colour {
let x = language.bytes().fold(0, |x, b| x ^ b);
let b = language.as_bytes();
let y = if b.len() >= 1 {
(b[0] & 0x1F) << 2
} else {
127
};
let z = if b.len() >= 2 {
(b[1] & 0x1F) << 2
} else {
127
};
match x % 6 {
0 => RGB(127 + y, 0, 0),
1 => RGB(0, 127 + z, 0),
2 => RGB(0, 0, 127 + y),
3 => RGB(127 + y, 127 + z, 0),
4 => RGB(127 + y, 0, 127 + z),
5 => RGB(0, 127 + y, 127 + z),
6..=u8::MAX => unreachable!(),
}
}
fn format_message(post: Post) -> Option<String> {
let language = &post.language?;
let time = &post.created_at;
@ -63,7 +88,7 @@ fn format_message(post: Post) -> Option<String> {
let text = html_to_text(&post.content);
Some(format!(
"[{}] {} {} <@{}@{}>\r\n{}\r\n\r\n",
Black.on(Red).paint(language),
Black.on(language_colour(language)).paint(language),
Red.paint(time),
Yellow.bold().paint(display_name),
Yellow.underline().paint(username),