summaryrefslogtreecommitdiff
path: root/src/config/theme/colorpair.rs
blob: 77e6af440034cff3bdc0e8ce3c62a36077af9e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::str::FromStr;

use mlua::{FromLua, IntoLua};
use ratatui::style::{Color, Styled, Stylize};

#[derive(Clone)]
pub struct ColorPair {
    pub fg: Color,
    pub bg: Color,
}

impl ColorPair {
    pub const fn new(fg: Color, bg: Color) -> Self {
        ColorPair { bg, fg }
    }

    pub fn apply<S, I>(&self, s: S) -> <I as Styled>::Item
    where
        S: Styled<Item = I>,
        I: Styled,
    {
        s.bg(self.bg).fg(self.fg)
    }
}

impl<'lua> FromLua<'lua> for ColorPair {
    fn from_lua(
        value: mlua::prelude::LuaValue<'lua>,
        _lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<Self> {
        if value.is_table() {
            let table = value.as_table().unwrap();
            if let Ok(fg) = table.get::<_, LuaColor>(1) {
                if let Ok(bg) = table.get::<_, LuaColor>(2) {
                    return Ok(ColorPair::new(fg.into(), bg.into()));
                }
            } else if let Ok(fg) = table.get::<_, LuaColor>("fg") {
                if let Ok(bg) = table.get::<_, LuaColor>("bg") {
                    return Ok(ColorPair::new(fg.into(), bg.into()));
                }
            }
        }

        return Err(mlua::Error::runtime("could not parse colorpair"));
    }
}

impl<'lua> IntoLua<'lua> for ColorPair {
    fn into_lua(
        self,
        lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<mlua::prelude::LuaValue<'lua>> {
        let table = lua.create_table()?;
        table.set("fg", LuaColor::from(self.fg))?;
        table.set("bg", LuaColor::from(self.bg))?;
        Ok(table.into_lua(lua)?)
    }
}

struct LuaColor {
    color: Color,
}

impl LuaColor {
    fn new(color: Color) -> Self {
        Self { color }
    }
}

impl From<Color> for LuaColor {
    fn from(value: Color) -> Self {
        LuaColor::new(value)
    }
}

impl Into<Color> for LuaColor {
    fn into(self) -> Color {
        self.color
    }
}

impl<'lua> FromLua<'lua> for LuaColor {
    fn from_lua(
        value: mlua::prelude::LuaValue<'lua>,
        _lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<Self> {
        if value.is_string() {
            let str = value.as_str().unwrap();
            match Color::from_str(str) {
                Ok(color) => Ok(color.into()),
                Err(_) => Err(mlua::Error::runtime("color has wrong format")),
            }
        } else {
            Err(mlua::Error::runtime("color has wrong format"))
        }
    }
}

impl<'lua> IntoLua<'lua> for LuaColor {
    fn into_lua(
        self,
        lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<mlua::prelude::LuaValue<'lua>> {
        Ok(self.color.to_string().to_lowercase().into_lua(lua)?)
    }
}