Clean-up code

This commit is contained in:
Matúš Púll 2025-06-25 00:00:52 +02:00
parent 34cc427533
commit 9fd135e540
3 changed files with 40 additions and 21 deletions

View file

@ -9,7 +9,7 @@ void Editor::save() {
}
for(count_type i = 0; i < file.size(); ++i)
outfile << file.get_line(i, 0) << std::endl;
outfile << file.get_line(i, false) << '\n';
}
// Clear line
@ -39,8 +39,10 @@ string Editor::get_input(string prompt, F func) {
string s = "";
char ch = getch();
while((ch != ENTER && func(ch)) || ch == BS) {
if(ch == BS)
if(ch == BS) {
if(s.size())
s.pop_back();
}
else
s += ch;
print_text(cur.r, prompt+": " + s);
@ -55,7 +57,11 @@ string Editor::get_string(string prompt) {
}
// Taking user input - number
count_type Editor::get_number(string prompt) {
return stoi(get_input(prompt, [](char ch) { return ch >= '0' && ch <= '9'; }));
string inp = get_input(prompt, [](char ch) { return ch >= '0' && ch <= '9'; });
if(inp.size() > 0)
return stoi(inp);
else
return 0;
}
@ -79,7 +85,8 @@ void Editor::move_cursor(char ch) {
break;
case 'j':
if(file_offset + cur.r >= file.size()-1) break;
if(file_offset + cur.r >= file.size()-1)
break;
if(cur.r < LINES-1) {
cur.r += 1;
move(cur);
@ -129,7 +136,7 @@ void Editor::jump(count_type r) {
}
// Find next string appearance
std::pair<bool, position> Editor::find(string text) {
find_result Editor::find(string text) {
count_type len = text.size();
count_type file_size = file.size();
@ -185,7 +192,7 @@ bool Editor::take_action() {
break;
case 'i':
mode = INSERT;
current_insert = file.get_line(file_offset + cur.r, 0);
current_insert = file.get_line(file_offset + cur.r, false);
break;
case 'v':
mode = SELECT;
@ -196,9 +203,9 @@ bool Editor::take_action() {
case 'n':
{
auto result = find(last_find);
if(!result.first)
if(!result.found)
break;
jump(result.second);
jump(result.pos);
}
print_file(file_offset);
break;
@ -208,9 +215,9 @@ bool Editor::take_action() {
case 'r':
{
auto result = find(last_find);
if(!result.first)
if(!result.found)
break;
jump(result.second);
jump(result.pos);
replace(cur + file_offset, last_find.size(), last_replace);
}
print_file(file_offset);
@ -220,6 +227,7 @@ bool Editor::take_action() {
print_file(file_offset);
break;
default:
//print_text(cur.r, "Unknown operation.");
break;
}
break;
@ -233,8 +241,12 @@ bool Editor::take_action() {
break;
case BS:
if(cur.c > 0) {
cur.c -= 1;
current_insert.erase(cur.c - get_tab_offset(cur.c, current_insert), 1);
count_type new_c = cur.c;
do new_c--;
while(new_c > 0 && get_tab_offset(new_c, current_insert) > get_tab_offset(new_c-1, current_insert));
current_insert.erase(cur.c-1 - get_tab_offset(cur.c-1, current_insert), 1);
cur.c = new_c;
}
print_text(cur.r, substitute_tabs(current_insert));
break;
@ -267,6 +279,10 @@ bool Editor::take_action() {
jump(get_number("line number"));
print_file(file_offset);
break;
case 'G':
jump(file.size());
print_file(file_offset);
break;
case 'v':
selection.copy_selection(cur + file_offset);
mode = NORMAL;

View file

@ -5,6 +5,7 @@
#include <iostream>
#define TAB_SIZE 8
constexpr int RANDOM_SEED = 666;
using std::string;
using std::vector;
@ -32,7 +33,7 @@ struct line {
line(count_type _p, string _t) :
priority(_p), text(_t), size(1), left(nullptr), right(nullptr) {}
};
typedef std::pair<line*, line*> two_lines;
struct change { count_type row, count; };
// Treap data structure
class Treap {
@ -46,8 +47,8 @@ class Treap {
line* find(line *l, count_type k);
line* find(count_type k);
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
public:
public:
// General operations
Treap() { srand(120); root = nullptr; }
count_type size() { return get_size(root); }
@ -79,9 +80,10 @@ class Selection {
public:
Selection() {}
Selection(Treap *_file) { file = _file; }
position pos; // last selection position
position pos;
count_type size() { return file->size(); }
void remove_selection(position p);
void copy_selection(position p);
void paste_selection(position p);
};
@ -93,6 +95,7 @@ public:
#define BS 127
#define adds(s) addstr(s.c_str())
enum mode_type { INSERT, NORMAL, SELECT };
struct find_result { bool found; position pos; };
class Editor{
// Editor state
@ -132,8 +135,8 @@ class Editor{
void jump(position p) { jump(p.r); cur.c = p.c; }
// Find and replace
std::pair<bool, position> find(string text);
void replace(position p, count_type length, string text) { file.remove(p, length); file.insert(p, text); }
find_result find(string text);
void replace(position p, count_type length, string text);
public:
Editor(string _filename, Treap &_file) {

View file

@ -115,7 +115,7 @@ count_type Treap::bulk_find(vector<string> *vec, line *l, count_type k, count_ty
return 0;
}
// This is the wanted vertex
// This is a wanted vertex
else if(k == get_size(l->left)+1) {
vec->push_back(substitute_tabs(l->text));
if(count > 1)