|
| 1 | +package argparse.core |
| 2 | + |
| 3 | +import javax.swing.GroupLayout.Alignment |
| 4 | + |
| 5 | +trait OutputApi extends ParsersApi with TypesApi with Printers: |
| 6 | + |
| 7 | + /** Top-level error handler for command line applications using the annotation |
| 8 | + * API. |
| 9 | + * |
| 10 | + * You can override this to change what should be done on errors. |
| 11 | + * |
| 12 | + * The default implementation prints the exception's message unless the DEBUG |
| 13 | + * environment variable is set, in which case the whole stack trace is |
| 14 | + * printed. Then, it exits with error code 1. |
| 15 | + */ |
| 16 | + def handleError(t: Throwable): Nothing = |
| 17 | + if sys.env.contains("DEBUG") then |
| 18 | + t.printStackTrace() |
| 19 | + else |
| 20 | + System.err.println(t.getMessage()) |
| 21 | + exit(1) |
| 22 | + |
| 23 | + trait Printer[A]: |
| 24 | + def print( |
| 25 | + a: A, |
| 26 | + out: java.io.PrintStream, |
| 27 | + info: OutputApi.StreamInfo |
| 28 | + ): Unit |
| 29 | + |
| 30 | +object OutputApi: |
| 31 | + |
| 32 | + case class StreamInfo( |
| 33 | + isatty: Boolean |
| 34 | + ) |
| 35 | + |
| 36 | + enum Alignment: |
| 37 | + case Left |
| 38 | + case Right |
| 39 | + case Numeric |
| 40 | + |
| 41 | + def tabulate( |
| 42 | + rows: Iterable[Iterable[_]], |
| 43 | + header: Iterable[String] = Iterable.empty, |
| 44 | + alignments: Iterable[Alignment] = Iterable.empty |
| 45 | + ): String = { |
| 46 | + if (header.isEmpty && rows.isEmpty) return "" |
| 47 | + |
| 48 | + val ncols = if (!header.isEmpty) { |
| 49 | + header.size |
| 50 | + } else { |
| 51 | + rows.head.size |
| 52 | + } |
| 53 | + |
| 54 | + val aligns = if (alignments.isEmpty) { |
| 55 | + if (!rows.isEmpty) { |
| 56 | + rows.head.map{ elem => |
| 57 | + val s = elem.toString |
| 58 | + if (s.size > 0 && (s(0) == '-' || s(0).isDigit)) { |
| 59 | + Alignment.Numeric |
| 60 | + } else { |
| 61 | + Alignment.Left |
| 62 | + } |
| 63 | + }.toArray |
| 64 | + } else { |
| 65 | + header.map(_ => Alignment.Left).toArray |
| 66 | + } |
| 67 | + } else { |
| 68 | + alignments.toArray |
| 69 | + } |
| 70 | + var i = 0 |
| 71 | + var j = 0 |
| 72 | + |
| 73 | + val leftMosts = Array.fill(ncols)(0) |
| 74 | + val rightMosts = Array.fill(ncols)(0) |
| 75 | + |
| 76 | + val strings = for (row <- rows) yield { |
| 77 | + i = 0 |
| 78 | + for (elem <- row) yield { |
| 79 | + val s = elem.toString |
| 80 | + |
| 81 | + if (aligns(i) == Alignment.Numeric) { |
| 82 | + j = 0 |
| 83 | + var commaPos = s.length |
| 84 | + while (j < s.length && commaPos == s.length) { |
| 85 | + if (s.charAt(j) == '.') commaPos = j |
| 86 | + j += 1 |
| 87 | + } |
| 88 | + |
| 89 | + if (commaPos > leftMosts(i)) leftMosts(i) = commaPos |
| 90 | + if ((s.length - commaPos) > rightMosts(i)) rightMosts(i) = s.length - commaPos |
| 91 | + } else { |
| 92 | + if (s.length > leftMosts(i)) leftMosts(i) = s.length |
| 93 | + } |
| 94 | + i += 1 |
| 95 | + s |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + val maxWidths = Array.fill(ncols)(0) |
| 100 | + |
| 101 | + val header1 = header.toArray |
| 102 | + |
| 103 | + i = 0 |
| 104 | + while (i < leftMosts.size) { |
| 105 | + val w = leftMosts(i) + rightMosts(i) |
| 106 | + if (w > maxWidths(i)) maxWidths(i) = w |
| 107 | + if (!header.isEmpty && header1(i).size > maxWidths(i)) maxWidths(i) = header1(i).size |
| 108 | + i += 1 |
| 109 | + } |
| 110 | + |
| 111 | + val buffer = new collection.mutable.StringBuilder |
| 112 | + val srows = strings.iterator |
| 113 | + |
| 114 | + inline def printElemLeft(elem: String) = { |
| 115 | + buffer ++= elem |
| 116 | + val padding = maxWidths(i) - elem.length |
| 117 | + j = 0 |
| 118 | + while (j < padding) { |
| 119 | + buffer += ' ' |
| 120 | + j += 1 |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + inline def printElemRight(elem: String) = { |
| 125 | + val padding = maxWidths(i) - elem.length |
| 126 | + j = 0 |
| 127 | + while (j < padding) { |
| 128 | + buffer += ' ' |
| 129 | + j += 1 |
| 130 | + } |
| 131 | + buffer ++= elem |
| 132 | + } |
| 133 | + |
| 134 | + inline def printElemNumeric(elem: String) = { |
| 135 | + j = 0 |
| 136 | + var commaPos = elem.length |
| 137 | + while (j < elem.size && commaPos == elem.length) { |
| 138 | + if (elem.charAt(j) == '.') commaPos = j |
| 139 | + j += 1 |
| 140 | + } |
| 141 | + |
| 142 | + val paddingL = (maxWidths(i) - leftMosts(i) - rightMosts(i)) + leftMosts(i) - commaPos |
| 143 | + j = 0 |
| 144 | + while (j < paddingL) { |
| 145 | + buffer += ' ' |
| 146 | + j += 1 |
| 147 | + } |
| 148 | + buffer ++= elem |
| 149 | + val paddingR = rightMosts(i) - (elem.length - commaPos) |
| 150 | + j = 0 |
| 151 | + while (j < paddingR) { |
| 152 | + buffer += ' ' |
| 153 | + j += 1 |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + inline def printElem(elem: String) = aligns(i) match { |
| 158 | + case Alignment.Left => printElemLeft(elem) |
| 159 | + case Alignment.Right => printElemRight(elem) |
| 160 | + case Alignment.Numeric => printElemNumeric(elem) |
| 161 | + } |
| 162 | + |
| 163 | + inline def printRow(elems: Iterator[String]) = { |
| 164 | + i = 0 |
| 165 | + if (elems.hasNext) { |
| 166 | + printElem(elems.next) |
| 167 | + i += 1 |
| 168 | + } |
| 169 | + while (elems.hasNext) { |
| 170 | + buffer += ' ' |
| 171 | + printElem(elems.next) |
| 172 | + i += 1 |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (!header.isEmpty) { |
| 177 | + val elems = header.iterator // if header is not empty, the first srows elem is the header |
| 178 | + i = 0 |
| 179 | + if (elems.hasNext) { |
| 180 | + printElemLeft(elems.next) |
| 181 | + i += 1 |
| 182 | + } |
| 183 | + while (elems.hasNext) { |
| 184 | + buffer += ' ' |
| 185 | + printElemLeft(elems.next) |
| 186 | + i += 1 |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (srows.hasNext) { |
| 191 | + if (!header.isEmpty) buffer += '\n' |
| 192 | + printRow(srows.next().iterator) |
| 193 | + } |
| 194 | + while (srows.hasNext) { |
| 195 | + buffer += '\n' |
| 196 | + printRow(srows.next().iterator) |
| 197 | + } |
| 198 | + buffer.toString |
| 199 | + } |
| 200 | + |
| 201 | + def tabulatep( |
| 202 | + rows: Iterable[Product], |
| 203 | + headers: Iterable[String] = null, |
| 204 | + alignments: Iterable[Alignment] = Iterable.empty |
| 205 | + ): String = |
| 206 | + tabulate( |
| 207 | + rows.map(_.productIterator.toIterable), |
| 208 | + if headers == null then |
| 209 | + if !rows.isEmpty then rows.head.productElementNames.toIterable.map(_.toUpperCase()) |
| 210 | + else Iterable.empty |
| 211 | + else headers, |
| 212 | + alignments |
| 213 | + ) |
| 214 | + |
| 215 | +trait Printers extends LowPrioPrinters: |
| 216 | + self: OutputApi => |
| 217 | + |
| 218 | + given Printer[Unit] with |
| 219 | + def print(a: Unit, out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = () |
| 220 | + |
| 221 | + given Printer[Array[Byte]] with |
| 222 | + def print(a: Array[Byte], out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 223 | + out.write(a) |
| 224 | + |
| 225 | + given Printer[geny.Writable] with |
| 226 | + def print(a: geny.Writable, out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 227 | + a.writeBytesTo(out) |
| 228 | + |
| 229 | + given [A](using p: Printer[A]): Printer[geny.Generator[A]] with |
| 230 | + def print(value: geny.Generator[A], out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 231 | + value.foreach(v => p.print(v, out, info)) |
| 232 | + |
| 233 | + inline given productListPrinter[A <: Iterable[B], B <: Product](using m: ProductLabels[B]): Printer[A] with |
| 234 | + def print(value: A, out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 235 | + val rows = value.map(_.productIterator.toIterable) |
| 236 | + if info.isatty then |
| 237 | + out.println(OutputApi.tabulate(rows, header = m.labels.map(_.toUpperCase))) |
| 238 | + else |
| 239 | + out.println(OutputApi.tabulate(rows, header = Iterable.empty)) |
| 240 | + |
| 241 | + given nonProductListPrinter[A <: Iterable[B], B](using elemPrinter: Printer[B]): Printer[A] with |
| 242 | + def print(value: A, out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 243 | + for elem <- value do elemPrinter.print(elem, out, info) |
| 244 | + |
| 245 | + given [A](using p: Printer[A]): Printer[concurrent.Future[A]] with |
| 246 | + def print(value: concurrent.Future[A], out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 247 | + p.print( |
| 248 | + concurrent.Await.result(value, concurrent.duration.Duration.Inf), |
| 249 | + out, |
| 250 | + info |
| 251 | + ) |
| 252 | + |
| 253 | +trait LowPrioPrinters: |
| 254 | + self: OutputApi => |
| 255 | + |
| 256 | + // fallback to always print something |
| 257 | + given [A]: Printer[A] with |
| 258 | + def print(a: A, out: java.io.PrintStream, info: OutputApi.StreamInfo): Unit = |
| 259 | + out.println(a.toString) |
0 commit comments