getCpuData function

CpuData getCpuData({
  1. bool isThrowOnError = false,
})

Implementation

CpuData getCpuData({
  bool isThrowOnError = false,
}) {
  try {
    if (Dart.isAndroid) {
      final Map cpuData = {
        "@type": "cpuData",
        "name": "",
      };
      final String cpuInfo = File("/proc/cpuinfo").readAsStringSync();
      final int cores = cpuInfo.split("\n").where((line) => line.startsWith("processor")).length;
      String cpu = "unknown";
      cpu = cpuInfo.split("\n").lastWhere((line) => line.startsWith("Hardware")).split(": ")[1].split("@")[0];
      cpuData["name"] = "${cpu} (${cores})";

      return CpuData(cpuData);
    }
    if (Dart.isLinux) {
      final Map cpuData = {
        "@type": "cpuData",
        "name": "",
      };

      final String cpuInfo = File("/proc/cpuinfo").readAsStringSync();
      int cores = cpuInfo.split("\n").where((line) => line.startsWith("processor")).length;
      String cpu = cpuInfo.split("\n").firstWhere((line) => line.startsWith("model name")).split(": ")[1].split("@")[0];

      cpu = cpu.replaceAll("(R)", "");
      cpu = cpu.replaceAll("Core(TM)", "");
      cpu = cpu.replaceAll("CPU", "");
      cpu = cpu.trim();
      cpuData["name"] = "${cpu} (${cores})";
      // cpuData.printPretty();
      return CpuData(cpuData);
    }
  } catch (e) {
    if (isThrowOnError) {
      rethrow;
    }
  }
  return CpuData({});
}