require "ratatui_ruby/tea"
module DiskUsage
Command = RatatuiRuby::Tea::Command
Model = Data.define(:output, :loading)
Init = -> do
Model.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: Ractor.make_shareable(stdout.strip), loading: false), nil]
in [{ type: :system, envelope: :disk_usage, stderr: }]
[model.with(output: Ractor.make_shareable("Error: #{stderr.strip}"), loading: false), nil]
else
[model, nil]
end
end
def self.fetch_command
Command.system("df -h /", :disk_usage)
end
end