TODAY I LEARNED

by lubosmato

tsx
Pure CSS Auto-Contrast Text Color

You can automatically pick black or white color based on a background color using a only CSS.

The trick abuses relative color syntax in oklch. It extracts the perceptual lightness (l) of the background, subtracts it from a threshold (0.6), and multiplies by infinity. The result is either +infinity (clamped to white) or -infinity (clamped to black). Zero chroma and hue ensure a pure achromatic result.

Perfect for dynamic badges, tags, or any element where the background color is unpredictable. Works in all modern browsers today.

<Badge
  style={{
    backgroundColor: tag.color,
    color: `oklch(from ${tag.color} calc((0.6 - l) * infinity) 0 0)`,
  }}
>
  {tag.name}
</Badge>
css
lubosmato
bash
Live-stream your terminal with asciinema

Did you know asciinema supports real-time terminal streaming? Running asciinema stream -r starts a live stream of your terminal session, giving anyone with the link a real-time view of what you're doing. Perfect for remote pair programming with Neovim or live demos.

https://asciinema.org/

I use Neovim BTW!

 asciinema stream -r         
::: asciinema session started
::: Live streaming at https://asciinema.org/s/BAgxaJcQbsr55T
::: Press <ctrl+d> or type 'exit' to end
bashtool
lubosmato
bash
psql accepts connection strings

psql can accept a full connection URI instead of passing individual flags for host, port, database, and user.

This is especially handy when working with managed database providers (Supabase, Neon, Railway, etc.) since they typically give you a connection string you can copy and paste directly. No more juggling five different flags.

fc -p # commands you run after this won't be saved to your shell history (for current session only)

psql postgres://myuser:mypassword@localhost:5432/mydb
# same as:
psql -h localhost -p 5432 -U myuser -d mydb

fc -P # back to normal
postgresqltool
lubosmato
typescript
Exhaustiveness check using satisfies never in switch

In the default branch, TypeScript narrows proto to whatever union members weren't handled. If all cases are covered, proto becomes never and the check passes silently. If you miss a case (like "raw" here), proto still has a remaining type, so satisfies never produces a compile-time error telling you exactly which variant you forgot to handle.

type Proto = "tcp" | "http1" | "raw"
const proto = "tcp" as Proto

switch (proto) {
  case "tcp":
  case "http1":
    break;
  default:
    proto satisfies never; // <-- Type '"raw"' does not satisfy the expected type 'never'.
}
typescript
lubosmato
sql
PostgreSQL INSERT INTO ... SELECT

This statement lets you insert rows into a table by pulling data from another table (or the same table) using a SELECT query, rather than specifying values manually.

-- Assume both tables have the same structure
INSERT INTO orders_archive (order_id, customer_id, total, order_date)
SELECT order_id, customer_id, total, order_date
FROM orders
WHERE status = 'completed' AND order_date < '2025-01-01';
postgresql
lubosmato