Marcus Vechiato

Monitoring my UK Commute

I wanted the next train, platform, and a simple signal for problems without opening a web page. This is a SwiftBar/xbar plugin written in Python that queries National Rail via Huxley2, switches routes based on time, and color-codes the menu bar. It also run in a cron on my homelab during the days and windows where I commute to and from Work.

GitHube Repository: https://github.com/vechiato/uk-next-train

What it does

  • Smart commuter mode that switches from home→work in the morning to work→home after a set time
  • Next train focus in the menu bar with a single emoji and color
  • Status-based colors: red for cancelled next train, orange if later trains have issues, green if all clear
  • Optional platform and operator display
  • Works in SwiftBar and xbar on macOS
image

Why a plugin and not a full app

  • Menu bar is low friction and always visible
  • Plugins are simple scripts with stdout as the UI
  • No credentials are required for Huxley2
  • Configuration lives in SwiftBar preferences and environment variables

Data source

Huxley2 exposes National Rail departures with a straightforward REST API. No API keys. Responses are small JSON documents with the next services, platforms, and status.

Commuter mode logic

  • Mode auto: morning shows home→work, afternoon shows work→home based on a switch time
  • Mode manual: fixed route from A to B
  • Defaults: WIM → WAT in the morning, WAT → WIM in the afternoon, switch at 12:00

Installing

# 1) Install SwiftBar or xbar
# SwiftBar: https://swiftbar.app
# xbar: https://xbarapp.com

# 2) Download the plugin
curl -L -o ~/Plugins/uk_next_train.5m.py https://raw.githubusercontent.com/vechiato/uk-next-train/main/uk_next_train.5m.py

# 3) Make it executable
chmod +x ~/Plugins/uk_next_train.5m.py

# 4) Point SwiftBar at your plugins folder
# SwiftBar > Preferences > Plugins Folder > select ~/Plugins

Configuration

You can configure via SwiftBar item preferences or environment variables.

  • Home/Work station codes: WIM and WAT by default
  • Switch time: 12:00 by default
  • Mode: auto or manual
  • Train count: default 3
  • Show platform: default true
  • Show operator: default false

Example using environment variables in SwiftBar:

# In SwiftBar, set these for the plugin item
HOME_STATION=WIM
WORK_STATION=WAT
SWITCH_TIME=12:00
MODE=auto
TRAIN_COUNT=3
SHOW_PLATFORM=true
SHOW_OPERATOR=false

Station codes

Common codes for quick testing:

  • WAT: Waterloo
  • WIM: Wimbledon
  • LBG: London Bridge
  • VIC: Victoria
  • CHX: Charing Cross
  • PAD: Paddington
  • KGX: Kings Cross
  • EUS: Euston
  • LST: Liverpool Street

Output format

SwiftBar/xbar expects a simple text protocol.

  • Top line is the menu bar text, e.g. 🚂 Next: 5m with color attributes
  • --- separates the dropdown menu
  • Each subsequent line represents an item with optional attributes like color=red or font=Monaco

Sample menu:

🚂 Wimbledon → London Waterloo
📍 Mode: Auto (Home→Work) | color=gray
🕐 Updated: 08:51:34 | color=gray
---
✅ 08:54 (2m) Plat 5 to London Waterloo | color=green font=Monaco
⏰ 09:02 (10m) Plat 5 to London Waterloo | color=orange font=Monaco
✅ 09:05 (13m) Plat 5 to London Waterloo | color=green font=Monaco
---

Status and colors

  • Menu bar icon color reflects overall status:
    • Red if the next train is cancelled
    • Orange if any later train has issues
    • Green if all trains are fine
  • Train line icons:
    • ✅ on time
    • ⏰ delayed
    • ❌ cancelled

Core script structure

The plugin is a single Python file that:

  • Reads config from environment or SwiftBar item settings
  • Decides route based on mode and time
  • Calls Huxley2 for departures
  • Computes time-to-departure and a compact status
  • Prints lines in the SwiftBar protocol

Repo

https://github.com/vechiato/uk-next-train