require "rooibos"
module DiskUsage
Command = Rooibos::Command
CommandResult = Data.define(:output, :loading)
Init = -> do
CommandResult.new(output: "Press 'd' for disk usage", loading: false)
end
View = -> (model, tui, disabled: false) do
text_style = if disabled && model.output == Init.().output
tui.style(fg: :dark_gray)
else
nil
end
tui.paragraph(
text: tui.text_span(content: model.output, style: text_style),
block: tui.block(title: "Disk Usage", borders: [:all], border_style: { fg: :cyan })
)
end
Update = -> (message, model) do
case message
in { type: :system, envelope: :disk_usage, status: 0, stdout: }
[model.with(output: stdout.strip, loading: false), nil]
in { type: :system, envelope: :disk_usage, stderr: }
[model.with(output: "Error: #{stderr.strip}", loading: false), nil]
else
[model, nil]
end
end
def self.fetch_command
Command.system("df -h /", :disk_usage)
end
end