Skip to content

Commit

Permalink
Convert the repo to use the new gapbuffer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ttibsi committed Nov 24, 2024
1 parent 0bea4da commit 5c1f0b4
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 174 deletions.
8 changes: 3 additions & 5 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ constexpr std::optional<const U> parse_action(View* v, const Action<T>& action)
if (active->current_char_in_line == 1 && active->current_line > 1) {
// At the start of the line, move cursor up
int prev_line_len = active->buf.line(active->get_abs_pos() - 1).size();
active->buf.erase(active->buf.begin() + active->get_abs_pos() - 2, 2);
active->buf.erase(2);
v->cursor_up();
for (int i = 0; i <= prev_line_len - 1; i++) {
v->cursor_right();
}
active->line_count--;
v->render_screen();
} else {
// Move cursor backwards
active->buf.erase(active->buf.begin() + active->get_abs_pos() - 1);
active->buf.pop_back();
v->render_line();
v->cursor_left();
}
Expand All @@ -100,12 +99,11 @@ constexpr std::optional<const U> parse_action(View* v, const Action<T>& action)
case ActionType::Newline: {
log("Action called: Newline");
Model* active = v->get_active_model();
active->buf.insert(active->buf.begin() + active->get_abs_pos(), "\r\n");
active->buf.insert("\r\n");
v->cursor_down();
while (active->current_char_in_line > 1) {
v->cursor_left();
}
active->line_count++;
v->render_screen();
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "action.h"
#include "constants.h"
#include "file_io.h"
#include "gapvector.h"
#include "gapbuffer.h"
#include "logger.h"

Controller::Controller() : term_size(rawterm::get_term_size()), view(View(this, term_size)) {
Expand Down Expand Up @@ -45,7 +45,7 @@ void Controller::create_view(const std::string& file_name) {
models.emplace_back();
} else {
log("Creating view from file: " + file_name);
std::optional<Gapvector<>> file_chars = open_file(file_name);
std::optional<Gapbuffer> file_chars = open_file(file_name);

if (file_chars.has_value()) {
models.emplace_back(file_chars.value(), file_name);
Expand Down
56 changes: 30 additions & 26 deletions src/file_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <filesystem>
#include <fstream>

[[nodiscard]] std::optional<Gapvector<>> open_file(const std::string& file) {
auto ret = Gapvector(1024);
[[nodiscard]] std::optional<Gapbuffer> open_file(const std::string& file) {
auto ret = Gapbuffer(1024);
char ch;

std::ifstream ifs(file);
Expand All @@ -26,34 +26,38 @@
ret.pop_back();
}

for (unsigned int i = 0; i <= ret.size(); i++) {
ret.retreat();
}

return ret;
}

[[nodiscard]] std::optional<Response> shell_exec(const std::string& cmd, bool output) {
namespace fs = std::filesystem;
std::string tmp_dir = fs::temp_directory_path();
int retcode = std::system(
(cmd + ">" + tmp_dir + "/iris_cmd_out.txt 2> " + tmp_dir + "/iris_cmd_err.txt").c_str());

if (output) {
auto stdout_contents = open_file(tmp_dir + "/iris_cmd_out.txt");

if (retcode) {
auto stderr_contents = open_file(tmp_dir + "/iris_cmd_err.txt");
if (stderr_contents.has_value()) {
return Response {"", stderr_contents.value().to_str(), retcode};
}
return Response {"", "No STDERR contents found", retcode};
}

return Response {stdout_contents.value().to_str(), "", 0};

} else {
return {};
}
}
// [[nodiscard]] std::optional<Response> shell_exec(const std::string& cmd, bool output) {
// namespace fs = std::filesystem;
// std::string tmp_dir = fs::temp_directory_path();
// int retcode = std::system(
// (cmd + ">" + tmp_dir + "/iris_cmd_out.txt 2> " + tmp_dir + "/iris_cmd_err.txt").c_str());
//
// if (output) {
// auto stdout_contents = open_file(tmp_dir + "/iris_cmd_out.txt");
//
// if (retcode) {
// auto stderr_contents = open_file(tmp_dir + "/iris_cmd_err.txt");
// if (stderr_contents.has_value()) {
// return Response {"", stderr_contents.value().to_str(), retcode};
// }
// return Response {"", "No STDERR contents found", retcode};
// }
//
// return Response {stdout_contents.value().to_str(), "", 0};
//
// } else {
// return {};
// }
// }

[[nodiscard]] std::size_t write_to_file(const std::string& file, Gapvector<> chars) {
[[nodiscard]] std::size_t write_to_file(const std::string& file, Gapbuffer chars) {
if (file == "NO FILE") {
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <optional>
#include <string>

#include "gapvector.h"
#include "gapbuffer.h"

struct Response {
std::string stdout;
Expand All @@ -16,8 +16,8 @@ struct Response {
}
};

[[nodiscard]] std::optional<Gapvector<>> open_file(const std::string&);
[[nodiscard]] std::optional<Response> shell_exec(const std::string&, bool);
[[nodiscard]] std::size_t write_to_file(const std::string&, Gapvector<>);
[[nodiscard]] std::optional<Gapbuffer> open_file(const std::string&);
// [[nodiscard]] std::optional<Response> shell_exec(const std::string&, bool);
[[nodiscard]] std::size_t write_to_file(const std::string&, Gapbuffer);

#endif // FILE_IO_H
15 changes: 9 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <string>

#include <cli11/CLI11.hpp>
#include <cpptrace/from_current.hpp>
#include <rawterm/core.h>

#include "controller.h"
Expand Down Expand Up @@ -36,15 +35,19 @@ int main(int argc, char* argv[]) {
rawterm::enable_raw_mode();
rawterm::enable_signals();

CPPTRACE_TRY {
// TODO: When we upgrade to c++23, use std::stacktrace here
// CPPTRACE_TRY {
try {
Controller c;
c.create_view(file);
c.start_action_engine();
}
CPPTRACE_CATCH(const std::exception& e) {
// } CPPTRACE_CATCH(const std::exception& e) {
} catch (const std::exception& e) {
rawterm::exit_alt_screen();
log(Level::ERROR, cpptrace::demangle(typeid(e).name()) + " " + e.what());
cpptrace::from_current_exception().print();
log(Level::ERROR, e.what());
throw e;
// log(Level::ERROR, cpptrace::demangle(typeid(e).name()) + " " + e.what());
// cpptrace::from_current_exception().print();
}

rawterm::exit_alt_screen();
Expand Down
31 changes: 21 additions & 10 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#include "file_io.h"

Model::Model() : buf(Gapvector()), line_count(0) {};
Model::Model() : buf(Gapbuffer()) {};

// TODO: readonly and modified
Model::Model(Gapvector<> file_chars, const std::string& filename)
: buf(file_chars), file_name(filename), line_count(std::count(buf.begin(), buf.end(), '\n')) {};
Model::Model(Gapbuffer file_chars, const std::string& filename)
: buf(file_chars), file_name(filename) {};

// 0-based position of the current character, used for insertion/deletion
[[nodiscard]] int Model::get_abs_pos() const {
int char_pos = buf.find_ith_char('\n', current_line - 1);
int char_pos = buf.find('\n', current_line - 1);
char_pos += current_char_in_line;
if (current_line == 1) {
char_pos--;
Expand All @@ -21,21 +21,32 @@ Model::Model(Gapvector<> file_chars, const std::string& filename)
}

[[nodiscard]] char Model::get_current_char() const {
return buf.at(get_abs_pos());
return buf.at(buf.pos());
}

[[nodiscard]] char Model::get_next_char() const {
return buf.at(get_abs_pos() + 1);
return buf.at(buf.pos() + 1);
}

[[nodiscard]] const std::string Model::get_current_line() const {
// The -1 is because we want the abs pos of the last char, not
// the cursor
return buf.line(std::max(get_abs_pos() - 1, 0));
return buf.line(buf.pos());
}

void Model::insert_char(char c) {
buf.insert(buf.begin() + get_abs_pos(), c);
buf.push_back(c);
current_char_in_line++;
}

void Model::line_up() {
for (unsigned int i = 0; i < buf.line(buf.pos()).size(); i++) {
buf.retreat();
}
}

void Model::line_down() {
for (unsigned int i = 0; i < buf.line(buf.pos()).size(); i++) {
buf.advance();
}
}

// TODO: Display "saved x bytes"
Expand Down
11 changes: 6 additions & 5 deletions src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

#include <string>

#include "gapvector.h"
#include "gapbuffer.h"

struct Model {
Gapvector<> buf;
Gapbuffer buf;
std::string file_name = "";
int line_count;
int current_line = 1;
unsigned int current_line = 1;
int current_char_in_line = 1;
bool readonly = false;
bool modified = false;
int vertical_file_offset = 0;

Model();
Model(Gapvector<>, const std::string&);
Model(Gapbuffer, const std::string&);
[[nodiscard]] int get_abs_pos() const;
[[nodiscard]] char get_current_char() const;
[[nodiscard]] char get_next_char() const;
[[nodiscard]] const std::string get_current_line() const;
void line_up();
void line_down();
void insert_char(char);
int save_file() const;
};
Expand Down
Loading

0 comments on commit 5c1f0b4

Please sign in to comment.