Skip to content

Instantly share code, notes, and snippets.

Make PowerShell Readable on a White Windows Terminal Background

When you switch Windows Terminal to a light or white background, the default PowerShell token colors can become hard to read. You can fix this by customizing PSReadLine’s color scheme so that prompts, selections, and syntax tokens use higher-contrast tones.

Below is a minimal configuration that keeps most text at the terminal’s default foreground color, uses a dim gray for numbers, and highlights selections in an inverted style. It also gives commands a warm accent for visibility.

# Run in PowerShell (requires PSReadLine)
Import-Module PSReadLine
Set-PSReadLineOption -Colors @{

A Tiny Windows Helper: Skipping argv[0] Without the CRT

If you’re writing ultra-small Windows utilities—like tiny launchers or wrappers—you may want to avoid both the C runtime (CRT) and the C++ runtime. That means no printf, no strlen, and no mainCRTStartup scaffolding. The snippet below shows a practical pattern: advance a command-line pointer past argv[0] and then print the progressively “shifted” command line, all using only Win32 APIs.

What the code does

  1. ShiftCmdline (no CRT / no C++ runtime): Walks through the process command line returned by GetCommandLineA() and returns a pointer to the first character of the next token after argv[0]. It accurately follows Windows quoting rules:

    • Tracks whether we’re inside a double-quoted section.

A Tiny ArgvToCommandLine for Windows Without the CRT

This snippet shows how to build a Python-style list2cmdline on Windows—without relying on the C/C++ runtime (CRT). It constructs a single command-line string from an argv array, following the same quoting and escaping rules used by Python’s subprocess.list2cmdline and the Windows command-line parser.


What the code does

  1. Stays CRT-free. The code compiles with -nostdlib style flags because it only uses Win32 APIs (LocalAlloc, LocalFree, WriteFile) and tiny helper functions:

CommandLineToArgvAEx: an ANSI-Compatible Command-Line Parser in Windows

When writing Windows applications in C, it’s common to rely on the CommandLineToArgvW function to split the command line into individual arguments. However, this function only works with wide-character (UTF-16) strings. If your application handles ANSI or UTF-8 command lines, you need a compatible bridge. This article demonstrates how to implement CommandLineToArgvAEx, an ANSI-aware version that supports arbitrary code pages.


Overview

CommandLineToArgvAEx converts an ANSI (or UTF-8) command line into a list of arguments (argv[]) by:

A Tiny CRT-Free Windows “Wrapper” Launcher (MSYS-friendly)

This article walks through a minimal Windows launcher executable—useful as a wrapper that forwards your process’s command line to a target program after adjusting environment variables (e.g., MSYS2/MSYS paths), while avoiding any dependency on the C/C++ runtime (CRT). The code uses only Win32 APIs and a hand-rolled zero-fill to keep the binary lean and linkable with -nostdlib.

What This Launcher Does

  • Skips argv[0]: Parses the raw process command line (GetCommandLineA) and returns a pointer to the first real argument. This means you can invoke the wrapper as wrapper.exe git status and have the child process receive just git status.
  • MSYS-centric PATH: Prepends a curated MSYS2/MSYS path segment to the existing PATH so tools like bash, perl, or git resolve as expected.
  • Environment hint: Sets MSYSTEM=MSYS to guide MSYS’s runtime behavior.
  • Ctrl-C handling: Installs a console control handler that *ignores

Applying Codex-Generated Patches on Windows (MSYS2)

When Codex proposes code changes, it can output them in two handy formats: a patch (diff) or a shell snippet that runs git apply. On Windows with MSYS2, the clipboard and character encodings can get in the way—especially if your system locale uses CP932 (Shift_JIS). Below are two simple one-liners you can run in MSYS2 to apply those changes safely.

1) Apply changes from “Copy patch” (a raw diff)

Use this when you clicked “Copy patch” in Codex and your clipboard now contains a unified diff:

msys2$ powershell.exe Get-Clipboard | iconv -f CP932 -t UTF-8 | tr -d '\r' | git apply -v --3way -

Prevent Accidental Cross-Account Pushes with a Git Hook

If you work with multiple Git identities (e.g., company and personal), it’s easy to commit with one account and accidentally push from another. The simple pre-push hook below helps catch that mistake by checking your recent commits against a list of “my accounts.” If it finds overlaps, it blocks the push and shows you the matches.

What the Hook Does

  • Reads a list of your own identities (name + email) from ~/.config/git/hooks/my-accounts.
  • Scans recent commits in the current repository for author lines (%an <%ae>).
  • Normalizes and deduplicates both lists, then computes the intersection.
  • If two or more identities overlap, it fails the push (helps catch cross-account mixing).

Splitting a PDF into Left and Right Halves Using Python

Managing and manipulating PDF files is a common task in document processing. Sometimes, you may need to divide each page of a PDF into two halves—such as separating the left and right sides of scanned book pages. The following Python script provides a simple and efficient way to accomplish this using the pypdf library.


Overview

The script, named split_pdf_halves.py, reads a PDF file and outputs a new one where each page is split into two separate pages: the left half and the right half. It handles both portrait and landscape orientations, as well as page rotation information, ensuring that the resulting pages maintain correct alignment.

Use System Packages in a Virtualenv

To let a virtual environment see globally installed packages, create it with:

python3 -m venv --system-site-packages venv

Why use it?