Fix relative offset in line printing
This commit is contained in:
parent
d8981403ac
commit
7259c7ba7a
2 changed files with 17 additions and 25 deletions
36
editor.cpp
36
editor.cpp
|
@ -13,8 +13,8 @@ void Editor::save() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear line
|
// Clear line
|
||||||
void Editor::clear_line(count_type r) {
|
void Editor::clear_line(count_type i) {
|
||||||
::move(r - file_offset, 0);
|
::move(i, 0);
|
||||||
clrtoeol();
|
clrtoeol();
|
||||||
}
|
}
|
||||||
// Print any text
|
// Print any text
|
||||||
|
@ -32,16 +32,10 @@ void Editor::print_file(count_type start) {
|
||||||
else
|
else
|
||||||
clear_line(i);
|
clear_line(i);
|
||||||
}
|
}
|
||||||
// Print input
|
|
||||||
void Editor::print_input(string text) {
|
|
||||||
clear_line(file_offset + cur.r);
|
|
||||||
::move(cur.r, 0);
|
|
||||||
adds(text);
|
|
||||||
}
|
|
||||||
// Generic input taking
|
// Generic input taking
|
||||||
template <typename F>
|
template <typename F>
|
||||||
string Editor::get_input(string prompt, F func) {
|
string Editor::get_input(string prompt, F func) {
|
||||||
print_input(prompt+": ");
|
print_text(cur.r, prompt+": ");
|
||||||
string s = "";
|
string s = "";
|
||||||
char ch = getch();
|
char ch = getch();
|
||||||
while((ch != ENTER && func(ch)) || ch == BS) {
|
while((ch != ENTER && func(ch)) || ch == BS) {
|
||||||
|
@ -49,7 +43,7 @@ string Editor::get_input(string prompt, F func) {
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
else
|
else
|
||||||
s += ch;
|
s += ch;
|
||||||
print_input(prompt+": " + s);
|
print_text(cur.r, prompt+": " + s);
|
||||||
ch = getch();
|
ch = getch();
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
|
@ -154,7 +148,7 @@ bool Editor::take_action() {
|
||||||
char ch = getch();
|
char ch = getch();
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case NORMAL:
|
case NORMAL:
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case 'h': case 'j': case 'k': case 'l':
|
case 'h': case 'j': case 'k': case 'l':
|
||||||
move_cursor(ch);
|
move_cursor(ch);
|
||||||
|
@ -175,7 +169,7 @@ bool Editor::take_action() {
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
file.remove(cur + file_offset);
|
file.remove(cur + file_offset);
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
file.insert(file_offset + cur.r, "");
|
file.insert(file_offset + cur.r, "");
|
||||||
|
@ -197,12 +191,12 @@ bool Editor::take_action() {
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
last_find = get_string("to find");
|
last_find = get_string("to find");
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
case 'n':
|
case 'n':
|
||||||
{
|
{
|
||||||
auto result = find(last_find);
|
auto result = find(last_find);
|
||||||
if(!result.first) {
|
if(!result.first) {
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
jump(result.second);
|
jump(result.second);
|
||||||
|
@ -212,12 +206,12 @@ bool Editor::take_action() {
|
||||||
case 's':
|
case 's':
|
||||||
last_find = get_string("to find");
|
last_find = get_string("to find");
|
||||||
last_replace = get_string("to replace");
|
last_replace = get_string("to replace");
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
case 'r':
|
case 'r':
|
||||||
{
|
{
|
||||||
auto result = find(last_find);
|
auto result = find(last_find);
|
||||||
if(!result.first) {
|
if(!result.first) {
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
jump(result.second);
|
jump(result.second);
|
||||||
|
@ -239,7 +233,7 @@ bool Editor::take_action() {
|
||||||
case ESC:
|
case ESC:
|
||||||
mode = NORMAL;
|
mode = NORMAL;
|
||||||
// Change current row into current_insert TODO
|
// Change current row into current_insert TODO
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
case BS:
|
case BS:
|
||||||
// Modify current_insert TODO
|
// Modify current_insert TODO
|
||||||
|
@ -247,7 +241,7 @@ bool Editor::take_action() {
|
||||||
cur.c -= 1;
|
cur.c -= 1;
|
||||||
file.remove(cur + file_offset);
|
file.remove(cur + file_offset);
|
||||||
}
|
}
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
case ENTER:
|
case ENTER:
|
||||||
// Modify current_insert and update TODO
|
// Modify current_insert and update TODO
|
||||||
|
@ -260,18 +254,18 @@ bool Editor::take_action() {
|
||||||
// Modify current_insert TODO
|
// Modify current_insert TODO
|
||||||
file.insert(cur + file_offset, "\t");
|
file.insert(cur + file_offset, "\t");
|
||||||
cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
|
cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Modify current_insert TODO
|
// Modify current_insert TODO
|
||||||
file.insert(cur + file_offset, string(1, ch));
|
file.insert(cur + file_offset, string(1, ch));
|
||||||
cur.c += 1;
|
cur.c += 1;
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SELECT:
|
case SELECT:
|
||||||
print_line(file_offset + cur.r);
|
print_current_line();
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case 'h': case 'j': case 'k': case 'l':
|
case 'h': case 'j': case 'k': case 'l':
|
||||||
move_cursor(ch);
|
move_cursor(ch);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <curses.h>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -104,12 +103,11 @@ class Editor{
|
||||||
void save();
|
void save();
|
||||||
|
|
||||||
// Printing
|
// Printing
|
||||||
void clear_line(count_type r);
|
void clear_line(count_type i);
|
||||||
void print_text(count_type i, string text);
|
void print_text(count_type i, string text);
|
||||||
void print_line(count_type r) { print_text(r - file_offset, file.get_line(r)); }
|
void print_current_line() { print_text(cur.r, file.get_line(file_offset + cur.r)); }
|
||||||
|
|
||||||
// User input
|
// User input
|
||||||
void print_input(string text);
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
string get_input(string prompt, F func);
|
string get_input(string prompt, F func);
|
||||||
string get_string(string prompt);
|
string get_string(string prompt);
|
||||||
|
|
Loading…
Reference in a new issue