-
Notifications
You must be signed in to change notification settings - Fork 509
/
ConsoleCrossPlatform.scala
290 lines (251 loc) · 8.84 KB
/
ConsoleCrossPlatform.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2020-2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This is an adapted version of the code originally found in
* https://github.com/profunktor/console4cats, by Gabriel Volpe.
*/
package cats.effect.std
import cats.{~>, Applicative, Functor, Show}
import cats.data.{EitherT, IorT, Kleisli, OptionT, ReaderWriterStateT, StateT, WriterT}
import cats.effect.kernel.Sync
import cats.kernel.Monoid
import cats.syntax.show._
import scala.annotation.tailrec
import java.io.{ByteArrayOutputStream, EOFException, PrintStream}
import java.lang.{StringBuilder => JStringBuilder}
import java.nio.{ByteBuffer, CharBuffer}
import java.nio.charset.{Charset, CodingErrorAction, MalformedInputException}
private[std] trait ConsoleCrossPlatform[F[_]] { self: Console[F] =>
/**
* Prints a value to the standard output using the implicit `cats.Show` instance.
*
* @param a
* value to be printed to the standard output
* @param S
* implicit `cats.Show[A]` instance, defaults to `cats.Show.fromToString`
*/
def print[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit]
/**
* Prints a value to the standard output followed by a new line using the implicit `cats.Show`
* instance.
*
* @param a
* value to be printed to the standard output
* @param S
* implicit `cats.Show[A]` instance, defaults to `cats.Show.fromToString`
*/
def println[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit]
/**
* Prints a value to the standard error output using the implicit `cats.Show` instance.
*
* @param a
* value to be printed to the standard error output
* @param S
* implicit `cats.Show[A]` instance, defaults to `cats.Show.fromToString`
*/
def error[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit]
/**
* Prints a value to the standard error output followed by a new line using the implicit
* `cast.Show` instance.
*
* @param a
* value to be printed to the standard error output
* @param S
* implicit `cats.Show[A]` instance, defaults to `cats.Show.fromToString`
*/
def errorln[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit]
/**
* Prints the stack trace of the given Throwable to standard error output.
*/
def printStackTrace(t: Throwable): F[Unit]
/**
* Modifies the context in which this console operates using the natural transformation `f`.
*
* @return
* a console in the new context obtained by mapping the current one using `f`
*/
def mapK[G[_]](f: F ~> G): Console[G]
}
private[std] abstract class ConsoleCompanionCrossPlatform {
/**
* Summoner method for `Console` instances.
*
* For printing to the standard output:
* {{{
* Console[F].print("Hello")
* Console[F].println("Hello")
* }}}
*
* For printing to the standard error:
* {{{
* Console[F].error("Hello")
* Console[F].errorln("Hello")
* }}}
*
* For reading from the standard input:
* {{{
* Console[F].readLine
* }}}
*/
def apply[F[_]](implicit C: Console[F]): C.type = C
/**
* [[Console]] instance built for `cats.data.EitherT` values initialized with any `F` data
* type that also implements `Console`.
*/
implicit def catsEitherTConsole[F[_]: Console: Functor, L]: Console[EitherT[F, L, *]] =
Console[F].mapK(EitherT.liftK)
/**
* [[Console]] instance built for `cats.data.Kleisli` values initialized with any `F` data
* type that also implements `Console`.
*/
implicit def catsKleisliConsole[F[_]: Console, R]: Console[Kleisli[F, R, *]] =
Console[F].mapK(Kleisli.liftK)
/**
* [[Console]] instance built for `cats.data.OptionT` values initialized with any `F` data
* type that also implements `Console`.
*/
implicit def catsOptionTConsole[F[_]: Console: Functor]: Console[OptionT[F, *]] =
Console[F].mapK(OptionT.liftK)
/**
* [[Console]] instance built for `cats.data.StateT` values initialized with any `F` data type
* that also implements `Console`.
*/
implicit def catsStateTConsole[F[_]: Console: Applicative, S]: Console[StateT[F, S, *]] =
Console[F].mapK(StateT.liftK)
/**
* [[Console]] instance built for `cats.data.WriterT` values initialized with any `F` data
* type that also implements `Console`.
*/
implicit def catsWriterTConsole[
F[_]: Console: Applicative,
L: Monoid
]: Console[WriterT[F, L, *]] =
Console[F].mapK(WriterT.liftK)
/**
* [[Console]] instance built for `cats.data.IorT` values initialized with any `F` data type
* that also implements `Console`.
*/
implicit def catsIorTConsole[F[_]: Console: Functor, L]: Console[IorT[F, L, *]] =
Console[F].mapK(IorT.liftK)
/**
* [[Console]] instance built for `cats.data.ReaderWriterStateT` values initialized with any
* `F` data type that also implements `Console`.
*/
implicit def catsReaderWriterStateTConsole[
F[_]: Console: Applicative,
E,
L: Monoid,
S
]: Console[ReaderWriterStateT[F, E, L, S, *]] =
Console[F].mapK(ReaderWriterStateT.liftK)
private[std] final class SyncConsole[F[_]](implicit F: Sync[F]) extends Console[F] {
def readLineWithCharset(charset: Charset): F[String] =
F.interruptible {
val in = System.in
val decoder = charset
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
val bytes = ByteBuffer.allocate(64)
val builder = new JStringBuilder()
def decodeNext(): CharBuffer = {
bytes.clear()
decodeNextLoop()
}
@tailrec
def decodeNextLoop(): CharBuffer = {
val b = in.read()
if (b == -1) null
else {
bytes.put(b.toByte)
val limit = bytes.limit()
val position = bytes.position()
var result: CharBuffer = null
try {
bytes.flip()
result = decoder.decode(bytes)
} catch {
case _: MalformedInputException =>
bytes.limit(limit)
bytes.position(position)
}
if (result == null) decodeNextLoop() else result
}
}
@tailrec
def loop(): String = {
val buffer = decodeNext()
if (buffer == null) {
val result = builder.toString()
if (result.nonEmpty) result
else throw new EOFException()
} else {
val decoded = buffer.toString()
if (decoded == "\n") {
val len = builder.length()
if (len > 0) {
if (builder.charAt(len - 1) == '\r') {
builder.deleteCharAt(len - 1)
}
}
builder.toString()
} else {
builder.append(decoded)
loop()
}
}
}
loop()
}
def print[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit] = {
val text = a.show
F.blocking(System.out.print(text))
}
def println[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit] = {
val text = a.show
F.blocking(System.out.println(text))
}
def error[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit] = {
val text = a.show
F.blocking(System.err.print(text))
}
def errorln[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit] = {
val text = a.show
F.blocking(System.err.println(text))
}
override def printStackTrace(t: Throwable): F[Unit] =
F.blocking(t.printStackTrace())
}
private[std] def printStackTrace[F[_]](c: Console[F])(t: Throwable): F[Unit] = {
val baos = new ByteArrayOutputStream()
val ps = new PrintStream(baos)
t.printStackTrace(ps)
c.error(baos.toString)
}
private[std] def mapK[F[_], G[_]](self: Console[F])(f: F ~> G): Console[G] =
new Console.MapKConsole(self, f) {
def print[A](a: A)(implicit S: Show[A]): G[Unit] =
f(self.print(a))
def println[A](a: A)(implicit S: Show[A]): G[Unit] =
f(self.println(a))
def error[A](a: A)(implicit S: Show[A]): G[Unit] =
f(self.error(a))
def errorln[A](a: A)(implicit S: Show[A]): G[Unit] =
f(self.errorln(a))
override def printStackTrace(t: Throwable): G[Unit] =
f(self.printStackTrace(t))
}
}