@@ -26,4 +26,114 @@ package htmlTests
2626 * </code></pre>
2727 *
2828*/
29- class HtmlTest
29+ class HtmlTest
30+
31+ /** Implements functionality for printing Scala values on the terminal. For reading values
32+ * use [[scala.io.StdIn$ StdIn ]].
33+ * Also defines constants for marking up text on ANSI terminals.
34+ *
35+ * == Console Output ==
36+ *
37+ * Use the print methods to output text.
38+ * {{{
39+ * scala> Console.printf(
40+ * "Today the outside temperature is a balmy %.1f°C. %<.1f°C beats the previous record of %.1f°C.\n",
41+ * -137.0,
42+ * -135.05)
43+ * Today the outside temperature is a balmy -137.0°C. -137.0°C beats the previous record of -135.1°C.
44+ * }}}
45+ *
46+ * == ANSI escape codes ==
47+ * Use the ANSI escape codes for colorizing console output either to STDOUT or STDERR.
48+ * {{{
49+ * import Console.{GREEN, RED, RESET, YELLOW_B, UNDERLINED}
50+ *
51+ * object PrimeTest {
52+ *
53+ * def isPrime(): Unit = {
54+ *
55+ * val candidate = io.StdIn.readInt().ensuring(_ > 1)
56+ *
57+ * val prime = (2 to candidate - 1).forall(candidate % _ != 0)
58+ *
59+ * if (prime)
60+ * Console.println(s"\${RESET}\${GREEN}yes\${RESET}")
61+ * else
62+ * Console.err.println(s"\${RESET}\${YELLOW_B}\${RED}\${UNDERLINED}NO!\${RESET}")
63+ * }
64+ *
65+ * def main(args: Array[String]): Unit = isPrime()
66+ *
67+ * }
68+ * }}}
69+ *
70+ * <table style="border: 10px solid #000;width:100%">
71+ * <tr><td style="background-color:#000;color:#fff">\$ scala PrimeTest</td></tr>
72+ * <tr><td style="background-color:#000;color:#fff">1234567891</td></tr>
73+ * <tr><td style="background-color:#000;color:#0f0">yes</td></tr>
74+ * <tr><td style="background-color:#000;color:#fff">\$ scala PrimeTest</td></tr>
75+ * <tr><td style="background-color:#000;color:#fff">56474</td></tr>
76+ * <tr><td style="background-color:#000;color:#fff"><span style="background-color:#ff0;color:#f00;text-decoration:underline">NO!</span></td></tr>
77+ * </table>
78+ *
79+ * == IO redefinition ==
80+ *
81+ * Use IO redefinition to temporarily swap in a different set of input and/or output streams. In this example the stream based
82+ * method above is wrapped into a function.
83+ *
84+ * {{{
85+ * import java.io.{ByteArrayOutputStream, StringReader}
86+ *
87+ * object FunctionalPrimeTest {
88+ *
89+ * def isPrime(candidate: Int): Boolean = {
90+ *
91+ * val input = new StringReader(s"\$candidate\n")
92+ * val outCapture = new ByteArrayOutputStream
93+ * val errCapture = new ByteArrayOutputStream
94+ *
95+ * Console.withIn(input) {
96+ * Console.withOut(outCapture) {
97+ * Console.withErr(errCapture) {
98+ * PrimeTest.isPrime()
99+ * }
100+ * }
101+ * }
102+ *
103+ * if (outCapture.toByteArray.nonEmpty) // "yes"
104+ * true
105+ * else if (errCapture.toByteArray.nonEmpty) // "NO!"
106+ * false
107+ * else throw new IllegalArgumentException(candidate.toString)
108+ * }
109+ *
110+ * def main(args: Array[String]): Unit = {
111+ * val primes = (2 to 50) filter (isPrime)
112+ * println(s"First primes: \$primes")
113+ * }
114+ *
115+ * }
116+ * }}}
117+ *
118+ *
119+ * <table style="border: 10px solid #000;width:100%">
120+ * <tr><td style="background-color:#000;color:#fff">\$ scala FunctionalPrimeTest</td></tr>
121+ * <tr><td style="background-color:#000;color:#fff">First primes: Vector(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)</td></tr>
122+ * </table>
123+ *
124+ * @syntax wiki
125+ * @groupname console-output Console Output
126+ * @groupprio console-output 30
127+ * @groupdesc console-output These methods provide output via the console.
128+ *
129+ * @groupname io-default IO Defaults
130+ * @groupprio io-default 50
131+ * @groupdesc io-default These values provide direct access to the standard IO channels
132+ *
133+ * @groupname io-redefinition IO Redefinition
134+ * @groupprio io-redefinition 60
135+ * @groupdesc io-redefinition These methods allow substituting alternative streams for the duration of
136+ * a body of code. Threadsafe by virtue of [[scala.util.DynamicVariable ]].
137+ *
138+ */
139+ object Console
0 commit comments