util: extract formatting helper functions from FileUtils into a new FormatUtils

This commit is contained in:
Han Yin 2025-04-20 18:37:10 -07:00
parent d7afcc41d5
commit 10ca2fa834
2 changed files with 48 additions and 30 deletions

View File

@ -12,36 +12,6 @@ import java.nio.ByteBuffer
import java.nio.channels.Channels import java.nio.channels.Channels
import java.nio.channels.ReadableByteChannel import java.nio.channels.ReadableByteChannel
import java.nio.channels.WritableByteChannel import java.nio.channels.WritableByteChannel
import java.util.Locale
/**
* Convert bytes into human readable sizes
*/
fun formatFileByteSize(sizeInBytes: Long) = when {
sizeInBytes >= 1_000_000_000 -> {
val sizeInGb = sizeInBytes / 1_000_000_000.0
String.format(Locale.getDefault(), "%.1f GB", sizeInGb)
}
sizeInBytes >= 1_000_000 -> {
val sizeInMb = sizeInBytes / 1_000_000.0
String.format(Locale.getDefault(), "%.0f MB", sizeInMb)
}
else -> {
val sizeInKb = sizeInBytes / 1_000.0
String.format(Locale.getDefault(), "%.0f KB", sizeInKb)
}
}
/**
* Formats numbers to human-readable form (K, M)
*/
fun formatContextLength(contextLength: Int): String {
return when {
contextLength >= 1_000_000 -> String.format(Locale.getDefault(), "%.1fM", contextLength / 1_000_000.0)
contextLength >= 1_000 -> String.format(Locale.getDefault(), "%.0fK", contextLength / 1_000.0)
else -> contextLength.toString()
}
}
/** /**
* Gets the file name from a content URI * Gets the file name from a content URI

View File

@ -0,0 +1,48 @@
package com.example.llama.revamp.util
import java.util.Locale
/**
* Formats milliseconds into a human-readable time string
* e.g., 2300ms -> "2.3 sec"
*/
fun formatMilliSeconds(millis: Long): String {
val seconds = millis / 1000.0
return if (seconds < 1.0) {
"${(seconds * 1000).toInt()} ms"
} else if (seconds < 60.0) {
"%.1f sec".format(seconds)
} else {
val minutes = seconds / 60.0
"%.1f min".format(minutes)
}
}
/**
* Convert bytes into human readable sizes
*/
fun formatFileByteSize(sizeInBytes: Long) = when {
sizeInBytes >= 1_000_000_000 -> {
val sizeInGb = sizeInBytes / 1_000_000_000.0
String.Companion.format(Locale.getDefault(), "%.1f GB", sizeInGb)
}
sizeInBytes >= 1_000_000 -> {
val sizeInMb = sizeInBytes / 1_000_000.0
String.Companion.format(Locale.getDefault(), "%.0f MB", sizeInMb)
}
else -> {
val sizeInKb = sizeInBytes / 1_000.0
String.Companion.format(Locale.getDefault(), "%.0f KB", sizeInKb)
}
}
/**
* Formats numbers to human-readable form (K, M)
*/
fun formatContextLength(contextLength: Int): String {
return when {
contextLength >= 1_000_000 -> String.Companion.format(Locale.getDefault(), "%.1fM", contextLength / 1_000_000.0)
contextLength >= 1_000 -> String.Companion.format(Locale.getDefault(), "%.0fK", contextLength / 1_000.0)
else -> contextLength.toString()
}
}