...

Source file src/github.com/redhat-developer/odo/pkg/watch/key_watcher.go

Documentation: github.com/redhat-developer/odo/pkg/watch

     1  package watch
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"golang.org/x/term"
    10  )
    11  
    12  // getKeyWatcher returns a channel which will emit
    13  // characters when keys are pressed on the keyboard
    14  func getKeyWatcher(ctx context.Context, out io.Writer) <-chan byte {
    15  
    16  	keyInput := make(chan byte)
    17  
    18  	go func() {
    19  		stdinfd := int(os.Stdin.Fd())
    20  		if !term.IsTerminal(stdinfd) {
    21  			return
    22  		}
    23  
    24  		// First set the terminal in character mode
    25  		// to be able to read characters as soon as
    26  		// they are emitted, instead of waiting
    27  		// for newline characters
    28  		oldState, err := term.GetState(stdinfd)
    29  		if err != nil {
    30  			fmt.Fprintln(out, fmt.Errorf("getstate: %w", err))
    31  			return
    32  		}
    33  		err = enableCharInput(stdinfd)
    34  		if err != nil {
    35  			fmt.Fprintln(out, fmt.Errorf("enableCharInput: %w", err))
    36  			return
    37  		}
    38  		defer func() {
    39  			_ = term.Restore(stdinfd, oldState)
    40  		}()
    41  
    42  		// Wait for the context to be cancelled
    43  		// or a character to be emitted
    44  		for {
    45  			select {
    46  			case <-ctx.Done():
    47  				return
    48  			case b := <-getKey(out):
    49  				keyInput <- b
    50  			}
    51  		}
    52  	}()
    53  
    54  	return keyInput
    55  }
    56  
    57  // getKey returns a channel which will emit a character
    58  // when a key is pressed on the keyboard
    59  func getKey(out io.Writer) <-chan byte {
    60  
    61  	ch := make(chan byte)
    62  
    63  	go func() {
    64  		b := make([]byte, 1)
    65  		_, err := os.Stdin.Read(b)
    66  		if err != nil {
    67  			fmt.Fprintln(out, fmt.Errorf("read: %w", err))
    68  			return
    69  		}
    70  		ch <- b[0]
    71  	}()
    72  
    73  	return ch
    74  }
    75  

View as plain text