More file reorganizations

This commit is contained in:
aristocratos 2021-06-19 22:48:31 +02:00
parent d459d088a0
commit 3e92a5e89a
14 changed files with 126 additions and 78 deletions

View File

@ -31,17 +31,18 @@ tab-size = 4
#include <unistd.h>
#include <robin_hood.h>
#include <cmath>
#include <iostream>
#include <btop_tools.h>
#include <btop_config.h>
#include <btop_input.h>
#include <btop_theme.h>
#include <btop_draw.h>
#include <btop_menu.h>
#include <btop_tools.hpp>
#include <btop_config.hpp>
#include <btop_input.hpp>
#include <btop_theme.hpp>
#include <btop_draw.hpp>
#include <btop_menu.hpp>
#if defined(__linux__)
#define LINUX
#include <btop_linux.h>
#include <btop_linux.hpp>
#elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__)
#include <sys/param.h>
#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){

View File

@ -22,8 +22,8 @@ tab-size = 4
#include <atomic>
#include <fstream>
#include <btop_config.h>
#include <btop_tools.h>
#include <btop_config.hpp>
#include <btop_tools.hpp>
using robin_hood::unordered_flat_map, std::map, std::array, std::atomic;
namespace fs = std::filesystem;

View File

@ -22,10 +22,10 @@ tab-size = 4
#include <algorithm>
#include <cmath>
#include <btop_draw.h>
#include <btop_config.h>
#include <btop_theme.h>
#include <btop_tools.h>
#include <btop_draw.hpp>
#include <btop_config.hpp>
#include <btop_theme.hpp>
#include <btop_tools.hpp>
using robin_hood::unordered_flat_map, std::round, std::views::iota,
std::string_literals::operator""s, std::clamp, std::array, std::floor;

View File

@ -22,9 +22,6 @@ tab-size = 4
#include <vector>
#include <robin_hood.h>
#include <btop_config.h>
#include <btop_tools.h>
using std::string, std::vector, robin_hood::unordered_flat_map;
namespace Symbols {

View File

@ -16,13 +16,12 @@ indent = tab
tab-size = 4
*/
#pragma once
#include <string>
#include <robin_hood.h>
#include <iostream>
#include <btop_tools.h>
#include <btop_input.hpp>
#include <btop_tools.hpp>
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 <timeout> 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);
}

47
src/btop_input.hpp Normal file
View File

@ -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 <string>
/* 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 <timeout> 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();
}

View File

@ -32,9 +32,9 @@ tab-size = 4
#include <unistd.h>
#include <btop_linux.h>
#include <btop_config.h>
#include <btop_tools.h>
#include <btop_linux.hpp>
#include <btop_config.hpp>
#include <btop_tools.hpp>
@ -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;

View File

@ -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 = "";
};

View File

@ -23,9 +23,9 @@ tab-size = 4
#include <ranges>
#include <algorithm>
#include <btop_tools.h>
#include <btop_config.h>
#include <btop_theme.h>
#include <btop_tools.hpp>
#include <btop_config.hpp>
#include <btop_theme.hpp>
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;

View File

@ -31,7 +31,7 @@ tab-size = 4
#include <termios.h>
#include <sys/ioctl.h>
#include <btop_tools.h>
#include <btop_tools.hpp>
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;
}