2021-05-09 00:56:48 +12:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2021-06-20 00:57:27 +12:00
|
|
|
#pragma once
|
2021-05-09 00:56:48 +12:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-21 13:17:34 +12:00
|
|
|
#include <robin_hood.h>
|
2021-05-29 12:32:36 +12:00
|
|
|
#include <filesystem>
|
2021-05-09 00:56:48 +12:00
|
|
|
|
2021-07-21 13:17:34 +12:00
|
|
|
using std::string, std::vector, robin_hood::unordered_flat_map;
|
2021-05-09 06:37:36 +12:00
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
//* Functions and variables for reading and writing the btop config file
|
|
|
|
namespace Config {
|
2021-06-12 10:35:15 +12:00
|
|
|
|
2021-06-20 00:57:27 +12:00
|
|
|
extern std::filesystem::path conf_dir;
|
|
|
|
extern std::filesystem::path conf_file;
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-07-21 13:17:34 +12:00
|
|
|
extern unordered_flat_map<string, string> strings;
|
2021-07-24 12:13:26 +12:00
|
|
|
extern unordered_flat_map<string, string> stringsTmp;
|
2021-07-21 13:17:34 +12:00
|
|
|
extern unordered_flat_map<string, bool> bools;
|
2021-07-24 12:13:26 +12:00
|
|
|
extern unordered_flat_map<string, bool> boolsTmp;
|
2021-07-21 13:17:34 +12:00
|
|
|
extern unordered_flat_map<string, int> ints;
|
2021-07-24 12:13:26 +12:00
|
|
|
extern unordered_flat_map<string, int> intsTmp;
|
2021-07-21 13:17:34 +12:00
|
|
|
|
|
|
|
const vector<string> valid_graph_symbols = { "braille", "block", "tty" };
|
2021-09-13 01:58:23 +12:00
|
|
|
const vector<string> valid_graph_symbols_def = { "default", "braille", "block", "tty" };
|
2021-08-16 09:20:55 +12:00
|
|
|
const vector<string> valid_boxes = { "cpu", "mem", "net", "proc" };
|
2021-09-13 01:58:23 +12:00
|
|
|
const vector<string> temp_scales = { "celsius", "fahrenheit", "kelvin", "rankine" };
|
2021-05-09 00:56:48 +12:00
|
|
|
|
2021-07-19 01:44:32 +12:00
|
|
|
extern vector<string> current_boxes;
|
2021-09-19 00:42:53 +12:00
|
|
|
extern vector<string> preset_list;
|
2021-10-18 09:26:43 +13:00
|
|
|
extern vector<string> available_batteries;
|
2021-09-19 00:42:53 +12:00
|
|
|
extern int current_preset;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
2021-12-14 01:45:22 +13:00
|
|
|
//* Check if string only contains space separated valid names for boxes
|
2021-07-24 12:13:26 +12:00
|
|
|
bool check_boxes(const string& boxes);
|
|
|
|
|
2021-07-30 09:40:56 +12:00
|
|
|
//* Toggle box and update config string shown_boxes
|
|
|
|
void toggle_box(const string& box);
|
|
|
|
|
2021-09-19 00:42:53 +12:00
|
|
|
//* Parse and setup config value presets
|
|
|
|
bool presetsValid(const string& presets);
|
|
|
|
|
|
|
|
//* Apply selected preset
|
|
|
|
void apply_preset(const string& preset);
|
|
|
|
|
2021-07-24 12:13:26 +12:00
|
|
|
bool _locked(const string& name);
|
2021-07-04 11:18:48 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Return bool for config key <name>
|
2021-07-21 13:17:34 +12:00
|
|
|
inline const bool& getB(const string& name) { return bools.at(name); }
|
2021-05-09 00:56:48 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Return integer for config key <name>
|
2021-07-21 13:17:34 +12:00
|
|
|
inline const int& getI(const string& name) { return ints.at(name); }
|
2021-05-09 00:56:48 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Return string for config key <name>
|
2021-07-21 13:17:34 +12:00
|
|
|
inline const string& getS(const string& name) { return strings.at(name); }
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-09-13 01:58:23 +12:00
|
|
|
string getAsString(const string& name);
|
|
|
|
|
|
|
|
extern string validError;
|
|
|
|
|
|
|
|
bool intValid(const string& name, const string& value);
|
|
|
|
bool stringValid(const string& name, const string& value);
|
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Set config key <name> to bool <value>
|
2021-07-24 12:13:26 +12:00
|
|
|
inline void set(const string& name, const bool& value) {
|
|
|
|
if (_locked(name)) boolsTmp.insert_or_assign(name, value);
|
|
|
|
else bools.at(name) = value;
|
|
|
|
}
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Set config key <name> to int <value>
|
2021-07-24 12:13:26 +12:00
|
|
|
inline void set(const string& name, const int& value) {
|
|
|
|
if (_locked(name)) intsTmp.insert_or_assign(name, value);
|
2021-08-04 10:11:50 +12:00
|
|
|
else ints.at(name) = value;
|
2021-07-24 12:13:26 +12:00
|
|
|
}
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Set config key <name> to string <value>
|
2021-07-24 12:13:26 +12:00
|
|
|
inline void set(const string& name, const string& value) {
|
|
|
|
if (_locked(name)) stringsTmp.insert_or_assign(name, value);
|
|
|
|
else strings.at(name) = value;
|
|
|
|
}
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-06-21 08:07:04 +12:00
|
|
|
//* Flip config key bool <name>
|
2021-07-24 12:13:26 +12:00
|
|
|
void flip(const string& name);
|
2021-06-06 11:41:36 +12:00
|
|
|
|
2021-07-21 13:17:34 +12:00
|
|
|
//* Lock config and cache changes until unlocked
|
2021-06-20 00:57:27 +12:00
|
|
|
void lock();
|
2021-06-06 11:41:36 +12:00
|
|
|
|
|
|
|
//* Unlock config and write any cached values to config
|
2021-06-20 00:57:27 +12:00
|
|
|
void unlock();
|
2021-06-05 11:41:24 +12:00
|
|
|
|
2021-06-12 10:35:15 +12:00
|
|
|
//* Load the config file from disk
|
2021-07-30 09:40:56 +12:00
|
|
|
void load(const std::filesystem::path& conf_file, vector<string>& load_warnings);
|
2021-06-10 05:47:49 +12:00
|
|
|
|
2021-06-12 10:35:15 +12:00
|
|
|
//* Write the config file to disk
|
2021-06-20 00:57:27 +12:00
|
|
|
void write();
|
2021-07-24 12:13:26 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|