{"id":13800114,"url":"https://github.com/i10416/prettytable-native","last_synced_at":"2026-03-15T15:11:08.920Z","repository":{"id":37183346,"uuid":"403857930","full_name":"i10416/prettytable-native","owner":"i10416","description":"standalone application with scala native","archived":false,"fork":false,"pushed_at":"2023-06-05T06:34:01.000Z","size":52,"stargazers_count":11,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T01:04:49.201Z","etag":null,"topics":["cli","scala","scala-native"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/i10416.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-09-07T05:56:58.000Z","updated_at":"2024-12-10T05:07:23.000Z","dependencies_parsed_at":"2024-01-05T21:58:16.137Z","dependency_job_id":null,"html_url":"https://github.com/i10416/prettytable-native","commit_stats":null,"previous_names":["itoyo16u/prettytable-native"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i10416%2Fprettytable-native","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i10416%2Fprettytable-native/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i10416%2Fprettytable-native/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i10416%2Fprettytable-native/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/i10416","download_url":"https://codeload.github.com/i10416/prettytable-native/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252542565,"owners_count":21764989,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","scala","scala-native"],"created_at":"2024-08-04T00:01:09.441Z","updated_at":"2026-03-15T15:11:03.890Z","avatar_url":"https://github.com/i10416.png","language":"Scala","funding_links":[],"categories":["Tutorials and Examples"],"sub_categories":[],"readme":"\n# Write a Simple CLI Application with Scala Native\nThis repository shows a simple example of writing a standalone CLI application with scala JVM and Scala Native.\n\nCompared to using GraalVM Native Image, there is few gotcha in writing and building scala-native application! \n\n## Goal\nOur goal is to write a toy application which pretty-prints a csv or json file in your terminal.\n\nInput: test.csv\n```\nid,name,icon,comment,favorite\n1,tama,😼,meow,matatabi\n2,pochi,🐶,bowwow,🦴\n```\nOutput\n```\nprettytable test.csv\nid|name |icon|comment|favorite\n==+=====+====+=======+========\n1 |tama |😼  |meow   |matatabi\n--+-----+----+-------+--------\n2 |pochi|🐶  |bowwow |🦴\n```\n\nOutput: test.json\n```\nprettytable test.json\nwebsite        |name                      |email                      |username          |company|id|address|phone\n===============+==========================+===========================+==================+=======+==+=======+=======================\n\"hildegard.org\"|\"Leanne Graham\"           |\"Sincere@april.biz\"        |\"Bret\"            |...    |1 |...    |\"1-770-736-8031 x56442\"\n---------------+--------------------------+---------------------------+------------------+-------+--+-------+-----------------------\n\"anastasia.net\"|\"Ervin Howell\"            |\"Shanna@melissa.tv\"        |\"Antonette\"       |...    |2 |...    |\"010-692-6593 x09125\"\n---------------+--------------------------+---------------------------+------------------+-------+--+-------+-----------------------\n\"ramiro.info\"  |\"Clementine Bauch\"        |\"Nathan@yesenia.net\"       |\"Samantha\"        |...    |3 |...    |\"1-463-123-4447\"\n---------------+--------------------------+---------------------------+------------------+-------+--+-------+----------------------\n\n\n```\n## Getting Started\nTo write a scala-native application, set up your dev environment according to following steps. \n\n### Install Scala(or sbt or bleep)\n\nWhat is the differences?\n\n- cs is a short for [coursier](https://get-coursier.io/), which is dependency resolution(and more) library for Scala. In addition to dependency fetching, coursier has a CLI which makes it easy for you to setup useful tools for Scala development.\n- sbt is a de facto build tool for Scala ecosystem. You can write build tasks in Scala DSL and then sbt takes all the hard works(managing Scala version, dependencies, run tasks, etc.) to build Scala application/library on behalf of you.\n- [bleep](https://bleep.build/docs/) is a new build tool for Scala. It is still its early phase and not stable yet. People from Rust and NodeJS may feel more comfortable with bleep build model than sbt build model.\n\nHere, I demonstrate how to install coursier CLI and setup Scala development environment.\n\n```sh\ncurl -fLo cs https://git.io/coursier-cli-\"$(uname | tr LD ld)\"\nchmod +x cs\n./cs install\nrm ./cs\ncs setup\n```\n\n### Install llvm and c++\n\nUbuntu\n\n```sh\nsudo bash -c \"$(wget -O - https://apt.llvm.org/llvm.sh)\"\n```\nor\n```sh\nsudo apt install clang\nsudo apt install libgc-dev # optional\n```\n\nMacOS\n\n```sh\nbrew install llvm\nbrew install bdw-gc # optional\n```\n\nThen, export CLANG_PATH and CLANGPP_PATH\n~/.bashrc\n```\nexport CLANG_PATH=/path/to/clang-\u003cversion\u003e\nexport CLANGPP_PATH=/path/to/clang++-\u003cversion\u003e\n\n```\n\n### create project\n\n```sh\nsbt new scala-native/scala-native.g8\n```\n\nBefore writing your application, run the default application to test your environment. If it successfully compiles, it will show a greeting message.\n```sh\ncd \u003cproject name\u003e\nsbt run\n```\n\n## Write Application\n\nWe use following dependencies to build the application. I recommend you have a quick look at their documentations before start.\n\n- [cats](https://typelevel.org/cats/): It offers functional data structures and syntax.\n- [decline](https://ben.kirw.in/decline/): It enables us to handle command-line arguments in a functional way.\n- [oslib](https://github.com/com-lihaoyi/os-lib): simple and unopinionated i/o utilities.\n- [fansi](https://github.com/com-lihaoyi/fansi): decorate terminal output.\n- [argonaut](http://argonaut.io/): Purely Functional JSON in Scala\n\n\n\n\nAdd these libraryDependencies to your `build.sbt`.\n\n```scala\nscalaVersion := \"3.3.0\"\n\n// Set to false or remove if you want to show stubs as linking errors\nnativeLinkStubs := true\n\nlibraryDependencies ++= Seq(\n  \"org.typelevel\" %%% \"cats-core\" % \"2.9.0\",\n  \"com.lihaoyi\" %%% \"fansi\" % \"0.4.0\",\n  \"com.lihaoyi\" %%% \"os-lib\" % \"0.9.1\",\n  \"com.monovore\" %%% \"decline\" % \"2.4.1\",\n  \"io.argonaut\" %%% \"argonaut\" % \"6.3.8\",\n  \"org.scalatest\" %% \"scalatest\" % \"3.2.16\" % Test\n)\n\nenablePlugins(ScalaNativePlugin)\n```\n\nIn addition, make sure your `project/plugins.sbt` has correct version of Scala Native plugin.\n\n\n```\naddSbtPlugin(\"org.scala-native\" % \"sbt-scala-native\" % \"0.4.12\")\n```\n\nNow, run `sbt compile` to check your sbt build works.\n\n\n## handle i/o\nTo get a file path from command line argument, we use oslib.\nFirst, we check if file exists. Second, check the extension of the file. Third, if file is in csv or json format, we read and parse the content. \n\n```scala\nimport os._\nval path = os.pwd / cmd.fileName\nif(os.isFile(path)) {\n  path.ext match {\n    case \"csv\" =\u003e os.read.lines.stream(path).foreach { line =\u003e\n       ???\n     }\n    case \"json\" =\u003e ???\n    case other =\u003e UnsupportedExt(other).asLeft\n  }\n}else {\n  Left(IsNotFile())\n}\n```\n## parse command line arguments with decline\n\nDecline offers composable syntax to parse command line arguments.\n\nFor example, this command below parses an argument like `\u003ccommand-name\u003e example.txt` as `Path`.\n\n```scala\nval file = Opts.argument[Path](metavar=\"file\")\nval cmd = Command.apply(\"command-name\",\"header text\",true)(file)\n```\n\n`Opts` has other constructors such as `Opts.arguments`(accept multiple arguments), `Opts.flag`(accept single flag starting with `--`) and `Opts.option(...)`(accept a flag and its argument of type T like `--optionname value`).\n\n```scala\nval fooFlag = Opts.flag(\"flagname\",help=\"description\")\n// Opts[Unit] = Opts(--flagname)\n\nval intOption = Opts.option[Int](long=\"meaningoflife\",short=\"m\",metavar=\"meaning of life\",\"???\")\nval cmd = Command.apply(\"...\",\"...\",true)(intOption)\n\ncmd.parse(Seq(\"--meaningoflife\",\"42\")) // or cmd.parse(Seq(\"-m42\"))\n// Right(42)\n```\n\nFor more details, read decline documentation.\n## decorate command-line output\n\nYou can add decoration to terminal output such as emphasis, underline, bold, and color using fansi.\n\n## parse json\n\nIt is easier to display array of json objects as a table than to display arbitrary json. Thus, our application accepts only json array. \n\nTo handle json string in a type-safe way, we use argonaut and its json types.\n```scala\nimport argonaut._\n\n\nval json = Parse.parse(rawValue)\njson match {\n  case Right(j) if j.isArray =\u003e\n    ???\n  case Right(_) =\u003e\n    Left(JsonIsNotArray())\n  case _ =\u003e Left(ParseError())\n}\n```\n\n\nNow, start writing the application and execute `sbt run` command to build and run your application.\n\nFor more details, `see src/main/scala/*.scala` .\n\n\n## Useful links\n- https://scala-native.readthedocs.io/en/latest/\n- https://ben.kirw.in/decline/\n- https://github.com/com-lihaoyi/os-lib\n- https://github.com/com-lihaoyi/fansi\n- scala-native documentation : http://scala-native.org/\n- Write CLI Application with Scala and graal native image : https://msitko.pl/blog/2020/03/10/writing-native-cli-applications-in-scala-with-graalvm.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fi10416%2Fprettytable-native","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fi10416%2Fprettytable-native","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fi10416%2Fprettytable-native/lists"}