so icon indicating copy to clipboard operation
so copied to clipboard

Add --print flag for non-interactive stdout output

Open DavidTeju opened this issue 8 months ago • 0 comments

Summary

Add -p/--print flag that prints the top answer to stdout and exits immediately — no spinner, no keypress prompt, no TUI. This makes so fully pipeable and usable from scripts, CI, and non-TTY environments (e.g., AI agents, editor plugins).

Motivation

The --lucky flag already fetches the top answer, but it requires a TTY because:

  1. The spinner uses crossterm::terminal::enable_raw_mode()
  2. Term::wait_for_key() blocks for interactive input

This means so --lucky "query" | head fails with Crossterm error: Device not configured (os error 6). The --print flag fixes this by bypassing both the spinner and the interactive prompt.

Usage

# Print answer to stdout (no TTY needed)
so --print "how to exit vim"
so -p "rust reverse string"

# Pipe to other tools
so -p "python list comprehension" | less
so -p "git rebase" 2>/dev/null | pbcopy

# Use from scripts
ANSWER=$(so -p -e stackexchange "bash array" 2>/dev/null)

--print implies --lucky (it always fetches the top-voted answer).

Changes

File Change
src/cli.rs Add -p/--print flag, force lucky mode when print is set
src/main.rs Add print code path: search without spinner, print answer, exit

Test plan

  • [x] cargo test — all 26 tests pass
  • [x] so --print -e startpage "how to exit vim" — prints answer, exits cleanly
  • [x] so -p -e stackexchange "python list comprehension" — works with short flag
  • [x] so -p "query" | head — pipes correctly, no TTY error
  • [x] so --lucky "query" — still works as before (regression)

DavidTeju avatar Mar 15 '26 01:03 DavidTeju