added config file

development
Michael Ochmann 8 years ago
parent 0b66d839be
commit 097143ecf7
  1. 2
      CMakeLists.txt
  2. 9
      src/Buffer.hpp
  3. 7
      src/Editor.cpp
  4. 2
      src/Editor.hpp
  5. 2
      src/Highlighter.cpp
  6. 72
      src/config/ConfigParser.cpp
  7. 79
      src/config/ConfigParser.hpp
  8. 4
      src/modes/Edit.cpp
  9. 1
      src/modes/Save.cpp

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.7)
project(groove C CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
include_directories(src)

@ -15,7 +15,8 @@ namespace groove {
this->removeTabs(line.replace(tab, 1, " "));
}
public:
Buffer() : lines(std::vector<std::string>()) {}
bool changed;
Buffer() : lines(std::vector<std::string>()), changed(false) {}
std::vector<std::string>& linebuffer() {
return this->lines;
}
@ -23,26 +24,32 @@ namespace groove {
void insert(std::string str, unsigned long line) {
this->lines.insert(this->lines.begin() + line, str);
this->removeTabs(this->lines.at(line));
this->changed = true;
}
void insert(std::string line) {
this->removeTabs(line);
this->lines.emplace_back(line);
this->changed = true;
}
void insert(unsigned long after) {
this->lines.insert(this->lines.begin() + after + 1, "");
this->changed = true;
}
void remove(unsigned long line) {
this->lines.erase(this->lines.begin() + line);
this->changed = true;
}
void remove(unsigned long line, int car) {
this->lines.at(line).erase(this->lines.at(line).begin() + car);
this->changed = true;
}
void deleteChar(unsigned long line, int car) {
this->lines.at(line).erase(this->lines.at(line).begin() + car);
this->changed = true;
}
std::string& at(unsigned long line) {

@ -8,13 +8,16 @@ namespace groove {
Editor::Editor(std::string filename) : x(0), y(0),
buffer(std::make_unique<Buffer>()), mode_(Mode::EDIT),
filename(filename), offset(0), voffset(0), lineMode(LineMode::RELATIVE),
history(History()), inComment(false) {
filename(filename), offset(0), voffset(0),
history(History()), inComment(false), config(config::ConfigParser()) {
this->modes.emplace(Mode::INSERT, std::make_unique<modes::Insert>(*this));
this->modes.emplace(Mode::EDIT, std::make_unique<modes::Edit>(*this));
this->modes.emplace(Mode::QUIT, std::make_unique<modes::Quit>(*this));
this->modes.emplace(Mode::SAVE, std::make_unique<modes::Save>(*this));
this->lineMode = this->config.get<std::string>("linenumbers") == "relative" ? LineMode::RELATIVE :
this->config.get<std::string>("linenumbers") == "none" ? LineMode::NONE : LineMode::NUMBERS;
this->overlay = newwin(LINES * 0.9, COLS * 0.9, (LINES * 0.1f) / 2.0f, (COLS * 0.1f) / 2.0f);
box(this->overlay, 0, 0);

@ -9,6 +9,7 @@
#include <modes/Quit.hpp>
#include <modes/Save.hpp>
#include <History.hpp>
#include <config/ConfigParser.hpp>
namespace groove {
@ -48,6 +49,7 @@ namespace groove {
std::string clipboard = "";
std::string filename;
std::unordered_map<Mode, std::unique_ptr<modes::Mode>> modes;
config::ConfigParser config;
bool load();
bool save();

@ -7,7 +7,7 @@ namespace groove {
std::vector<std::pair<std::regex, ncurses::Colors>> Highlighter::list = {
make_pair("([+-.<>,;=!:&*])", ncurses::Colors::CYAN),
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
make_pair("(^|\\s)(while|if|try|catch|void|this|else|using|namespace|private|public|protected|friend|class|char|bool|unsigned|long|short|int|return)\\*?", ncurses::Colors::MAGENTA),
make_pair("(^|\\s)(while|if|try|catch|void|this|else|using|namespace|private|public|protected|friend|class|char|bool|unsigned|long|short|int|return)\\s+\\*?", ncurses::Colors::MAGENTA),
make_pair("([a-zA-Z_][a-zA-Z_0-9]+)::", ncurses::Colors::GREEN),
make_pair("::([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
make_pair("\\.([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),

@ -0,0 +1,72 @@
#include <config/ConfigParser.hpp>
#include <fstream>
namespace groove {
namespace config {
void ConfigValue::set(std::string &value) {
strVal = value;
boolVal = value == "true";
try {
intVal = std::stoi(value);
} catch (std::invalid_argument) {
intVal = 0;
}
try {
floatVal = std::stof(value);
} catch (std::invalid_argument) {
floatVal = 0.0f;
}
}
void ConfigParser::parse() {
std::ifstream file(this->filename.c_str());
if(file.is_open()) {
while(!file.eof()) {
std::string line;
std::getline(file, line);
if (line == "" || line.at(0) == '#')
continue;
std::vector<std::string> pair = std::explode(line,'=');
std::string key = pair.at(0);
std::string value = pair.at(1);
std::trim(key);
std::trim(value);
this->params.emplace(key, ConfigValue(value));
}
}
else {
std::cerr << "Could not load config file." << std::endl;
}
}
template<>
std::string ConfigParser::get<std::string>(const std::string& key) {
if (this->params.find(key) == this->params.end())
return "";
return this->params.at(key).strVal;
}
template<>
int ConfigParser::get<int>(const std::string& key) {
if (this->params.find(key) == this->params.end())
return 0;
return this->params.at(key).intVal;
}
template<>
float ConfigParser::get<float>(const std::string& key) {
if (this->params.find(key) == this->params.end())
return 0.0f;
return this->params.at(key).floatVal;
}
template<>
bool ConfigParser::get<bool>(const std::string& key) {
if (this->params.find(key) == this->params.end())
return false;
return this->params.at(key).boolVal;
}
}
}

@ -0,0 +1,79 @@
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
namespace std {
static inline std::vector<std::string> explode(const std::string &str, char delimiter) {
std::vector<std::string> tokens;
std::stringstream inStream(str);
std::string tempString;
while (std::getline(inStream, tempString, delimiter))
tokens.push_back(tempString);
return tokens;
}
static inline void ltrim(std::string &string) {
string.erase(string.begin(), std::find_if(string.begin(), string.end(), [](auto c){return !std::isspace(c);}));
}
static inline void rtrim(std::string &string) {
string.erase(std::find_if(string.rbegin(), string.rend(), [](auto c){return !std::isspace(c);}).base(), string.end());
}
static inline void trim(std::string &string) {
rtrim(string);
ltrim(string);
}
}
namespace groove {
namespace config {
struct ConfigValue {
std::string strVal;
int intVal;
float floatVal;
bool boolVal;
ConfigValue(std::string &value) {
set(value);
}
void set(std::string &value);
};
class ConfigParser {
private:
std::unordered_map<std::string, ConfigValue> params;
std::string filename;
void parse();
public:
ConfigParser() {
this->filename = getenv("HOME");
this->filename += "/.grooverc";
this->parse();
}
template <typename T>
T get(const std::string& key) {}
template<typename T>
void set(std::string key, T value) {
std::string newValue = std::to_string(value);
this->params.at(key).set(newValue);
}
};
}
}

@ -1,11 +1,11 @@
#include <modes/Edit.hpp>
#include <Editor.hpp>
#include <ncurses.h>
namespace groove {
namespace modes {
void Edit::input(int c) {
this->status_ = this->editor.buffer->changed ? "*" : "";
if (this->editor.movement(c))
return;
switch (c) {
@ -60,6 +60,8 @@ namespace groove {
break;
case 'd':
{
if (this->editor.y >= this->editor.buffer->size() - 1)
break;
long y = this->editor.y;
std::string line = this->editor.buffer->at(y);
this->editor.history.push(

@ -14,6 +14,7 @@ namespace groove {
case 'y':
if (!this->editor.save())
std::cerr << "Could not write to file: '" << this->editor.filename << "'\n";
this->editor.buffer->changed = false;
this->editor.mode_ = groove::Mode::EDIT;
break;
}

Loading…
Cancel
Save