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

View file

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