跪拜 Guibai
← All articles
Backend

BossKey-Stock Puts Live Market Data in Your Terminal, Then Disguises It as a Docker Build

By 寒蝉128 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The tool solves a specific social-signaling problem that many developers recognize but few tools address: terminal-native workflows make window-switching conspicuous, and a boss key that renders plausible build output eliminates the ambient judgment of a screen full of red and green numbers. The `termios` + `select` pattern for single-threaded, non-blocking keyboard input alongside Rich's Live display is reusable for any terminal app that needs both real-time updates and instant key responses without pulling in curses.

Summary

BossKey-Stock is a Python terminal tool that fetches live Chinese stock data from Sina Finance's free, no-key HTTP endpoint and renders it in a Rich Live table. Pressing `b` instantly replaces the price table with a fake Docker build log — complete with step numbers, occasional warnings, and a running timer — so anyone glancing at the screen sees a routine build, not a portfolio. The whole thing runs in a single thread using `termios` for raw keyboard input and `select` for non-blocking reads, avoiding conflicts with Rich's alternate-screen rendering.

The data layer handles Shanghai/Shenzhen code prefixes, converts share counts to lot-based display units, detects trading hours to skip idle polling, and preserves the last successful fetch when the network drops. Configuration lives in a TOML file, and installation is a single `pip install` away. The 500-line codebase splits cleanly into CLI, main loop, data fetching, boss-mode rendering, and config modules.

Underneath the technical choices is a sharper observation: the social friction of checking stocks at work isn't about the act itself, but about the public signal it sends. A terminal-native tool with a boss key removes that signal without changing the behavior.

Takeaways
BossKey-Stock pulls live A-share quotes from Sina Finance's free HTTP endpoint — no API key, no registration, just a GET with a Referer header.
Pressing `b` toggles a boss mode that renders a fake Docker build log with step numbers, occasional WARNING lines, and a running timer.
Stock codes starting with 6 or 9 map to the Shanghai exchange (`sh` prefix); all others map to Shenzhen (`sz`).
Sina returns volume in shares; the tool converts to lots (1 lot = 100 shares) and formats large numbers with 万/亿 units.
The main loop uses `termios` to disable canonical mode, echo, and signals on the input side while leaving output processing untouched, avoiding conflicts with Rich's rendering.
A `select` call with a 0.5-second timeout lets the single-threaded loop handle both keyboard input and timed data refreshes without background threads or locks.
The `ICRNL` termios flag must be explicitly cleared, or Enter (`\r`) gets converted to `\n` on some terminal emulators, breaking key detection.
Outside trading hours (Mon–Fri, 9:30–11:30 and 13:00–15:00 Beijing time), the display shows `[After Hours]` and the last trade timestamp.
On network failure, the tool shows a yellow `[Offline]` indicator and retains the last successfully fetched data rather than clearing the table.
Installation is `pip install bosskey-stock`; it requires Python 3.10+ and reads configuration from `~/.bosskey.toml`.
Conclusions

The core problem isn't technical — it's that switching to a stock app in an open-plan office broadcasts a negative social signal, and a terminal-native tool with a plausible disguise removes that signal without changing the underlying behavior.

Rich's Live component and curses both want control of the terminal, but the `termios` approach sidesteps the conflict entirely by only reconfiguring the input side, leaving Rich's output rendering untouched.

A single-threaded loop with `select` and a sub-second timeout is often a simpler, more debuggable alternative to threading or asyncio for terminal apps that need both real-time UI updates and instant keyboard response.

The boss-key concept — disguising one application as another — is decades old (DOS games faking Lotus 1-2-3), but applying it to a terminal stock ticker with a Docker build log is a clever modern remix that exploits the ambient plausibility of build output in a dev workspace.

Concepts & terms
Boss key
A keyboard shortcut that instantly hides or disguises the current application, historically used to conceal games as spreadsheets during work hours. Here, it swaps a stock ticker for a fake Docker build log.
Rich Live
A component in Python's Rich library that maintains a dynamically updating region in the terminal using the alternate screen buffer — the same mechanism `vim` and `top` use — restoring previous terminal content on exit.
termios ICANON / ECHO / ICRNL
Terminal input flags on Unix. ICANON enables line-buffered input (waits for Enter); disabling it gives per-key reads. ECHO prints typed characters back. ICRNL converts carriage returns to newlines and must be cleared to detect the Enter key correctly.
Sina Finance API
A free, unauthenticated HTTP endpoint (`hq.sinajs.cn`) that returns real-time Chinese stock quotes as GBK-encoded JavaScript variable assignments. Requires only a Referer header pointing to Sina Finance.
From the discussion
Featured comments
向新出发叭

Haha this is brilliant, the need to 'press the b key to pretend you're compiling' is practically a hard requirement 😂 I used to sneak peeks at the market in my browser too, but switching windows was way too obvious. After my boss caught me twice, I learned my lesson. Your approach of using termios + select for non-blocking monitoring is clever, much lighter than curses, and Rich's table rendering really does look good. I've also stepped on that GBK decoding pitfall; Sina's API returns data with inconsistent encoding. A small suggestion: if you want to support Hong Kong or US stocks later, you could try akshare as a data source. Its API stability is better than Sina's, and you wouldn't have to handle encoding issues yourself. Also, the choice of TOML for configuration is great, much more readable than JSON and it supports comments. Starred, looking forward to a custom stock grouping feature later~

寒蝉128

Haha, same feeling. Just added a position cost/profit feature, and more features will be added later [grin]

寒蝉128

I also considered using akshare, but I haven't seen stability issues with Sina yet. Supporting multiple data source rotations could be an option later~

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