Typstify /tictactoe endpoint

This commit is contained in:
Joscha 2025-03-01 22:57:02 +01:00
parent d32be913fd
commit 8bece23baf
8 changed files with 64 additions and 84 deletions

View file

@ -5,6 +5,7 @@ pub mod cells;
pub mod egg; pub mod egg;
pub mod image; pub mod image;
pub mod text; pub mod text;
pub mod tictactoe;
fn typst_with_lib() -> Typst { fn typst_with_lib() -> Typst {
Typst::new() Typst::new()

View file

@ -0,0 +1,3 @@
{
"feed": false
}

View file

@ -0,0 +1 @@
../lib

View file

@ -0,0 +1,20 @@
#import "@preview/cetz:0.3.2"
#import "lib/main.typ" as lib;
#show: it => lib.init(it)
#let data = json("data.json")
#cetz.canvas(
length: lib.width,
{
import cetz.draw: *
line((1 / 3, 0), (1 / 3, 1), stroke: 4pt)
line((2 / 3, 0), (2 / 3, 1), stroke: 4pt)
line((0, 1 / 3), (1, 1 / 3), stroke: 4pt)
line((0, 2 / 3), (1, 2 / 3), stroke: 4pt)
},
)
#if data.feed {
lib.feed
}

View file

@ -0,0 +1,34 @@
use axum::{Form, extract::State};
use serde::{Deserialize, Serialize};
use crate::{
drawer::{Command, NewTypstDrawing},
server::{Server, somehow},
};
#[derive(Serialize)]
struct Data {
feed: bool,
}
#[derive(Deserialize)]
pub struct FormData {
pub feed: Option<bool>,
}
pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let data = Data {
feed: form.feed.unwrap_or(true),
};
let typst = super::typst_with_lib()
.with_json("/data.json", &data)
.with_main_file(include_str!("main.typ"));
let _ = server
.tx
.send(Command::draw(NewTypstDrawing::new(typst)))
.await;
Ok(())
}

View file

@ -1,7 +1,6 @@
mod backlog; mod backlog;
mod chat_message; mod chat_message;
mod new_typst; mod new_typst;
mod tictactoe;
use showbits_common::widgets::{FontStuff, HasFontStuff}; use showbits_common::widgets::{FontStuff, HasFontStuff};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -10,11 +9,8 @@ use crate::persistent_printer::PersistentPrinter;
pub use self::{ pub use self::{
backlog::BacklogDrawing, chat_message::ChatMessageDrawing, new_typst::NewTypstDrawing, backlog::BacklogDrawing, chat_message::ChatMessageDrawing, new_typst::NewTypstDrawing,
tictactoe::TicTacToeDrawing,
}; };
pub const FEED: f32 = 96.0;
#[derive(Default)] #[derive(Default)]
pub struct Context { pub struct Context {
font_stuff: FontStuff, font_stuff: FontStuff,

View file

@ -1,72 +0,0 @@
use showbits_common::{
Node, Tree, WidgetExt,
color::{BLACK, WHITE},
widgets::{Block, Text},
};
use taffy::{
AlignItems, Display, FlexDirection,
style_helpers::{length, percent, repeat},
};
use crate::persistent_printer::PersistentPrinter;
use super::{Context, Drawing, FEED};
pub struct TicTacToeDrawing;
impl Drawing for TicTacToeDrawing {
fn draw(&self, printer: &mut PersistentPrinter, ctx: &mut Context) -> anyhow::Result<()> {
let block_size = length(128.0);
let width = length(2.0);
let mut tree = Tree::<Context>::new(WHITE);
let mut grid = Node::empty()
.with_display(Display::Grid)
.with_grid_template_columns(vec![repeat(3, vec![block_size])])
.with_grid_auto_rows(vec![block_size]);
for y in 0..3 {
for x in 0..3 {
let mut block = Block::new().with_border(BLACK).node();
if x >= 1 {
block = block.with_border_left(width);
}
if x <= 1 {
block = block.with_border_right(width);
}
if y >= 1 {
block = block.with_border_top(width);
}
if y <= 1 {
block = block.with_border_bottom(width);
}
grid = grid.and_child(block.register(&mut tree)?);
}
}
let title = Text::new()
.with_metrics(Text::default_metrics().scale(2.0))
.and_plain("Tick-Tack-Zeh")
.widget(&mut ctx.font_stuff)
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.with_padding_bottom(length(FEED))
.with_display(Display::Flex)
.with_flex_direction(FlexDirection::Column)
.with_align_items(Some(AlignItems::Center))
.with_gap(length(16.0))
.and_child(title)
.and_child(grid.register(&mut tree)?)
.register(&mut tree)?;
printer.print_tree(&mut tree, ctx, root)?;
Ok(())
}
}

View file

@ -12,7 +12,7 @@ use tokio::{net::TcpListener, sync::mpsc};
use crate::{ use crate::{
documents, documents,
drawer::{ChatMessageDrawing, Command, TicTacToeDrawing}, drawer::{ChatMessageDrawing, Command},
}; };
use self::r#static::get_static_file; use self::r#static::get_static_file;
@ -42,7 +42,10 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
"/text", "/text",
post(documents::text::post).fallback(get_static_file), post(documents::text::post).fallback(get_static_file),
) )
.route("/tictactoe", post(post_tictactoe)) .route(
"/tictactoe",
post(documents::tictactoe::post).fallback(get_static_file),
)
.fallback(get(get_static_file)) .fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
.with_state(Server { tx }); .with_state(Server { tx });
@ -69,9 +72,3 @@ async fn post_chat_message(server: State<Server>, request: Form<PostChatMessageF
})) }))
.await; .await;
} }
// /tictactoe
async fn post_tictactoe(server: State<Server>) {
let _ = server.tx.send(Command::draw(TicTacToeDrawing)).await;
}