~prokop/lebka

cf1ee994c39c4f90a713f07f05b29b104e5107b6 — Prokop Randacek 1 year, 4 months ago 2a9f4c1
separate out color into its own table
3 files changed, 122 insertions(+), 44 deletions(-)

M index.lua
M src/main.rs
M src/sandbox.rs
M index.lua => index.lua +16 -15
@@ 1,16 1,19 @@
local font = readfont(fetch("/usr/share/fonts/inconsolata/Inconsolata-Bold.otf"))

local text_color = { 238 / 255, 238 / 255, 238 / 255 }
local link_color = { 140 / 255, 194 / 255, 221 / 255, }

function Title(text)
	return Box {
		text = text, font = font,
		fS = 42, fR = 238 / 255, fG = 238 / 255, fB = 238 / 255,
		fS = 42, Fc = text_color,
	}
end

function Link(text)
	return Box {
		text = text, font = font,
		fS = 24, fR = 140 / 255, fG = 194 / 255, fB = 221 / 255,
		fS = 24, Fc = link_color,
		t = 20 * Px,
		r = 40 * Px,
	}


@@ 19,7 22,7 @@ end
function NavItem(text)
	return Box {
		text = text, font = font,
		fS = 24, fR = 140 / 255, fG = 194 / 255, fB = 221 / 255,
		fS = 24, Fc = link_color,
		t = 20 * Px,
		r = 40 * Px,
	}


@@ 28,7 31,7 @@ end
function H1(text)
	return Box {
		text = text, font = font,
		fS = 52, fR = 238 / 255, fG = 238 / 255, fB = 238 / 255,
		fS = 52, Fc = text_color,
		t = 20 * Px,
	}
end


@@ 36,7 39,7 @@ end
function H2(text)
	return Box {
		text = text, font = font,
		fS = 30, fR = 238 / 255, fG = 238 / 255, fB = 238 / 255,
		fS = 30, Fc = text_color,
		t = 20 * Px,
		b = 20 * Px,
	}


@@ 45,14 48,14 @@ end
function P(text)
	return Box {
		text = text, font = font,
		fS = 24, fR = 238 / 255, fG = 238 / 255, fB = 238 / 255,
		fS = 24, Fc = text_color,
		t = 20 * Px,
	}
end

