///|
pub fn format_bytes(value : Int64) -> String {
if value < 0L {
return "-" + format_bytes(-value)
}
let units = ["B", "KiB", "MiB", "GiB", "TiB"]
let mut divisor = 1L
let mut unit = 0
while unit < units.length() - 1 && value >= divisor * 1024L {
divisor *= 1024L
unit += 1
}
if unit == 0 {
value.to_string() + " B"
} else {
let whole = value / divisor
let decimal = value % divisor * 10L / divisor
whole.to_string() + "." + decimal.to_string() + " " + units[unit]
}
}
///|
pub fn format_percent(part : Int64, total : Int64) -> String {
if total <= 0L {
"0.0%"
} else {
let tenths = part * 1000L / total
(tenths / 10L).to_string() + "." + (tenths % 10L).to_string() + "%"
}
}
///|
pub fn pad_left(text : String, width : Int) -> String {
if text.length() >= width {
text
} else {
" ".repeat(width - text.length()) + text
}
}
///|
pub fn pad_right(text : String, width : Int) -> String {
if text.length() >= width {
text
} else {
text + " ".repeat(width - text.length())
}
}