summaryrefslogtreecommitdiff
path: root/internal/repl/tty.go
blob: 12992cfe7993f9ecef8c97e8abb76b8c351be1cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

import (
	"fmt"
	"os"

	"github.com/mattn/go-isatty"
)

// TTYChecker provides TTY detection functionality.
// It uses the go-isatty package to determine if stdin is a terminal.
type TTYChecker struct{}

// IsTTY returns true if stdin is a terminal.
// This is useful for determining whether to run in interactive REPL mode.
//
// Returns true if stdin is a TTY, false otherwise
func (c *TTYChecker) IsTTY() bool {
	return isatty.IsTerminal(os.Stdin.Fd())
}

// EnsureTTY checks if stdin is a TTY and returns an error if not.
// This is used to prevent running the REPL in non-interactive contexts
// (e.g., when stdin is piped from a file or another command).
//
// Returns nil if stdin is a TTY, or an error describing the issue otherwise
func (c *TTYChecker) EnsureTTY() error {
	if !c.IsTTY() {
		fmt.Fprintln(os.Stderr, "REPL mode requires a TTY. Use 'gt <calculation>' for non-interactive mode.")
		return fmt.Errorf("stdin is not a TTY")
	}
	return nil
}