Skip to content
Myr

stdin.go

This file

Path

internal/stdin/stdin.go

91 lines · 1.6 KiB · Go

Last changed by

fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd

History of this file

Take it

The walk stopped after 200 commits: lines older than 6837ed2 point at it.

fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd

line 1why, one step back
1// Package stdin handles processing input from stdin.

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 2–9why, one step back
2package stdin
34import (5 "bufio"6 "fmt"7 "io"8 "os"9 "strings"

fix: strip ansi sequences from stdin (#739)Carlos Alexandro Beckerbefore adoptionfb543c3

lines 10–11why, one step back
10
11 "github.com/charmbracelet/x/ansi"

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 12–13why, one step back
12)
13

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

lines 14–35why, one step back
14type options struct {
15 ansiStrip bool16 singleLine bool17}1819// Option is a read option.20type Option func(*options)2122// StripANSI optionally strips ansi sequences.23func StripANSI(b bool) Option {24 return func(o *options) {25 o.ansiStrip = b26 }27}2829// SingleLine reads a single line.30func SingleLine(b bool) Option {31 return func(o *options) {32 o.singleLine = b33 }34}35

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 36why, one step back
36// Read reads input from an stdin pipe.

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

line 37why, one step back
37func Read(opts ...Option) (string, error) {

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 38–41why, one step back
38 if IsEmpty() {
39 return "", fmt.Errorf("stdin is empty")40 }41

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

lines 42–46why, one step back
42 options := options{}
43 for _, opt := range opts {44 opt(&options)45 }46

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 47–49why, one step back
47 reader := bufio.NewReader(os.Stdin)
48 var b strings.Builder49

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

lines 50–61why, one step back
50 if options.singleLine {
51 line, _, err := reader.ReadLine()52 if err != nil {53 return "", fmt.Errorf("failed to read line: %w", err)54 }55 _, err = b.Write(line)56 if err != nil {57 return "", fmt.Errorf("failed to write: %w", err)58 }59 }6061 for !options.singleLine {

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 62–71why, one step back
62 r, _, err := reader.ReadRune()
63 if err != nil && err == io.EOF {64 break65 }66 _, err = b.WriteRune(r)67 if err != nil {68 return "", fmt.Errorf("failed to write rune: %w", err)69 }70 }71

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

lines 72–76why, one step back
72 s := strings.TrimSpace(b.String())
73 if options.ansiStrip {74 return ansi.Strip(s), nil75 }76 return s, nil

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 77–91why, one step back
77}
7879// IsEmpty returns whether stdin is empty.80func IsEmpty() bool {81 stat, err := os.Stdin.Stat()82 if err != nil {83 return true84 }8586 if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {87 return true88 }8990 return false91}