commit 20/08/2025
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package codes
|
||||
|
||||
import oshi.SystemInfo
|
||||
import oshi.hardware.CentralProcessor
|
||||
import oshi.hardware.GlobalMemory
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
@@ -7,6 +10,45 @@ import java.nio.file.Path
|
||||
@Suppress("unused")
|
||||
class Somecodes {
|
||||
companion object {
|
||||
|
||||
val si = SystemInfo()
|
||||
val processor: CentralProcessor = si.hardware.processor
|
||||
val memory : GlobalMemory = si.hardware.memory
|
||||
|
||||
|
||||
const val KB_threshold = 1024.0
|
||||
const val MB_threshold = KB_threshold * 1024.0
|
||||
const val GB_threshold = MB_threshold * 1024.0
|
||||
const val TB_threshold = GB_threshold * 1024.0
|
||||
|
||||
/**
|
||||
* Converts a size in bytes to a human-readable format.
|
||||
* @param size Size in bytes.
|
||||
* @return A string representing the size in a human-readable format.
|
||||
*/
|
||||
fun SizetoHuman(size: Long): String {
|
||||
return when {
|
||||
size < KB_threshold -> "${size}B"
|
||||
size < MB_threshold -> String.format("%.2f KB", size / KB_threshold)
|
||||
size < GB_threshold -> String.format("%.2f MB", size / MB_threshold)
|
||||
size < TB_threshold -> String.format("%.2f GB", size / GB_threshold)
|
||||
else -> String.format("%.2f TB", size / TB_threshold)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun getMemoryUsage() : String{
|
||||
val totalMemory = memory.total
|
||||
val availableMemory = memory.available
|
||||
val usedMemory = totalMemory - availableMemory
|
||||
return String.format("Total: %s, Used: %s, Available: %s, Usage: %.2f%%",
|
||||
SizetoHuman(totalMemory),
|
||||
SizetoHuman(usedMemory),
|
||||
SizetoHuman(availableMemory)
|
||||
, (usedMemory.toDouble() / totalMemory * 100))
|
||||
}
|
||||
|
||||
fun ValidString(value: Any) : Boolean {
|
||||
return value is String && value.isNotBlank()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user