style: apply stylua formatter

This commit is contained in:
Erica Marigold 2024-12-25 16:11:05 +00:00
parent 269c05cbb4
commit 472813b41b
Signed by: DevComp
GPG key ID: 429EF1C337871656
3 changed files with 126 additions and 126 deletions

View file

@ -1,16 +1,16 @@
--> Non-exhaustive set of extensions for the `Option<T>` type --> Non-exhaustive set of extensions for the `Option<T>` type
local Option = require("../../../lune_packages/option") local Option = require("../../../lune_packages/option")
local Result = require("../../../lune_packages/result") local Result = require("../../../lune_packages/result")
local OptionExt = {} local OptionExt = {}
function OptionExt.okOr<T, E>(self: Option.Option<T>, err: E): Result.Result<T, E> function OptionExt.okOr<T, E>(self: Option.Option<T>, err: E): Result.Result<T, E>
return self:mapOrElse(function() return self:mapOrElse(function()
return Result.Err(err) return Result.Err(err)
end, function(val) end, function(val)
return Result.Ok(val) return Result.Ok(val)
end) :: Result.Result<T, E> end) :: Result.Result<T, E>
end end
return OptionExt return OptionExt

View file

@ -1,14 +1,14 @@
--> Non-exhaustive set of extensions for the `Result<T, E>` type --> Non-exhaustive set of extensions for the `Result<T, E>` type
local Option = require("../../../lune_packages/option") local Option = require("../../../lune_packages/option")
local Result = require("../../../lune_packages/result") local Result = require("../../../lune_packages/result")
local ResultExt = {} local ResultExt = {}
function ResultExt.ok<T, E>(self: Result.Result<T, E>): Option.Option<T> function ResultExt.ok<T, E>(self: Result.Result<T, E>): Option.Option<T>
return self:mapOr(Option.None, function(val: T) return self:mapOr(Option.None, function(val: T)
return Option.Some(val) return Option.Some(val)
end) :: Option.Option<T> end) :: Option.Option<T>
end end
return ResultExt return ResultExt

View file

