consts: store colors as hex codes in struct
This commit is contained in:
parent
1ff95de3bf
commit
a3014f2694
6 changed files with 52 additions and 28 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::{consts, Context};
|
||||
use crate::{consts::Colors, Context};
|
||||
|
||||
use eyre::{eyre, Context as _, OptionExt, Result};
|
||||
use log::trace;
|
||||
|
@ -29,7 +29,7 @@ pub async fn members(ctx: Context<'_>) -> Result<()> {
|
|||
let embed = CreateEmbed::new()
|
||||
.title(format!("{member_count} total members!",))
|
||||
.description(format!("{online_count} online members",))
|
||||
.color(consts::colors()["blue"]);
|
||||
.color(Colors::BLUE);
|
||||
let reply = CreateReply::default().embed(embed);
|
||||
|
||||
ctx.send(reply).await?;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{api, consts, Context};
|
||||
use crate::{api, consts::Colors, Context};
|
||||
|
||||
use eyre::Result;
|
||||
use log::trace;
|
||||
|
@ -27,7 +27,7 @@ pub async fn stars(ctx: Context<'_>) -> Result<()> {
|
|||
|
||||
let embed = CreateEmbed::new()
|
||||
.title(format!("⭐ {count} total stars!"))
|
||||
.color(consts::colors()["yellow"]);
|
||||
.color(Colors::YELLOW);
|
||||
let reply = CreateReply::default().embed(embed);
|
||||
|
||||
ctx.send(reply).await?;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#![allow(non_camel_case_types, clippy::upper_case_acronyms)]
|
||||
use crate::{consts, tags::Tag, Context};
|
||||
use crate::{consts::Colors, tags::Tag, Context};
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use eyre::{eyre, Result};
|
||||
|
@ -40,9 +41,10 @@ pub async fn tag(
|
|||
let mut e = CreateEmbed::new();
|
||||
|
||||
if let Some(color) = &frontmatter.color {
|
||||
let color = *consts::colors()
|
||||
.get(color.as_str())
|
||||
.unwrap_or(&Color::default());
|
||||
let color = Colors::from_str(color.as_str())
|
||||
.map(Color::from)
|
||||
.unwrap_or_default();
|
||||
|
||||
e = e.color(color);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,39 @@
|
|||
use std::{collections::HashMap, sync::OnceLock};
|
||||
#![allow(clippy::unreadable_literal)]
|
||||
use std::str::FromStr;
|
||||
|
||||
use poise::serenity_prelude::Color;
|
||||
use poise::serenity_prelude::Colour;
|
||||
|
||||
pub fn colors() -> &'static HashMap<&'static str, Color> {
|
||||
static COLORS: OnceLock<HashMap<&str, Color>> = OnceLock::new();
|
||||
COLORS.get_or_init(|| {
|
||||
HashMap::from([
|
||||
("red", Color::from((239, 68, 68))),
|
||||
("green", Color::from((34, 197, 94))),
|
||||
("blue", Color::from((96, 165, 250))),
|
||||
("yellow", Color::from((253, 224, 71))),
|
||||
("orange", Color::from((251, 146, 60))),
|
||||
// TODO purple & pink :D
|
||||
])
|
||||
})
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct Colors(i32);
|
||||
|
||||
impl Colors {
|
||||
pub const RED: i32 = 0xEF4444;
|
||||
pub const GREEN: i32 = 0x22C55E;
|
||||
pub const BLUE: i32 = 0x60A5FA;
|
||||
pub const YELLOW: i32 = 0xFDE047;
|
||||
pub const ORANGE: i32 = 0xFB923C;
|
||||
|
||||
pub fn as_i32(self) -> i32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Colors {
|
||||
type Err = ();
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"red" => Ok(Colors(Self::RED)),
|
||||
"green" => Ok(Colors(Self::GREEN)),
|
||||
"blue" => Ok(Colors(Self::BLUE)),
|
||||
"yellow" => Ok(Colors(Self::YELLOW)),
|
||||
"orange" => Ok(Colors(Self::ORANGE)),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Colors> for Colour {
|
||||
fn from(value: Colors) -> Self {
|
||||
Self::from(value.as_i32())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::consts;
|
||||
use crate::Data;
|
||||
use crate::{consts::Colors, Data};
|
||||
|
||||
use std::fmt::Write;
|
||||
|
||||
use eyre::Report;
|
||||
|
@ -34,7 +34,7 @@ pub async fn handle(error: FrameworkError<'_, Data, Report>) {
|
|||
.title("Something went wrong!")
|
||||
.description("oopsie")
|
||||
.timestamp(Timestamp::now())
|
||||
.color(consts::colors()["red"]);
|
||||
.color(Colors::RED);
|
||||
|
||||
let reply = CreateReply::default().embed(embed);
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{consts, Data};
|
||||
use crate::{consts::Colors, Data};
|
||||
|
||||
use eyre::Result;
|
||||
use log::{debug, trace};
|
||||
|
@ -48,10 +48,10 @@ pub async fn handle(ctx: &Context, message: &Message, data: &Data) -> Result<()>
|
|||
|
||||
if issues.is_empty() {
|
||||
e = e
|
||||
.color(consts::colors()["green"])
|
||||
.color(Colors::GREEN)
|
||||
.description("No issues found automatically");
|
||||
} else {
|
||||
e = e.color(consts::colors()["red"]);
|
||||
e = e.color(Colors::RED);
|
||||
|
||||
for (title, description) in issues {
|
||||
e = e.field(title, description, false);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue