Skip to content

Commit

Permalink
fix: last line wrapping, potential segfaults on syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
ikozyris committed Jul 1, 2024
1 parent e4e8a6c commit c02e82c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Yocto
[![C/C++ CI](https://github.com/ikozyris/yocto/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/ikozyris/yocto/actions/workflows/c-cpp.yml)
A simple, compact and *fast* text editor using ncurses and a gap buffer, written in C++, in <1k lines of code.
More information is available on the [wiki](https://github.com/ikozyris/yocto/wiki).

![A C++ file in Yocto](https://github.com/ikozyris/yocto/assets/80053394/8fa12952-272f-41e0-9535-0a77f3652286)

## How to use
## Usage
```
make build
make install
sudo make install
```

Or use the user-friendly dialog utility (`wizard.sh`)
Expand Down
2 changes: 1 addition & 1 deletion headers/gapbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ char *data(const gap_buf &a, const unsigned from, const unsigned to)
if (a.len == 0)
return 0;
char *tmp = (char*)malloc(to - from + 10);
bzero(tmp, to - from);
bzero(tmp, to - from + 1);
// try some special cases where 1 copy is required
if (a.gps == a.len && a.gpe == a.cpt) // gap ends at end so don't bother
memcpy(tmp, a.buffer + from, min(to, a.gps));
Expand Down
5 changes: 2 additions & 3 deletions headers/vars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ unsigned maxy, maxx; // to store the maximum rows and columns
int ch;
char *filename;

// indx: tmp for lenght of line
// curnum: total lines
size_t indx, curnum;
// total lines
size_t curnum;
16 changes: 7 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc, char *argv[])
mvwprintw(ln_win, maxy - 1, 0, "%3d", ry + 2);
wrefresh(ln_win);
wmove(text_win, y, 0);
print_line_no_nl(*it);
print_line(*it);
#ifdef HIGHLIGHT
wmove(text_win, y, 0);
apply(y);
Expand Down Expand Up @@ -140,10 +140,10 @@ int main(int argc, char *argv[])
break;

case LEFT:
if (x == 0 && rx == maxx - 1) { // line has been wrapped
if (x == 0 && rx >= maxx - 1) { // line has been wrapped
wmove(text_win, y, 0);
wclrtoeol(text_win);
print_line_no_nl(*it);
print_line(*it);
#ifdef HIGHLIGHT
wmove(text_win, y, 0);
apply(y);
Expand All @@ -157,7 +157,7 @@ int main(int argc, char *argv[])
break;

case RIGHT:
if (x == maxx - 1 && rx < it->len - 1) {
if (x == maxx - 1 && x < it->len - 1) {
wmove(text_win, y, 0);
wclrtoeol(text_win);
// printline() with custom start
Expand Down Expand Up @@ -236,15 +236,13 @@ int main(int argc, char *argv[])
wclear(text_win);
goto read;
} else if (ch == OFF) { // calculate x offset
wmove(text_win, y, 0);
wchar_t temp[256];
bzero(temp, 256 * sizeof(wchar_t));
if (winwstr(text_win, temp) == ERR)
break;
ofx = sizeofline(y);
x = ofx;
print2header(itoa(ofx), 1);
ofx = (long)it->len - (long)ofx;
x = sizeofline(y);
print2header(itoa(x), 1);
ofx = (long)it->len - (long)x;
print2header(itoa(ofx), 2);
}
wtimeout(text_win, -1);
Expand Down
13 changes: 7 additions & 6 deletions utils/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ const char *types[] = {"int", "char", "float", "double", "unsigned", "void", "co
const char *defs[] = {"if", "else", "while", "for", "do", "return", "sizeof", "switch",
"goto", "case", "break", "struct", "default", "continue", "true", "false"};
const char *oper[] = {"=", "+=", "+", "-", "-=", "*", "*=", "/", "/=", "%%", "&", "++",
"--", "==", "<", ">", "\'"};
"--", "==", "<", ">", "[", "]"};

#define DEFINC COLOR_CYAN
#define COMMENT COLOR_GREEN
#define TYPES COLOR_RED
#define OPER COLOR_YELLOW
#define DEFS COLOR_BLUE
#define STR COLOR_MAGENTA
// TODO: color for numbers?

#define nelems(x) (sizeof(x) / sizeof((x)[0]))
#define isvalid(ch) (ch < 65 || ch > 122)

struct res_t {
short len;
unsigned short len;
char type;
};

Expand Down Expand Up @@ -77,15 +78,15 @@ void apply(unsigned line)
wchgat(text_win, -1, 0, DEFINC, 0);
return;
} // comments
else if (str[i] == '/' && str[i + 1] == '/') {
else if (str[i] == '/' && str[i + 1] == '/' && i < maxx) {
wmove(text_win, line, i);
wchgat(text_win, -1, 0, COMMENT, 0);
return;
} else if (str[i] == '/' && str[i + 1] == '*') {
} else if (str[i] == '/' && str[i + 1] == '*' && i < maxx) {
previ = i;
i += 2;
wmove(text_win, line, previ);
while (str[i] != '*' && str[i + 1] != '/')
while (str[i] != '*' && str[i + 1] != '/' && i < maxx)
++i;
wchgat(text_win, i++ - previ + 2, 0, COMMENT, 0);
} // string / char
Expand All @@ -98,7 +99,7 @@ void apply(unsigned line)
} else if (str[i] == '\"') {
previ = i++;
wmove(text_win, line, previ);
while (str[i] != '\"')
while (str[i] != '\"' && i < maxx)
++i;
wchgat(text_win, i - previ + 1, 0, STR, 0);
} // type (int, char) / keyword (if, return) / operator (==, +)
Expand Down
15 changes: 9 additions & 6 deletions utils/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ char *input_header(const char *q)
return tmp;
}

#define print_line(a) {char *t = data((a), 0, maxx); waddnstr(text_win, t, maxx); free(t);}
#define print_line_no_nl(a) {char *t = data((a), 0, maxx-1); waddnstr(text_win, t, min((a).len-1, maxx-1)); free(t);}

#define print_line(a) {char *t = data((a), 0, (a).len-1);\
waddnstr(text_win, t, min(get_offset(a), ((long)maxx + ((long)(a).len - get_offset(a))))-1);free(t);}

void print_text()
{
std::list<gap_buf>::iterator iter = text.begin();
std::advance(iter, ofy);
wclear(text_win);
unsigned ty = 0;
for (unsigned char i = ofy; i < ofy + min(txt_cpt, maxy ) && iter != text.end(); ++i, ++iter) {
print_line_no_nl((*iter));
for (unsigned ty = 0; ty < min(curnum, maxy) && iter != text.end(); ++iter) {
print_line(*iter);
#ifdef HIGHLIGHT
wmove(text_win, ty, 0);
apply(ty);
Expand All @@ -59,7 +59,7 @@ unsigned print_text_w(unsigned start)
{
wmove(text_win, 0, 0);
buf_indx = start;
while ((unsigned)getcury(text_win) < maxy-1 && buf_indx < it->len)
while ((unsigned)getcury(text_win) < maxy - 1 && buf_indx < it->len)
waddch(text_win, it->buffer[buf_indx++]);
wclrtobot(text_win);
return buf_indx - start; // how many characters were printed
Expand Down Expand Up @@ -134,4 +134,7 @@ void read_fread(FILE *fi)
while ((a = fread(tmp, sizeof(tmp[0]), SZ, fi)))
for (unsigned i = 0; i < a; ++i)
ins_b(tmp[i]);
// TODO: find a proper way (in print_line)
ins_b('\n');
--curnum;
}
18 changes: 16 additions & 2 deletions utils/sizes.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const char *itoa(long a)
return b;
}

// convert bytes to human-readable string e.g 1024 = 1.024KB = 1KiB
// convert bytes to base-10 human-readable string e.g 1024 = 1.024KB = 1KiB
char *hrsize(size_t bytes)
{
char *suffix[] = {(char*)"B", (char*)"KB", (char*)"MB",
Expand All @@ -32,4 +32,18 @@ unsigned sizeofline(unsigned y) {
while ((winch(text_win) & A_CHARTEXT) == ' ')
wmove(text_win, y, --i);
return i+2;
}
}

// get length of line with offset by tabs
long get_offset(gap_buf &buffer)
{
if (buffer.len == 0)
return 0;
char *rbuff = data(buffer, 0, buffer.len-1);
unsigned rlen = buffer.len;
for (unsigned i = 0; i < buffer.len && rbuff[i] != 0; ++i)
if (rbuff[i] == L'\t')
rlen += 7;

return rlen;
}

0 comments on commit c02e82c

Please sign in to comment.