github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/term/in.go (about)

     1  package term
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/lmorg/murex/builtins/pipes/streams"
     8  	"github.com/lmorg/murex/lang/stdio"
     9  )
    10  
    11  // Terminal: Standard In
    12  
    13  // In is the Stdin interface for term
    14  type In struct {
    15  	streams.Stdin
    16  }
    17  
    18  func NewIn(dataType string) stdio.Io {
    19  	t := new(In)
    20  	t.Stdin = *streams.NewStdin()
    21  	t.SetDataType(dataType)
    22  	go backgroundRead(t)
    23  	return t
    24  }
    25  
    26  func backgroundRead(t *In) {
    27  	_, err := t.ReadFrom(os.Stdin)
    28  	if err != nil {
    29  		os.Stderr.WriteString("Error reading from STDIN: " + err.Error())
    30  	}
    31  }
    32  
    33  func (t *In) File() *os.File {
    34  	return nil
    35  }
    36  
    37  // Write is the io.Writer() interface for term
    38  func (t *In) Write(_ []byte) (int, error) {
    39  	return 0, errors.New("attempting to write to a readonly STDIN interface: Write()")
    40  }
    41  
    42  // Writeln writes an OS-specific terminated line to the stdout
    43  func (t *In) Writeln(_ []byte) (int, error) {
    44  	return 0, errors.New("attempting to write to a readonly STDIN interface: Writeln()")
    45  }
    46  
    47  // WriteArray performs data type specific buffered writes to an stdio.Io interface
    48  func (t *In) WriteArray(_ string) (stdio.ArrayWriter, error) {
    49  	return nil, errors.New("attempting to write to a readonly STDIN interface: WriteArray()")
    50  }