1  package frost.io
  2  
  3  ====================================================================================================
  4  Provides access to the standard input, output, and error streams.
  5  ====================================================================================================
  6  class Console {
  7      @private
  8      init() {
  9      }
 10  
 11      ================================================================================================
 12      Returns the standard input stream.
 13      ================================================================================================
 14      @class
 15      @external(frostConsoleInputStream)
 16      function inputStream():InputStream
 17  
 18      ================================================================================================
 19      Returns the standard output stream.
 20      ================================================================================================
 21      @class
 22      @external(frostConsoleOutputStream)
 23      function outputStream():OutputStream
 24  
 25      ================================================================================================
 26      Returns the standard error stream.
 27      ================================================================================================
 28      @class
 29      @external(frostConsoleErrorStream)
 30      function errorStream():OutputStream
 31  
 32      ================================================================================================
 33      Prints a string to the standard output stream.
 34      ================================================================================================
 35      @class
 36      @external(frostConsolePrint)
 37      method print(s:String)
 38  
 39      ================================================================================================
 40      Prints a string followed by a newline to the standard output stream.
 41      ================================================================================================
 42      @class
 43      method printLine(s:String) {
 44          print(s)
 45          printLine()
 46      }
 47  
 48      ================================================================================================
 49      Prints the string represention of an object to the standard output stream.
 50      ================================================================================================
 51      @class
 52      method print(o:Object) {
 53          print(o.toString)
 54      }
 55  
 56      ================================================================================================
 57      Prints the string represention of an object followed by a newline to the standard output stream.
 58      ================================================================================================
 59      @class
 60      method printLine(o:Object) {
 61          printLine(o.toString)
 62      }
 63  
 64      ================================================================================================
 65      Prints a newline to the standard output stream.
 66      ================================================================================================
 67      @class
 68      method printLine() {
 69          print("\n")
 70      }
 71  
 72      ================================================================================================
 73      Prints a string to the standard error stream.
 74      ================================================================================================
 75      @class
 76      @external(frostConsolePrintError)
 77      method printError(s:String)
 78  
 79      ================================================================================================
 80      Prints a string followed by a newline to the standard error stream.
 81      ================================================================================================
 82      @class
 83      method printErrorLine(s:String) {
 84          printError(s)
 85          printErrorLine()
 86      }
 87  
 88      ================================================================================================
 89      Prints the string represention of an object to the standard error stream.
 90      ================================================================================================
 91      @class
 92      method printError(o:Object) {
 93          printError(o.toString)
 94      }
 95  
 96      ================================================================================================
 97      Prints the string represention of an object followed by a newline to the standard error stream.
 98      ================================================================================================
 99      @class
100      method printErrorLine(o:Object) {
101          printErrorLine(o.toString)
102      }
103  
104      ================================================================================================
105      Prints a newline to the standard error stream.
106      ================================================================================================
107      @class
108      method printErrorLine() {
109          printError("\n")
110      }
111  
112      ================================================================================================
113      Reads a single byte from the standard input stream. Returns `null` if the end of the stream has
114      been reached.
115      ================================================================================================
116      @class
117      @external(frostConsoleRead)
118      method read():Char8?
119  
120      ================================================================================================
121      Reads a line of text from the standard input stream. Returns `null` if the end of the stream has
122      been reached.
123      ================================================================================================
124      @class
125      method readLine():String? {
126          def result := MutableString()
127          loop {
128              def c:Char8? := read()
129              if c == null {
130                  if result.byteLength = 0 {
131                      return null
132                  }
133                  break
134              }
135              if c = "\n" {
136                  break
137              }
138              result.append(c)
139          }
140          return result.finish()
141      }
142  }