Skip to content

Commit

Permalink
better buffer opening and displaying, maybe fixing #40
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurbacci committed Sep 4, 2024
1 parent 1ff17ae commit 53a8d92
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
21 changes: 12 additions & 9 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,33 @@ void display_buffer(Buffer buf, int len_line_number) {

if (size + gw > screen_sz) {
attron(A_REVERSE);
while (size < screen_sz) {
for (; size < screen_sz; size++)
addch('<');
size++;
}
attroff(A_REVERSE);

break;
}

if (1 == grapheme.sz && !isprint(*grapheme.dt))
grapheme = replacement_character();

if (1 == grapheme.sz && '\t' == *grapheme.dt) {
/*attron(A_REVERSE);
addch('T');
for (int k = 0; k < config.tablen - 1; k++)
addch(' ');
attroff(A_REVERSE);*/
attron(A_REVERSE);

for (int k = 0; k < config.tablen; k++)
addch(' ');

size += config.tablen;
} else if (is_replacement_character(grapheme)) {
attron(A_REVERSE);

addch('X');
} else {
printw("%.*s", (int)grapheme.sz, grapheme.dt);

size += grapheme_width(grapheme);
}


attroff(A_REVERSE);
}

Expand Down
20 changes: 14 additions & 6 deletions src/grapheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Grapheme get_next_grapheme(char **str, size_t len) {
return grapheme;
}

void print_grapheme_width_debug() {
}

size_t grapheme_width(Grapheme g) {
// TODO: the current mechanism of getting the width of a grapheme cluster
Expand All @@ -53,10 +51,6 @@ size_t grapheme_width(Grapheme g) {
// Resources that may be useful:
// - https://www.unicode.org/reports/tr51/

if (1 == g.sz && *g.dt == '\t')
return config.tablen;


for (size_t off = 0; off < g.sz; ) {
uint_least32_t cp;

Expand Down Expand Up @@ -125,4 +119,18 @@ size_t index_by_width(size_t wi, char **s) {
return wi;
}

bool is_replacement_character(Grapheme g) {
return g.sz == 3
&& g.dt[0] == (char)0xef
&& g.dt[1] == (char)0xbf
&& g.dt[2] == (char)0xbd;
}

Grapheme replacement_character(void) {
static unsigned char dt[3] = {0xef, 0xbf, 0xbd};
Grapheme g = {3, (char *)dt};

return g;
}


28 changes: 12 additions & 16 deletions src/open_and_save.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,27 @@ Buffer read_lines(FILE *fp, char *filename, bool can_write) {
}

b.num_lines = 0;
for (size_t i = 0; !feof(fp); i++) {
for (size_t ln_num = 0; !feof(fp); ln_num++) {
b.lines = realloc(b.lines, ++b.num_lines * sizeof(Line));
b.lines[i] = blank_line();
Line *curln = &b.lines[i];
b.lines[ln_num] = blank_line();
Line *curln = &b.lines[ln_num];


int c;
size_t j;
for (j = 0; EOF != (c = fgetc(fp)) && '\n' != c; j++) {
if (c == '\r')
for (int c; EOF != (c = fgetc(fp)) && '\n' != c; curln->length++) {
if ('\r' == c) {
b.crlf = true;
continue;
}

// TODO: check unicode if file isn't tooooo big
// For now the buffer is not being asserted to be encoded correctly

if (++curln->length >= curln->cap) {
if (curln->length + 1 >= curln->cap) {
curln->cap *= 2;
curln->data = realloc(curln->data, curln->cap * sizeof(Line));
curln->data = realloc(curln->data, curln->cap * sizeof(char));
}
curln->data[j] = c;
curln->data[curln->length] = c;
}

curln->data[j] = '\0';
curln->data[curln->length] = '\0';
}


if (fp) fclose(fp);

if (b.num_lines > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/ted.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ size_t wi_to_gi(size_t si, char *s);
size_t gi_to_wi(size_t gi, char *s);
ssize_t index_by_width_after(size_t _wi, char **s);
size_t index_by_width(size_t wi, char **s);
bool is_replacement_character(Grapheme g);
Grapheme replacement_character(void);


extern GlobalCfg config;
Expand Down

0 comments on commit 53a8d92

Please sign in to comment.