@ -1,96 +1,96 @@
--> Inspired by Rokit's progress bar: https://github.com/rojo-rbx/rokit/blob/a303faf/src/util/progress.rs --> Inspired by Rokit's progress bar: https://github.com/rojo-rbx/rokit/blob/a303faf/src/util/progress.rs
local task = require("@lune/task") local task = require("@lune/task")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
local Result = require("../../lune_packages/result") local Result = require("../../lune_packages/result")
local Option = require("../../lune_packages/option") local Option = require("../../lune_packages/option")
type Option<T> = Option.Option<T> type Option<T> = Option.Option<T>
type Result<T, E> = Result.Result<T, E> type Result<T, E> = Result.Result<T, E>
-- FORMAT: {SPINNER} {MESSAGE} {BAR} {STAGE} -- FORMAT: {SPINNER} {MESSAGE} {BAR} {STAGE}
local SPINNERS = { "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } local SPINNERS = { "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }
local BAR_COMPONENT = "▇" local BAR_COMPONENT = "▇"
local MAX_BAR_LENGTH = 30 local MAX_BAR_LENGTH = 30
local ProgressBar = {} local ProgressBar = {}
type ProgressBar = { type ProgressBar = {
stages: { { tag: string, message: string } }, stages: { { tag: string, message: string } },
currentStageIndex: number, currentStageIndex: number,
finished: boolean, finished: boolean,
thread: thread?, thread: thread?,
} }
export type ProgressBarImpl = typeof(setmetatable({} :: ProgressBar, { __index = ProgressBar })) export type ProgressBarImpl = typeof(setmetatable({} :: ProgressBar, { __index = ProgressBar }))
function ProgressBar.new(): ProgressBarImpl function ProgressBar.new(): ProgressBarImpl
return setmetatable( return setmetatable(
{ {
stages = {}, stages = {},
currentStageIndex = 1, currentStageIndex = 1,
finished = false, finished = false,
} :: ProgressBar, } :: ProgressBar,
{ {
__index = ProgressBar, __index = ProgressBar,
} }
) )
end end
function ProgressBar.withStage(self: ProgressBarImpl, tag: string, msg: string): ProgressBarImpl function ProgressBar.withStage(self: ProgressBarImpl, tag: string, msg: string): ProgressBarImpl
table.insert(self.stages, { tag = tag, message = msg }) table.insert(self.stages, { tag = tag, message = msg })
return self return self
end end
function ProgressBar.start(self: ProgressBarImpl): () function ProgressBar.start(self: ProgressBarImpl): ()
local BAR_LENGTH = MAX_BAR_LENGTH // #self.stages local BAR_LENGTH = MAX_BAR_LENGTH // #self.stages
local TOTAL_BAR_LENGTH = BAR_LENGTH * #self.stages local TOTAL_BAR_LENGTH = BAR_LENGTH * #self.stages
local BAR = string.rep(BAR_COMPONENT, BAR_LENGTH) local BAR = string.rep(BAR_COMPONENT, BAR_LENGTH)
local MAX_MESSAGE_LENGTH = 0 local MAX_MESSAGE_LENGTH = 0
for _, stage in self.stages do for _, stage in self.stages do
local len = #stage.message local len = #stage.message
if len > MAX_MESSAGE_LENGTH then if len > MAX_MESSAGE_LENGTH then
MAX_MESSAGE_LENGTH = len MAX_MESSAGE_LENGTH = len
end end
end end
self.thread = task.spawn(function() self.thread = task.spawn(function()
while not self.finished do while not self.finished do
for _, spinner in SPINNERS do for _, spinner in SPINNERS do
local stage = self.stages[self.currentStageIndex] local stage = self.stages[self.currentStageIndex]
stdio.write( stdio.write(
`\x1b[2K\x1b[0G{stdio.color("cyan")}{spinner} {stage.message}{stdio.color("reset")}{string.rep( `\x1b[2K\x1b[0G{stdio.color("cyan")}{spinner} {stage.message}{stdio.color("reset")}{string.rep(
" ", " ",
MAX_MESSAGE_LENGTH - #stage.message MAX_MESSAGE_LENGTH - #stage.message
)} [{stdio.style("dim")}{string.rep(BAR, self.currentStageIndex)}{string.rep( )} [{stdio.style("dim")}{string.rep(BAR, self.currentStageIndex)}{string.rep(
" ", " ",
TOTAL_BAR_LENGTH - (BAR_LENGTH * self.currentStageIndex) TOTAL_BAR_LENGTH - (BAR_LENGTH * self.currentStageIndex)
)}{stdio.style("reset")}] {stdio.style("bold")}{self.currentStageIndex} / {#self.stages}{stdio.style( )}{stdio.style("reset")}] {stdio.style("bold")}{self.currentStageIndex} / {#self.stages}{stdio.style(
"reset" "reset"
)}` )}`
) )
task.wait(0.1) task.wait(0.1)
end end
end end
end) end)
end end
function ProgressBar.stop(self: ProgressBarImpl): () function ProgressBar.stop(self: ProgressBarImpl): ()
-- Trigger upvalue, kill thread and clean progress bar remnant -- Trigger upvalue, kill thread and clean progress bar remnant
self.finished = true self.finished = true
task.cancel(self.thread :: thread) task.cancel(self.thread :: thread)
stdio.write("\x1b[2K\x1b[0G") stdio.write("\x1b[2K\x1b[0G")
end end
function ProgressBar.nextStage(self: ProgressBarImpl): Result<nil, string> function ProgressBar.nextStage(self: ProgressBarImpl): Result<nil, string>
local inc = self.currentStageIndex + 1 local inc = self.currentStageIndex + 1
if inc > #self.stages then if inc > #self.stages then
-- TODO: Make this a result -- TODO: Make this a result
self.finished = true self.finished = true
return Result.Err("OutOfBounds - Attempted to advance past last stage") return Result.Err("OutOfBounds - Attempted to advance past last stage")
end end
self.currentStageIndex = inc self.currentStageIndex = inc
return Result.Ok(nil) return Result.Ok(nil)
end end
return ProgressBar return ProgressBar