Skip to content

Allow is-terminal to detect redirection - #18443

Merged
fdncred merged 1 commit into
nushell:mainfrom
fdncred:update_is_terminal_with_redirection
Jun 22, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
fdncred:update_is_terminal_with_redirection

Conversation

@fdncred

@fdncred fdncred commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR enables the is-terminal command to detect redirection. The command without any flags defaults to --stdout.

Scenario Before PR After PR
is-terminal --stdout (in terminal) true true
is-terminal --stdout | cmd true  false 
is-terminal --stdout o> file true false 
let x = (is-terminal --stdout) true false 

User-facing changes (Release notes)

The is-terminal command can detect redirection now. It now defaults to --stdout.

Scenario Current
is-terminal (in terminal) true
is-terminal | cmd false 
is-terminal o> file false 
let x = (is-terminal) false 

Additional notes

closes #15368

@fdncred
fdncred merged commit 238ed68 into nushell:main Jun 22, 2026
18 checks passed
@fdncred
fdncred deleted the update_is_terminal_with_redirection branch June 22, 2026 00:21
@github-actions github-actions Bot added this to the v0.114.0 milestone Jun 22, 2026
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Jun 22, 2026
fdncred added a commit that referenced this pull request Jul 18, 2026
…estinations (#18661)

## Description
This PR updates/reverts the `is-terminal` command to it's original state
so it can detect ttys and we add a new command `is-redirected` to
determine redirection. We conflated these two commands and it was
confusing.

#### `is-terminal` (restored)

- Pure OS checks for `--stdin` / `--stdout` / `--stderr`.
- No flags defaults to `--stdout` (kept from #18443).
- Works inside `if (…)`, `let`, and mid-block again.
- Help text points users needing Nu pipeline awareness to
`is-redirected`.

#### `is-redirected` (new)

- `true` when the current custom command’s **return value** is not going
to the interactive display path.
- Stable for the whole command body (including `if (is-redirected)`).

## User-facing changes (Release notes)

Fixed `is-terminal` so it again reports whether process
stdin/stdout/stderr is a real terminal (like bash `test -t`), including
inside `if (is-terminal)`. This restores correct detection for scripts
such as `./script.nu | cat`.

Added `is-redirected` for custom commands to detect whether their return
value is piped, collected (`let`), written to a file, or
discarded—versus printed for display. Unlike checking process stdout,
this is stable for the whole command body and works inside `if
(is-redirected)`.

Example for pretty-vs-data commands:

```nushell
def mycmd [] {
  if (is-terminal --stdout) and not (is-redirected) {
    # human-friendly formatting
  } else {
    # structured data
  }
}

mycmd            # pretty on a TTY when not piped
mycmd | $in      # data
let x = (mycmd)  # data
```

Scripts that only care about process stdout still use:

```nushell
if (is-terminal --stdout) { "human" } else { "piped" }
```

`is-terminal` with no flags still defaults to checking stdout.

A few other examples of how `is-redirected` is only designed to work on
nushell destinations. These are all redirections with nushell
destinations aka `is-redirected = true`.
```nushell
• mycmd | $in / mycmd | where …
• let x = (mycmd)
• mycmd o> file
• mycmd | ignore
```

## Additional notes
closes #15368
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to determine if a output is being displayed in the terminal vs. piped to another command

1 participant