Skip to content

Add --print flag for non-interactive stdout output#59

Open
DavidTeju wants to merge 1 commit intosamtay:mainfrom
DavidTeju:feat/print-to-stdout
Open

Add --print flag for non-interactive stdout output#59
DavidTeju wants to merge 1 commit intosamtay:mainfrom
DavidTeju:feat/print-to-stdout

Conversation

@DavidTeju
Copy link
Copy Markdown

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

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

Add -p/--print flag that prints the top answer to stdout and exits
immediately, without a spinner, keypress prompt, or TUI. This makes
so fully pipeable and usable from scripts and non-TTY environments.

--print implies --lucky (fetches the top answer).
@DavidTeju DavidTeju force-pushed the feat/print-to-stdout branch from 638865e to ecbec54 Compare March 15, 2026 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant