diff --git a/src/btop.cpp b/src/btop.cpp index 8ed0a60..e2651c9 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -31,17 +31,18 @@ tab-size = 4 #include #include #include +#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #if defined(__linux__) #define LINUX - #include + #include #elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__) #include #if defined(BSD) @@ -572,9 +573,9 @@ int main(int argc, char **argv){ lc = 0; ostring = Mv::u(2) + Mv::l(Term::width) + Mv::r(12) - + trans("Filter: " + filter + (filtering ? Fx::bl + "█" + Fx::reset : "")) + Mv::l(Term::width) + + trans("Filter: " + filter + (filtering ? Fx::bl + "█" + Fx::reset : " ")) + trans(rjust("Per core: " + (Config::getB("proc_per_core") ? "On "s : "Off"s) + " Sorting: " - + string(Config::getS("proc_sorting")), Term::width - 3)) + + string(Config::getS("proc_sorting")), Term::width - 23 - ulen(filter))) + Mv::restore; for (auto& p : plist){ diff --git a/src/btop_config.cpp b/src/btop_config.cpp index c250a78..45fce43 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -22,8 +22,8 @@ tab-size = 4 #include #include -#include -#include +#include +#include using robin_hood::unordered_flat_map, std::map, std::array, std::atomic; namespace fs = std::filesystem; diff --git a/src/btop_config.h b/src/btop_config.hpp similarity index 100% rename from src/btop_config.h rename to src/btop_config.hpp diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index f059e0f..0f11235 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -22,10 +22,10 @@ tab-size = 4 #include #include -#include -#include -#include -#include +#include +#include +#include +#include using robin_hood::unordered_flat_map, std::round, std::views::iota, std::string_literals::operator""s, std::clamp, std::array, std::floor; diff --git a/src/btop_draw.h b/src/btop_draw.hpp similarity index 98% rename from src/btop_draw.h rename to src/btop_draw.hpp index 3e21a6d..de3d8db 100644 --- a/src/btop_draw.h +++ b/src/btop_draw.hpp @@ -22,9 +22,6 @@ tab-size = 4 #include #include -#include -#include - using std::string, std::vector, robin_hood::unordered_flat_map; namespace Symbols { diff --git a/src/btop_input.h b/src/btop_input.cpp similarity index 95% rename from src/btop_input.h rename to src/btop_input.cpp index 5d694e8..eb13516 100644 --- a/src/btop_input.h +++ b/src/btop_input.cpp @@ -16,13 +16,12 @@ indent = tab tab-size = 4 */ -#pragma once - #include #include #include -#include +#include +#include using std::string, robin_hood::unordered_flat_map, std::cin; using namespace Tools; @@ -78,7 +77,7 @@ namespace Input { string last = ""; //* Poll keyboard & mouse input for ms and return input availabilty as a bool - bool poll(int timeout=0){ + bool poll(int timeout){ if (timeout < 1) return cin.rdbuf()->in_avail() > 0; while (timeout > 0) { if (cin.rdbuf()->in_avail() > 0) return true; @@ -89,7 +88,7 @@ namespace Input { } //* Get a key or mouse action from input - string get(bool clear = false){ + string get(bool clear){ string key; while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get(); if (!clear && !key.empty()){ @@ -102,7 +101,7 @@ namespace Input { } //* Wait until input is available - void wait(bool clear=false){ + void wait(bool clear){ while (cin.rdbuf()->in_avail() < 1) sleep_ms(10); if (clear) get(clear); } diff --git a/src/btop_input.hpp b/src/btop_input.hpp new file mode 100644 index 0000000..901183b --- /dev/null +++ b/src/btop_input.hpp @@ -0,0 +1,47 @@ +/* Copyright 2021 Aristocratos (jakob@qvantnet.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +indent = tab +tab-size = 4 +*/ + +#pragma once + +#include + +/* The input functions relies on the following std::cin options being set: + cin.sync_with_stdio(false); + cin.tie(NULL); + These will automatically be set when running Term::init() from btop_tools.cpp +*/ + +//* Functions and variables for handling keyboard and mouse input +namespace Input { + + //* Last entered key + extern std::string last; + + //* Poll keyboard & mouse input for ms and return input availabilty as a bool + bool poll(int timeout=0); + + //* Get a key or mouse action from input + std::string get(bool clear = false); + + //* Wait until input is available + void wait(bool clear=false); + + //* Clears last entered key + void clear(); + +} \ No newline at end of file diff --git a/src/btop_linux.cpp b/src/btop_linux.cpp index 0541752..eb4d6d8 100644 --- a/src/btop_linux.cpp +++ b/src/btop_linux.cpp @@ -32,9 +32,9 @@ tab-size = 4 #include -#include -#include -#include +#include +#include +#include @@ -98,7 +98,7 @@ namespace Proc { out_procs.push_back(cur_proc); int children = 0; for (auto& p : in_procs) { - if (p.ppid == (int)cur_proc.pid) { + if (p.ppid == cur_proc.pid) { children++; if (collapsed) { out_procs.back().cpu_p += p.cpu_p; @@ -254,46 +254,52 @@ namespace Proc { s_pos = instr.find_last_of(')') + 2; if (s_pos == string::npos) continue; - do { - c_pos = instr.find(' ', s_pos); - if (c_pos == string::npos) break; + try { + do { + c_pos = instr.find(' ', s_pos); + if (c_pos == string::npos) break; - switch (s_count) { - case 0: { //? Process state - new_proc.state = instr[s_pos]; - break; + switch (s_count) { + case 0: { //? Process state + new_proc.state = instr[s_pos]; + break; + } + case 1: { //? Process parent pid + new_proc.ppid = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 11: { //? Process utime + cpu_t = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 12: { //? Process stime + cpu_t += stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 16: { //? Process nice value + new_proc.p_nice = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 17: { //? Process number of threads + new_proc.threads = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 19: { //? Cache cpu seconds + if (new_cache) cache[new_proc.pid].cpu_s = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 36: { //? CPU number last executed on + new_proc.cpu_n = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } } - case 1: { //? Process parent pid - new_proc.ppid = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 11: { //? Process utime - cpu_t = stoull(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 12: { //? Process stime - cpu_t += stoull(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 16: { //? Process nice value - new_proc.p_nice = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 17: { //? Process number of threads - new_proc.threads = stoul(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 19: { //? Cache cpu seconds - if (new_cache) cache[new_proc.pid].cpu_s = stoull(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 36: { //? CPU number last executed on - new_proc.cpu_n = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - } - s_pos = c_pos + 1; - } while (s_count++ < 36); + + s_pos = c_pos + 1; + } while (s_count++ < 36); + } + catch (std::out_of_range&) { + continue; + } if (s_count < 19) continue; diff --git a/src/btop_linux.h b/src/btop_linux.hpp similarity index 96% rename from src/btop_linux.h rename to src/btop_linux.hpp index 64ea044..a871e4e 100644 --- a/src/btop_linux.h +++ b/src/btop_linux.hpp @@ -43,15 +43,14 @@ namespace Proc { //* Container for process information struct proc_info { - uint pid; + uint64_t pid; string name = "", cmd = ""; size_t threads = 0; string user = ""; uint64_t mem = 0; double cpu_p = 0.0, cpu_c = 0.0; char state = '0'; - int cpu_n = 0, p_nice = 0; - int ppid = -1; + uint64_t cpu_n = 0, p_nice = 0, ppid = 0; string prefix = ""; }; diff --git a/src/btop_menu.h b/src/btop_menu.hpp similarity index 100% rename from src/btop_menu.h rename to src/btop_menu.hpp diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index 9792c87..59ff663 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -23,9 +23,9 @@ tab-size = 4 #include #include -#include -#include -#include +#include +#include +#include using std::round, std::vector, robin_hood::unordered_flat_map, std::stoi, std::views::iota, std::array, std::clamp, std::max, std::min, std::ceil, std::to_string; diff --git a/src/btop_theme.h b/src/btop_theme.hpp similarity index 100% rename from src/btop_theme.h rename to src/btop_theme.hpp diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 85e6cbc..004209c 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -31,7 +31,7 @@ tab-size = 4 #include #include -#include +#include using std::string_view, std::array, std::regex, std::max, std::to_string, std::cin, std::atomic, robin_hood::unordered_flat_map; @@ -278,11 +278,10 @@ namespace Tools { newstr.reserve(str.size()); while ((pos = oldstr.find(' ')) != string::npos){ newstr.append(oldstr.substr(0, pos)); - oldstr.remove_prefix(pos+1); - pos = 1; - while (pos < oldstr.size() && oldstr.at(pos) == ' ') pos++; - newstr.append(Mv::r(pos)); - oldstr.remove_suffix(pos-1); + size_t x = 0; + while (pos + x < oldstr.size() && oldstr.at(pos + x) == ' ') x++; + newstr.append(Mv::r(x)); + oldstr.remove_prefix(pos + x); } return (newstr.empty()) ? str : newstr + (string)oldstr; } diff --git a/src/btop_tools.h b/src/btop_tools.hpp similarity index 100% rename from src/btop_tools.h rename to src/btop_tools.hpp