Skip to content

Commit

Permalink
ui/view: general code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Mar 12, 2016
1 parent 20db7ce commit 3ffd4e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
15 changes: 10 additions & 5 deletions ui-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,28 +670,33 @@ static void ui_window_draw(UiWin *w) {
UiCursesWin *win = (UiCursesWin*)w;
if (!ui_window_draw_sidebar(win))
return;

wbkgd(win->win, style_to_attr(&win->styles[UI_STYLE_DEFAULT]));
wmove(win->win, 0, 0);
int width = view_width_get(win->view);
CellStyle *prev_style = NULL;
size_t cursor_lineno = -1;

if (win->options & UI_OPTION_CURSOR_LINE && win->ui->selwin == win) {
Cursor *cursor = view_cursors(win->view);
Filerange selection = view_cursors_selection_get(cursor);
if (!view_cursors_next(cursor) && !text_range_valid(&selection))
cursor_lineno = view_cursor_getpos(win->view).line;
}

short selection_bg = win->styles[UI_STYLE_SELECTION].bg;
short cursor_line_bg = win->styles[UI_STYLE_CURSOR_LINE].bg;
bool multiple_cursors = view_cursors_multiple(win->view);
attr_t attr = A_NORMAL;

for (const Line *l = view_lines_get(win->view); l; l = l->next) {
bool cursor_line = l->lineno == cursor_lineno;
for (int x = 0; x < width; x++) {
unsigned int style_id = l->cells[x].attr;
enum UiStyles style_id = l->cells[x].style;
if (style_id == 0)
style_id = UI_STYLE_DEFAULT;
CellStyle *style = &win->styles[style_id];

if (l->cells[x].cursor && win->ui->selwin == win) {
if (multiple_cursors && l->cells[x].cursor_primary)
attr = style_to_attr(&win->styles[UI_STYLE_CURSOR_PRIMARY]);
Expand Down Expand Up @@ -725,6 +730,7 @@ static void ui_window_draw(UiWin *w) {
for (; 0 < x && x < width; x++)
waddstr(win->win, " ");
}

wclrtobot(win->win);

if (win->winstatus)
Expand Down Expand Up @@ -951,12 +957,11 @@ static UiWin *ui_window_new(Ui *ui, View *view, File *file, enum UiOption option
return NULL;
}

CellStyle style = (CellStyle) {
.fg = -1, .bg = -1, .attr = A_NORMAL,
};

for (int i = 0; i < UI_STYLE_MAX; i++) {
win->styles[i] = style;
win->styles[i] = (CellStyle) {
.fg = -1, .bg = -1, .attr = A_NORMAL,
};
}

win->styles[UI_STYLE_CURSOR].attr |= A_REVERSE;
Expand Down
14 changes: 7 additions & 7 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void view_syntax_color(View *view) {
for (bool token_next = false; line; line = line->next, col = 0) {
for (; col < line->width; col++) {
if (pos < token_end) {
line->cells[col].attr = token_style;
line->cells[col].style = token_style;
pos += line->cells[col].len;
} else {
token_next = true;
Expand Down Expand Up @@ -354,7 +354,7 @@ static bool view_addch(View *view, Cell *cell) {
cell->len = w == 0 ? 1 : 0;
int t = w == 0 ? SYNTAX_SYMBOL_TAB : SYNTAX_SYMBOL_TAB_FILL;
strncpy(cell->data, view->symbols[t]->symbol, sizeof(cell->data)-1);
cell->attr = view->symbols[t]->style;
cell->style = view->symbols[t]->style;
view->line->cells[view->col] = *cell;
view->line->len += cell->len;
view->line->width += cell->width;
Expand All @@ -373,7 +373,7 @@ static bool view_addch(View *view, Cell *cell) {
}

strncpy(cell->data, view->symbols[SYNTAX_SYMBOL_EOL]->symbol, sizeof(cell->data)-1);
cell->attr = view->symbols[SYNTAX_SYMBOL_EOL]->style;
cell->style = view->symbols[SYNTAX_SYMBOL_EOL]->style;

view->line->cells[view->col] = *cell;
view->line->len += cell->len;
Expand All @@ -393,13 +393,13 @@ static bool view_addch(View *view, Cell *cell) {
.data = { '^', ch == 127 ? '?' : ch + 64, '\0' },
.len = 1,
.width = 2,
.attr = cell->attr,
.style = cell->style,
};
}

if (ch == ' ') {
strncpy(cell->data, view->symbols[SYNTAX_SYMBOL_SPACE]->symbol, sizeof(cell->data)-1);
cell->attr = view->symbols[SYNTAX_SYMBOL_SPACE]->style;
cell->style = view->symbols[SYNTAX_SYMBOL_SPACE]->style;

}

Expand Down Expand Up @@ -650,7 +650,7 @@ void view_update(View *view) {

/* This screen line contains the cell we want to highlight */
if (line_cols >= view->colorcolumn) {
l->cells[(view->colorcolumn - 1) % view->width].attr = UI_STYLE_COLOR_COLUMN;
l->cells[(view->colorcolumn - 1) % view->width].style = UI_STYLE_COLOR_COLUMN;
line_cc_set = true;
}
}
Expand All @@ -661,7 +661,7 @@ void view_update(View *view) {

for (Line *l = view->lastline->next; l; l = l->next) {
strncpy(l->cells[0].data, view->symbols[SYNTAX_SYMBOL_EOF]->symbol, sizeof(l->cells[0].data));
l->cells[0].attr = view->symbols[SYNTAX_SYMBOL_EOF]->style;
l->cells[0].style = view->symbols[SYNTAX_SYMBOL_EOF]->style;
for (int x = 1; x < view->width; x++)
l->cells[x] = cell_blank;
l->width = 1;
Expand Down
4 changes: 2 additions & 2 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ typedef struct {
char data[16]; /* utf8 encoded character displayed in this cell (might be more than
one Unicode codepoint. might also not be the same as in the
underlying text, for example tabs get expanded */
unsigned int attr;
enum UiStyles style;/* style id used to display this cell */
bool selected; /* whether this cell is part of a selected region */
bool cursor; /* whether a cursor is currently located on the cell */
bool cursor_primary;
bool cursor_primary;/* whether it is the primary cursor located on the cell */
} Cell;

typedef struct Line Line;
Expand Down

0 comments on commit 3ffd4e7

Please sign in to comment.