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

     1  //go:build !js
     2  // +build !js
     3  
     4  package term
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/lmorg/murex/lang/stdio"
    10  	"github.com/lmorg/murex/utils"
    11  	"github.com/lmorg/murex/utils/ansi/codes"
    12  )
    13  
    14  // Terminal: Standard Error - Coloured Red
    15  
    16  // ErrRed is the Stderr interface for term - with output coloured red
    17  type ErrRed struct {
    18  	term
    19  }
    20  
    21  func (t *ErrRed) File() *os.File {
    22  	return os.Stderr
    23  }
    24  
    25  // SetDataType is a null method because the term interface is write-only
    26  func (t *ErrRed) SetDataType(string) {}
    27  
    28  // Write is the io.Writer() interface for term
    29  func (t *ErrRed) Write(b []byte) (i int, err error) {
    30  	t.mutex.Lock()
    31  	t.bWritten += uint64(len(b))
    32  	t.mutex.Unlock()
    33  
    34  	i, err = os.Stderr.WriteString(codes.FgRed + string(b) + codes.Reset)
    35  	if err != nil {
    36  		os.Stdout.WriteString(codes.FgRed + err.Error() + codes.Reset)
    37  	}
    38  
    39  	return i - 9, err
    40  }
    41  
    42  // Writeln writes an OS-specific terminated line to the stderr
    43  func (t *ErrRed) Writeln(b []byte) (int, error) {
    44  	//line := append(b, utils.NewLineByte...)
    45  	//return t.Write(line)
    46  	return t.Write(appendBytes(b, utils.NewLineByte...))
    47  }
    48  
    49  // WriteArray performs data type specific buffered writes to an stdio.Io interface
    50  func (t *ErrRed) WriteArray(dataType string) (stdio.ArrayWriter, error) {
    51  	return stdio.WriteArray(t, dataType)
    52  }