From 472813b41bbfa045fde71aefbf2f87e33be97498 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Wed, 25 Dec 2024 16:11:05 +0000 Subject: [PATCH] style: apply stylua formatter --- toolchainlib/src/utils/ext/option.luau | 32 ++--- toolchainlib/src/utils/ext/result.luau | 28 ++-- toolchainlib/src/utils/progress.luau | 192 ++++++++++++------------- 3 files changed, 126 insertions(+), 126 deletions(-) diff --git a/toolchainlib/src/utils/ext/option.luau b/toolchainlib/src/utils/ext/option.luau index 7b10dd0..81b5217 100644 --- a/toolchainlib/src/utils/ext/option.luau +++ b/toolchainlib/src/utils/ext/option.luau @@ -1,16 +1,16 @@ ---> Non-exhaustive set of extensions for the `Option` type - -local Option = require("../../../lune_packages/option") -local Result = require("../../../lune_packages/result") - -local OptionExt = {} - -function OptionExt.okOr(self: Option.Option, err: E): Result.Result - return self:mapOrElse(function() - return Result.Err(err) - end, function(val) - return Result.Ok(val) - end) :: Result.Result -end - -return OptionExt +--> Non-exhaustive set of extensions for the `Option` type + +local Option = require("../../../lune_packages/option") +local Result = require("../../../lune_packages/result") + +local OptionExt = {} + +function OptionExt.okOr(self: Option.Option, err: E): Result.Result + return self:mapOrElse(function() + return Result.Err(err) + end, function(val) + return Result.Ok(val) + end) :: Result.Result +end + +return OptionExt diff --git a/toolchainlib/src/utils/ext/result.luau b/toolchainlib/src/utils/ext/result.luau index 001fc9f..ef93440 100644 --- a/toolchainlib/src/utils/ext/result.luau +++ b/toolchainlib/src/utils/ext/result.luau @@ -1,14 +1,14 @@ ---> Non-exhaustive set of extensions for the `Result` type - -local Option = require("../../../lune_packages/option") -local Result = require("../../../lune_packages/result") - -local ResultExt = {} - -function ResultExt.ok(self: Result.Result): Option.Option - return self:mapOr(Option.None, function(val: T) - return Option.Some(val) - end) :: Option.Option -end - -return ResultExt +--> Non-exhaustive set of extensions for the `Result` type + +local Option = require("../../../lune_packages/option") +local Result = require("../../../lune_packages/result") + +local ResultExt = {} + +function ResultExt.ok(self: Result.Result): Option.Option + return self:mapOr(Option.None, function(val: T) + return Option.Some(val) + end) :: Option.Option +end + +return ResultExt diff --git a/toolchainlib/src/utils/progress.luau b/toolchainlib/src/utils/progress.luau index 63b551a..25743c9 100644 --- a/toolchainlib/src/utils/progress.luau +++ b/toolchainlib/src/utils/progress.luau @@ -1,96 +1,96 @@ ---> Inspired by Rokit's progress bar: https://github.com/rojo-rbx/rokit/blob/a303faf/src/util/progress.rs - -local task = require("@lune/task") -local stdio = require("@lune/stdio") - -local Result = require("../../lune_packages/result") -local Option = require("../../lune_packages/option") -type Option = Option.Option -type Result = Result.Result - --- FORMAT: {SPINNER} {MESSAGE} {BAR} {STAGE} -local SPINNERS = { "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } -local BAR_COMPONENT = "▇" -local MAX_BAR_LENGTH = 30 - -local ProgressBar = {} -type ProgressBar = { - stages: { { tag: string, message: string } }, - currentStageIndex: number, - finished: boolean, - thread: thread?, -} -export type ProgressBarImpl = typeof(setmetatable({} :: ProgressBar, { __index = ProgressBar })) - -function ProgressBar.new(): ProgressBarImpl - return setmetatable( - { - stages = {}, - currentStageIndex = 1, - finished = false, - } :: ProgressBar, - { - __index = ProgressBar, - } - ) -end - -function ProgressBar.withStage(self: ProgressBarImpl, tag: string, msg: string): ProgressBarImpl - table.insert(self.stages, { tag = tag, message = msg }) - return self -end - -function ProgressBar.start(self: ProgressBarImpl): () - local BAR_LENGTH = MAX_BAR_LENGTH // #self.stages - local TOTAL_BAR_LENGTH = BAR_LENGTH * #self.stages - local BAR = string.rep(BAR_COMPONENT, BAR_LENGTH) - local MAX_MESSAGE_LENGTH = 0 - for _, stage in self.stages do - local len = #stage.message - if len > MAX_MESSAGE_LENGTH then - MAX_MESSAGE_LENGTH = len - end - end - - self.thread = task.spawn(function() - while not self.finished do - for _, spinner in SPINNERS do - local stage = self.stages[self.currentStageIndex] - stdio.write( - `\x1b[2K\x1b[0G{stdio.color("cyan")}{spinner} {stage.message}{stdio.color("reset")}{string.rep( - " ", - MAX_MESSAGE_LENGTH - #stage.message - )} [{stdio.style("dim")}{string.rep(BAR, self.currentStageIndex)}{string.rep( - " ", - TOTAL_BAR_LENGTH - (BAR_LENGTH * self.currentStageIndex) - )}{stdio.style("reset")}] {stdio.style("bold")}{self.currentStageIndex} / {#self.stages}{stdio.style( - "reset" - )}` - ) - - task.wait(0.1) - end - end - end) -end - -function ProgressBar.stop(self: ProgressBarImpl): () - -- Trigger upvalue, kill thread and clean progress bar remnant - self.finished = true - task.cancel(self.thread :: thread) - stdio.write("\x1b[2K\x1b[0G") -end - -function ProgressBar.nextStage(self: ProgressBarImpl): Result - local inc = self.currentStageIndex + 1 - if inc > #self.stages then - -- TODO: Make this a result - self.finished = true - return Result.Err("OutOfBounds - Attempted to advance past last stage") - end - - self.currentStageIndex = inc - return Result.Ok(nil) -end - -return ProgressBar +--> Inspired by Rokit's progress bar: https://github.com/rojo-rbx/rokit/blob/a303faf/src/util/progress.rs + +local task = require("@lune/task") +local stdio = require("@lune/stdio") + +local Result = require("../../lune_packages/result") +local Option = require("../../lune_packages/option") +type Option = Option.Option +type Result = Result.Result + +-- FORMAT: {SPINNER} {MESSAGE} {BAR} {STAGE} +local SPINNERS = { "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } +local BAR_COMPONENT = "▇" +local MAX_BAR_LENGTH = 30 + +local ProgressBar = {} +type ProgressBar = { + stages: { { tag: string, message: string } }, + currentStageIndex: number, + finished: boolean, + thread: thread?, +} +export type ProgressBarImpl = typeof(setmetatable({} :: ProgressBar, { __index = ProgressBar })) + +function ProgressBar.new(): ProgressBarImpl + return setmetatable( + { + stages = {}, + currentStageIndex = 1, + finished = false, + } :: ProgressBar, + { + __index = ProgressBar, + } + ) +end + +function ProgressBar.withStage(self: ProgressBarImpl, tag: string, msg: string): ProgressBarImpl + table.insert(self.stages, { tag = tag, message = msg }) + return self +end + +function ProgressBar.start(self: ProgressBarImpl): () + local BAR_LENGTH = MAX_BAR_LENGTH // #self.stages + local TOTAL_BAR_LENGTH = BAR_LENGTH * #self.stages + local BAR = string.rep(BAR_COMPONENT, BAR_LENGTH) + local MAX_MESSAGE_LENGTH = 0 + for _, stage in self.stages do + local len = #stage.message + if len > MAX_MESSAGE_LENGTH then + MAX_MESSAGE_LENGTH = len + end + end + + self.thread = task.spawn(function() + while not self.finished do + for _, spinner in SPINNERS do + local stage = self.stages[self.currentStageIndex] + stdio.write( + `\x1b[2K\x1b[0G{stdio.color("cyan")}{spinner} {stage.message}{stdio.color("reset")}{string.rep( + " ", + MAX_MESSAGE_LENGTH - #stage.message + )} [{stdio.style("dim")}{string.rep(BAR, self.currentStageIndex)}{string.rep( + " ", + TOTAL_BAR_LENGTH - (BAR_LENGTH * self.currentStageIndex) + )}{stdio.style("reset")}] {stdio.style("bold")}{self.currentStageIndex} / {#self.stages}{stdio.style( + "reset" + )}` + ) + + task.wait(0.1) + end + end + end) +end + +function ProgressBar.stop(self: ProgressBarImpl): () + -- Trigger upvalue, kill thread and clean progress bar remnant + self.finished = true + task.cancel(self.thread :: thread) + stdio.write("\x1b[2K\x1b[0G") +end + +function ProgressBar.nextStage(self: ProgressBarImpl): Result + local inc = self.currentStageIndex + 1 + if inc > #self.stages then + -- TODO: Make this a result + self.finished = true + return Result.Err("OutOfBounds - Attempted to advance past last stage") + end + + self.currentStageIndex = inc + return Result.Ok(nil) +end + +return ProgressBar