function Hr()
	return Box {
		R = 238 / 255, G = 238 / 255, B = 238 / 255,
		c = { 238 / 255, 238 / 255, 238 / 255 },
		w = 1 * Pct,
		h = 2 * Px,
		t = 20 * Px,


@@ 60,16 63,14 @@ function Hr()
end

return Box {
	w = 1 * Pct,
	h = 1 * Pct,
	w = 1 * Pct, -- width
	h = 1 * Pct, -- height

	R = 34 / 255,
	G = 40 / 255,
	B = 49 / 255,
	c = { 34 / 255, 40 / 255, 49 / 255 }, -- color

	Box { -- content
		t = 40 * Px,
		l = 0.2 * Pct,
		t = 40 * Px, -- top padding
		l = 0.2 * Pct, -- left padding
		w = 0.6 * Pct,

		Title('rdck.dev'), -- header


@@ 78,7 79,7 @@ return Box {
			overflow = "wrap",
			NavItem('Home'), NavItem('Contact'), NavItem('Posts'), NavItem('Fun stuff'), NavItem('Maps'),
		},
		Box {
		Box { -- content
			H1('Hi!'),
			P('My name is Prokop Randáček. I write code, like cats and play Factorio.'),
			Hr(),

M src/main.rs => src/main.rs +4 -3
@@ 3,16 3,17 @@
use std::env;

#[tokio::main]
async fn main() {
async fn main() -> Result<(), lebka::Error> {
    let args: Vec<String> = env::args().collect();

    let url = match args.len() {
        2 => &args[1],
        _ => {
            println!("usage: lebka <url>");
            return;
            return Ok(());
        }
    };

    lebka::run_url(url).await.unwrap();
    lebka::run_url(url).await?;
    Ok(())
}

M src/sandbox.rs => src/sandbox.rs +102 -26
@@ 10,7 10,6 @@ use speedy2d::color::Color;
use speedy2d::font::Font;
use thiserror::Error;

#[derive(Debug)]
pub struct Sandbox {
    lua: Lua,
}


@@ 20,6 19,11 @@ pub struct LuaFont {
    font: Font,
}

#[derive(Clone, Debug)]
pub struct LuaColor {
    color: Color,
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("lua engine returned error")]


@@ 129,6 133,91 @@ impl<'lua> FromLua<'lua> for Overflow {
    }
}

impl<'lua> FromLua<'lua> for LuaColor {
    // supported:
    // - sequence of 3 or 4 numbers all in range 0-1
    // - table with r, g, b or r, g, b, a keys and number values
    // - css style hex string (without the '#' prefix) 3, 4, 6, 8 chars long for 1 or 2 hex digits
    //   per channel with or without alpha
    fn from_lua(lua_value: LuaValue<'lua>, _lua: &'lua Lua) -> LuaResult<Self> {
        match lua_value {
            LuaValue::String(s) => {
                let string = s.to_str()?;
                match string.len() {
                    3 => Ok(LuaColor {
                        color: Color::from_rgb(
                            f32::from(u8::from_str_radix(&string[0..=0], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[1..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=2], 16).to_lua_err()?) / 255.0,
                        ),
                    }),
                    4 => Ok(LuaColor {
                        color: Color::from_rgba(
                            f32::from(u8::from_str_radix(&string[0..=0], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[1..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=2], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[3..=3], 16).to_lua_err()?) / 255.0,
                        ),
                    }),
                    6 => Ok(LuaColor {
                        color: Color::from_rgb(
                            f32::from(u8::from_str_radix(&string[0..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=3], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[4..=5], 16).to_lua_err()?) / 255.0,
                        ),
                    }),
                    8 => Ok(LuaColor {
                        color: Color::from_rgba(
                            f32::from(u8::from_str_radix(&string[0..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=3], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[4..=5], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[6..=7], 16).to_lua_err()?) / 255.0,
                        ),
                    }),
                    _ => Err(LuaError::RuntimeError(
                        "expected string 3, 4, 6 or 8 bytes long".to_string(),
                    )),
                }
            }
            LuaValue::Table(t) => match t.raw_len() {
                0 => match (t.get("r"), t.get("g"), t.get("b"), t.get("a")) {
                    (Ok(r), Ok(g), Ok(b), Ok(a)) => Ok(LuaColor {
                        color: Color::from_rgba(r, g, b, a),
                    }),
                    (Ok(r), Ok(g), Ok(b), Err(_)) => Ok(LuaColor {
                        color: Color::from_rgb(r, g, b),
                    }),
                    _ => Err(LuaError::RuntimeError(
                        "expected r, g, b keys in the table with number values".to_string(),
                    )),
                },
                3 => match (t.get(1), t.get(2), t.get(3)) {
                    (Ok(r), Ok(g), Ok(b)) => Ok(LuaColor {
                        color: Color::from_rgb(r, g, b),
                    }),
                    _ => Err(LuaError::RuntimeError(
                        "Expected sequence of 3 or 4 numbers".to_string(),
                    )),
                },
                4 => match (t.get(1), t.get(2), t.get(3), t.get(4)) {
                    (Ok(r), Ok(g), Ok(b), Ok(a)) => Ok(LuaColor {
                        color: Color::from_rgba(r, g, b, a),
                    }),
                    _ => Err(LuaError::RuntimeError(
                        "Expected sequence of 3 or 4 numbers".to_string(),
                    )),
                },
                _ => Err(LuaError::RuntimeError(
                    "expected sequence of 3 or 4 elements".to_string(),
                )),
            },
            _ => Err(LuaError::RuntimeError(
                "can't convert this type to Color".to_string(),
            )),
        }
    }
}

// Setups a global environment that is not touched by anyone and then loads frames with theid own
// copies of the global environment
impl Sandbox {


@@ 169,12 258,12 @@ impl Sandbox {

        if let Ok(v) = t.get::<&str, String>("text") {
            b.ty = BoxType::Leaf(LeafType::Text {
                color: Color::from_rgba(
                    t.get("fR").unwrap_or_default(),
                    t.get("fG").unwrap_or_default(),
                    t.get("fB").unwrap_or_default(),
                    t.get("fA").unwrap_or(1.0),
                ),
                color: t
                    .get::<&str, LuaColor>("Fc")
                    .unwrap_or(LuaColor {
                        color: Color::WHITE,
                    })
                    .color,
                text: v,
                size: t.get("fS").unwrap_or(48.0),
                font: t


@@ 208,25 297,12 @@ impl Sandbox {
        b.padding_bottom = t.get("b").unwrap_or_default();
        b.padding_right = t.get("r").unwrap_or_default();

        b.background_color = {
            let r = t.get("R");
            let g = t.get("G");
            let b = t.get("B");
            let a = t.get("A");

            // if non of rgba are set, the default trasnparency is 0, else 1
            let a = a.unwrap_or(if r.is_err() && g.is_err() && b.is_err() {
                0.0
            } else {
                1.0
            });

            let r = r.unwrap_or(0.0);
            let g = g.unwrap_or(0.0);
            let b = b.unwrap_or(0.0);

            Color::from_rgba(r, g, b, a)
        };
        b.background_color = t
            .get::<&str, LuaColor>("c")
            .unwrap_or(LuaColor {
                color: Color::TRANSPARENT,
            })
            .color;

        Ok(b)
    }