-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhexdump4s.sc
39 lines (36 loc) · 1.48 KB
/
hexdump4s.sc
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
//> using scala 3.3.1
//> using lib org.scodec::scodec-bits::1.1.38
//> using lib com.monovore::decline::2.4.1
//
// Build JVM distribution with: scala-cli package hexdump4s.sc -o hexdump4s -f --assembly
// Build Scala Native version with: scala-cli package --native hexdump4s.sc -o hexdump4s -f
// Build GraalVM native image version with: scala-cli package --native-image hexdump4s.sc -f -- --no-fallback
//
import scodec.bits.*
import com.monovore.decline.*
import java.nio.file.{Files, Path}
import cats.syntax.all.*
val command = Command(
name = "hexdump4s",
header = "Prints a hex dump of a file"
):
val offset = Opts.option[Long]("offset", short = "s", metavar = "count",
help = "Number of bytes to skip at start of input").withDefault(0L)
val length = Opts.option[Long]("length", short = "n", metavar = "count",
help = "Number of bytes to dump").orNone
val noColor = Opts.flag("no-color", help = "Disables color output").orFalse
val file = Opts.argument[Path](metavar = "file").orNone
(offset, length, noColor, file).tupled
command.parse(args) match
case Left(help) =>
System.err.println(help)
case Right((offset, limit, noColor, file)) =>
def data: BitVector =
val source = BitVector.fromInputStream(
file.map(f => Files.newInputStream(f)).getOrElse(System.in))
source.drop(offset * 8L)
HexDumpFormat.Default
.withAnsi(!noColor)
.withAddressOffset(offset.toInt)
.withLengthLimit(limit.getOrElse(Long.MaxValue))
.print(data)