|
| 1 | +package bubbletea |
| 2 | + |
| 3 | +import ( |
| 4 | + "image/color" |
| 5 | + "io" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/charmbracelet/x/ansi" |
| 9 | + "github.com/charmbracelet/x/input" |
| 10 | +) |
| 11 | + |
| 12 | +// queryBackgroundColor queries the terminal for the background color. |
| 13 | +// If the terminal does not support querying the background color, nil is |
| 14 | +// returned. |
| 15 | +// |
| 16 | +// Note: you will need to set the input to raw mode before calling this |
| 17 | +// function. |
| 18 | +// |
| 19 | +// state, _ := term.MakeRaw(in.Fd()) |
| 20 | +// defer term.Restore(in.Fd(), state) |
| 21 | +// |
| 22 | +// copied from x/[email protected]. |
| 23 | +func queryBackgroundColor(in io.Reader, out io.Writer) (c color.Color, err error) { |
| 24 | + // nolint: errcheck |
| 25 | + err = queryTerminal(in, out, defaultQueryTimeout, |
| 26 | + func(events []input.Event) bool { |
| 27 | + for _, e := range events { |
| 28 | + switch e := e.(type) { |
| 29 | + case input.BackgroundColorEvent: |
| 30 | + c = e.Color |
| 31 | + continue // we need to consume the next DA1 event |
| 32 | + case input.PrimaryDeviceAttributesEvent: |
| 33 | + return false |
| 34 | + } |
| 35 | + } |
| 36 | + return true |
| 37 | + }, ansi.RequestBackgroundColor+ansi.RequestPrimaryDeviceAttributes) |
| 38 | + return |
| 39 | +} |
| 40 | + |
| 41 | +const defaultQueryTimeout = time.Second * 2 |
| 42 | + |
| 43 | +// QueryTerminalFilter is a function that filters input events using a type |
| 44 | +// switch. If false is returned, the QueryTerminal function will stop reading |
| 45 | +// input. |
| 46 | +type QueryTerminalFilter func(events []input.Event) bool |
| 47 | + |
| 48 | +// queryTerminal queries the terminal for support of various features and |
| 49 | +// returns a list of response events. |
| 50 | +// Most of the time, you will need to set stdin to raw mode before calling this |
| 51 | +// function. |
| 52 | +// Note: This function will block until the terminal responds or the timeout |
| 53 | +// is reached. |
| 54 | +// copied from x/[email protected]. |
| 55 | +func queryTerminal( |
| 56 | + in io.Reader, |
| 57 | + out io.Writer, |
| 58 | + timeout time.Duration, |
| 59 | + filter QueryTerminalFilter, |
| 60 | + query string, |
| 61 | +) error { |
| 62 | + rd, err := input.NewDriver(in, "", 0) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + defer rd.Close() // nolint: errcheck |
| 68 | + |
| 69 | + done := make(chan struct{}, 1) |
| 70 | + defer close(done) |
| 71 | + go func() { |
| 72 | + select { |
| 73 | + case <-done: |
| 74 | + case <-time.After(timeout): |
| 75 | + rd.Cancel() |
| 76 | + } |
| 77 | + }() |
| 78 | + |
| 79 | + if _, err := io.WriteString(out, query); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + for { |
| 84 | + events, err := rd.ReadEvents() |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + if !filter(events) { |
| 90 | + break |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
0 commit comments