geek out on Monospace

Beautiful Code

Any language Shiki supports takes the kit's code colors, so these five are only examples.

JavaScript

stations.js
// Find the stations playing right now
import { stations } from './data.js';

const MAX_RESULTS = 12;

export async function liveStations(genre = 'jazz') {
  const live = stations.filter((s) => s.online && s.genres.includes(genre));
  return live.slice(0, MAX_RESULTS).map(({ name, city }) => `${name}, ${city}`);
}

Bash

deploy.sh
#!/usr/bin/env bash
# Build the site and stop at the first error
set -euo pipefail

SITE_DIR="demo"
cd "$SITE_DIR"

if [[ -f .nvmrc ]]; then
  nvm use
fi

npm ci && npm run build
echo "Built $(find dist -type f | wc -l) files"

Python

tracks.py
"""Count how often each composer was played."""
from collections import Counter
from dataclasses import dataclass


@dataclass
class Track:
    title: str
    composer: str | None = None
    seconds: int = 0


def top_composers(tracks: list[Track], limit: int = 5) -> list[tuple[str, int]]:
    # Tracks with no composer are left out
    counts = Counter(t.composer for t in tracks if t.composer)
    return counts.most_common(limit)

Java

Playlist.java
package com.example.radio;

import java.util.List;

/** A list of tracks with a total running time. */
public record Playlist(String name, List<Track> tracks) {
    private static final int SECONDS_PER_MINUTE = 60;

    public int totalMinutes() {
        int seconds = tracks.stream().mapToInt(Track::seconds).sum();
        return seconds / SECONDS_PER_MINUTE;
    }

    @Override
    public String toString() {
        return "%s (%d min)".formatted(name, totalMinutes());
    }
}

C#

NowPlaying.cs
using System.Net.Http.Json;

namespace Radio;

// Reads the current title for a station
public class NowPlaying(HttpClient http)
{
    private const int TimeoutSeconds = 5;

    public async Task<string?> GetTitleAsync(string stationId)
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeoutSeconds));
        var reply = await http.GetFromJsonAsync<Reply>($"/api/now-playing?station={stationId}", cts.Token);
        return reply?.Title;
    }

    private record Reply(string? Title);
}