From 032475c713da00cbfbca338610c7d52b615696c7 Mon Sep 17 00:00:00 2001 From: Robert Attard Date: Sat, 25 May 2024 19:03:43 -0400 Subject: [PATCH] spinners --- src/gladvent/internal/cmd/run.gleam | 22 ++++++++++++++++++++++ src/gladvent/internal/runners.gleam | 14 ++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/gladvent/internal/cmd/run.gleam b/src/gladvent/internal/cmd/run.gleam index aae4b40..b36339f 100644 --- a/src/gladvent/internal/cmd/run.gleam +++ b/src/gladvent/internal/cmd/run.gleam @@ -2,6 +2,7 @@ import filepath import gladvent/internal/cmd.{Ending, Endless} import gladvent/internal/parse.{type Day} import gladvent/internal/runners +import gladvent/internal/util import gleam import gleam/dict as map import gleam/dynamic.{type Dynamic} @@ -17,6 +18,7 @@ import gleam/string import glint import simplifile import snag.{type Result, type Snag} +import spinner type AsyncResult = gleam.Result(RunResult, String) @@ -258,6 +260,18 @@ pub fn run_command() -> glint.Command(Result(List(String))) { use days <- result.then(parse.days(args)) let assert Ok(year) = glint.get_flag(flags, cmd.year_flag()) let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag()) + + let spinner = + spinner.new( + "running days " + <> string.join(args, ", ") + <> " in " + <> int.to_string(year), + ) + |> spinner.start() + + use <- util.defer(do: fn() { spinner.stop(spinner) }) + let timing = glint.get_flag(flags, timeout_flag()) |> result.map(Ending) @@ -276,8 +290,16 @@ pub fn run_all_command() -> glint.Command(Result(List(String))) { use <- glint.command_help("Run all registered days") use <- glint.unnamed_args(glint.EqArgs(0)) use _, _, flags <- glint.command() + let assert Ok(year) = glint.get_flag(flags, cmd.year_flag()) let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag()) + + let spinner = + spinner.new("running all days in " <> int.to_string(year)) + |> spinner.start() + + use <- util.defer(do: fn() { spinner.stop(spinner) }) + let timing = glint.get_flag(flags, timeout_flag()) |> result.map(Ending) diff --git a/src/gladvent/internal/runners.gleam b/src/gladvent/internal/runners.gleam index 89ddaae..a15dd02 100644 --- a/src/gladvent/internal/runners.gleam +++ b/src/gladvent/internal/runners.gleam @@ -71,35 +71,39 @@ fn package_interface_error_to_snag(e: PkgInterfaceErr) -> snag.Snag { pub fn pkg_interface() -> Result(package_interface.Package) { use <- snagify_error(with: package_interface_error_to_snag) let spinner = - spinner.new("generating " <> package_interface_path) + spinner.new("initializing package interface") |> spinner.start() use <- defer(do: fn() { spinner.stop(spinner) }) let root = cmd.root() + spinner.set_text(spinner, "reading gleam.toml") use gleam_toml <- result.try( simplifile.read(filepath.join(root, "gleam.toml")) |> result.map_error(FailedToReadGleamToml), ) + spinner.set_text(spinner, "decoding gleam.toml") use toml <- result.try( tom.parse(gleam_toml) |> result.map_error(FailedToDecodeGleamToml), ) + spinner.set_text(spinner, "fetching project name") use name <- result.try( tom.get_string(toml, ["name"]) |> result.map_error(FailedToGetPackageName), ) + spinner.set_text(spinner, "clearing build cache") use _ <- result.try( - { - use cache <- list.try_map(["build/prod/erlang", "build/dev/erlang"]) + ["build/prod/erlang", "build/dev/erlang"] + |> list.try_map(fn(cache) { filepath.join(root, cache) |> filepath.join(name) |> simplifile.delete - } + }) |> result.try_recover(fn(e) { case e { simplifile.Enoent -> Ok([]) @@ -108,11 +112,13 @@ pub fn pkg_interface() -> Result(package_interface.Package) { }), ) + spinner.set_text(spinner, "rebuilding project") use _ <- result.try( shellout.command("gleam", ["build"], root, []) |> result.map_error(fn(e) { FailedToGeneratePackageInterface(e.1) }), ) + spinner.set_text(spinner, "generating package interface file") use _ <- result.try( shellout.command( "gleam",