github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/term/term.go (about) 1 package term 2 3 import ( 4 "context" 5 6 "github.com/lmorg/murex/config" 7 "github.com/lmorg/murex/lang/stdio" 8 "github.com/lmorg/murex/lang/types" 9 10 "io" 11 "sync" 12 ) 13 14 // We don't register these pipes because we don't want users creating them 15 // adhoc inside murex 16 /*func init() { 17 stdio.RegisterPipe("term-out", func(string) (stdio.Io, error) { 18 return nil, errors.New("`term-out` is a system device and cannot be created") 19 }) 20 21 stdio.RegisterPipe("term-err", func(string) (stdio.Io, error) { 22 return nil, errors.New("`term-err` is a system device and cannot be created") 23 }) 24 }*/ 25 26 // NewErr returns either Err or ErrRed depending on whether colourised output 27 // was defined via `colorise` 28 func NewErr(colourise bool) stdio.Io { 29 if colourise { 30 return new(ErrRed) 31 } 32 return new(Err) 33 } 34 35 // term structure exists as a wrapper around tty.Stdout and tty.Stderr so they 36 // can be easily interchanged with this shells streams (which has a larger 37 // array of methods to enable easier writing of builtin shell functions. 38 type term struct { 39 mutex sync.Mutex 40 bWritten uint64 41 bRead uint64 42 } 43 44 // Read is a null method because the term interface is write-only 45 func (t *term) Read([]byte) (int, error) { return 0, io.EOF } 46 47 // ReadLine is a null method because the term interface is write-only 48 func (t *term) ReadLine(func([]byte)) error { return nil } 49 50 // ReadArray is a null method because the term interface is write-only 51 func (t *term) ReadArray(context.Context, func([]byte)) error { return nil } 52 53 // ReadArray is a null method because the term interface is write-only 54 func (t *term) ReadArrayWithType(context.Context, func(interface{}, string)) error { return nil } 55 56 // ReadMap is a null method because the term interface is write-only 57 func (t *term) ReadMap(*config.Config, func(*stdio.Map)) error { return nil } 58 59 // ReadAll is a null method because the term interface is write-only 60 func (t *term) ReadAll() ([]byte, error) { return []byte{}, nil } 61 62 // WriteTo is a null method because the term interface is write-only 63 func (t *term) WriteTo(io.Writer) (int64, error) { return 0, io.EOF } 64 65 // GetDataType is a null method because the term interface is write-only 66 func (t *term) GetDataType() string { return types.Null } 67 68 // DefaultDataType is a null method because the term interface is write-only 69 func (t *term) DefaultDataType(bool) {} 70 71 // Open is a null method because the OS standard streams shouldn't be closed 72 // thus we don't need to track how many times they've been opened 73 func (t *term) Open() {} 74 75 // Close is a null method because the OS standard streams shouldn't be closed 76 func (t *term) Close() {} 77 78 // ForceClose is a null method because the OS standard streams shouldn't be closed 79 func (t *term) ForceClose() {} 80 81 // IsTTY always returns `true` because you are writing to a TTY. All over 82 // stream.Io interfaces should return `false` 83 func (t *term) IsTTY() bool { return true } 84 85 // Stats returns the bytes written and bytes read from the term interface 86 func (t *term) Stats() (bytesWritten, bytesRead uint64) { 87 //t.mutex.RLock() 88 t.mutex.Lock() 89 bytesWritten = t.bWritten 90 bytesRead = t.bRead 91 //t.mutex.RUnlock() 92 t.mutex.Unlock() 93 return 94 }