чување e76d553f1b57dbb3a869f8f10f3cfffd795c867a
родитељ b96e9c288f0b59eff244a6130e3e7c01b30b08bf
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Thu, 8 Jul 2021 19:05:21 +0200
Continued work on u32_draw_string wrt tabs, p3
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | draw.c | | | 165 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- |
| M | draw.h | | | 4 | ++++ |
| M | poe.c | | | 67 | +++++++++++-------------------------------------------------------- |
измењених датотека: 3, додавања: 135(+), брисања: 101(-)
diff --git a/draw.c b/draw.c
@@ -186,6 +186,52 @@ split(const uint32_t* s)
return newbuf;
}
+/* Calculates display length of an input buffer line.
+ * max_index = the input index up to which the length is calculated
+ */
+size_t
+display_length(const struct DrawState* state, uint32_t* buffer,
+ const size_t max_index)
+{
+ size_t len = 0;
+ uint32_t* pbuf = buffer;
+
+ for (size_t i = 0; *pbuf && i < max_index; i++)
+ {
+ pbuf = buffer + i;
+ if (*pbuf == '\t')
+ len += TAB_SIZE - (len % TAB_SIZE);
+ else
+ len++;
+ }
+
+ return len;
+}
+
+/* Calculates input length from a display length.
+ * max_index = the display index up to which the length is calculated
+ */
+size_t
+input_length(const struct DrawState* state, uint32_t* buffer,
+ const size_t max_index)
+{
+ size_t len = 0;
+ uint32_t* pbuf = buffer;
+ size_t i = 0;
+
+ for (i = 0; *pbuf && len < max_index; i++)
+ {
+ pbuf = buffer + i;
+ if (*pbuf == '\t')
+ len += MIN(TAB_SIZE - (len % TAB_SIZE),
+ max_index - len);
+ else
+ len++;
+ }
+
+ return i;
+}
+
void
draw_rect(const int startx, const int endx, const int starty, const int endy,
const uint16_t fg, const uint16_t bg)
@@ -251,9 +297,9 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const Alignment align, const int display_starts_at)
{
int col = 0;
- const uint32_t* ps = NULL;
+ int c = display_starts_at;
+ const uint32_t* ps = s;
size_t len = u32_strlen(s);
- ps = s;
if (align == LEFT)
{
@@ -263,7 +309,7 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
}
else if (align == RIGHT)
{
- for (col = max_cols-fill_cols-1; col < max_cols; col++)
+ for (col = max_cols-1-fill_cols; col < max_cols; col++)
tb_change_cell(x+col, y, ' ', fg, bg);
col = max_cols-len-padding_right-1;
}
@@ -277,7 +323,7 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
{
if (len > max_cols)
{
- ps += (len-max_cols)/2;
+ c = (len-max_cols)/2;
col = 0;
}
else
@@ -285,54 +331,80 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
}
}
- while (ps && *ps && col < display_starts_at+max_cols-padding_right)
+ while (ps && *ps && col < c+max_cols-padding_right)
{
if (*ps == '\t')
{
- if (col < display_starts_at)
+ int start = col % TAB_SIZE;
+ int end = TAB_SIZE - (col % TAB_SIZE);
+ if (col < c)
{
- if (col+TAB_SIZE >= display_starts_at)
+ /* col+TAB_SIZE
+ * \t |
+ * ------[ :::|: ]
+ * | | c+padding_left=c+2
+ * | c
+ * col
+ * */
+ if (end >= c)
{
- for (int j = 0; j <
- TAB_SIZE-(display_starts_at
- -col); j++)
+ for (int j = 0; j < end-c+padding_left;
+ j++)
tb_change_cell(x+padding_left+
- col+j, y, ' ', fg, bg);
- col += TAB_SIZE;
+ j, y, ' ', fg, bg);
}
- else
+ /*
+ * \t
+ * ----------|----[ ::::: ]
+ * | | |
+ * | | c
+ * | col+TAB_SIZE
+ * col
+ * */
+ col += end-start;
+ }
+ else
+ {
+ /*
+ * col+TAB_SIZE = col+end
+ * \t |
+ * [ ::::::: ]---|-----
+ * | |
+ * | dlimit
+ * col = (col - start) + start
+ * */
+ int dlimit = max_cols-padding_right;
+ if (col + end >= dlimit)
{
- int limit = MIN(display_starts_at+
- max_cols-padding_right-col,
- TAB_SIZE);
- for (int j = 0; j < limit; j++)
+ int spaces = dlimit - col;
+ for (int j = 0; j < spaces; j++)
tb_change_cell(x+padding_left+
col+j, y, ' ', fg, bg);
- col += limit;
+ col += spaces;
+ }
+ /*
+ * \t
+ * [ :::::::::::|::: ]-----
+ * | | | |
+ * | | | dlimit
+ * | | col+TAB_SIZE
+ * | col
+ * c
+ * */
+ else
+ {
+ for (int j = start; j < end; j++)
+ tb_change_cell(x+padding_left+
+ (col-start)+j, y, ' ',
+ fg, bg);
+ col += end-start;
}
}
- else
- {
- if (col >= display_starts_at)
- tb_change_cell(x+padding_left+col,
- y, *ps, fg, bg);
- col++;
- }
- /*
- *int i = 0;
- *for (i = 0; i < TAB_SIZE -
- * ((col - display_starts_at) % TAB_SIZE)
- * && col+i < max_cols-padding_right; i++)
- * tb_change_cell(x+padding_left+col+i, y, ' ',
- * fg, bg);
- *col += i;
- *ps++;
- */
}
else
{
- if (col >= display_starts_at)
- tb_change_cell(x+padding_left+col, y, *ps,
+ if (col >= c)
+ tb_change_cell(x+padding_left+col-c, y, *ps,
fg, bg);
col++;
}
@@ -387,6 +459,7 @@ draw_editbox(const struct DrawState* state)
//uint32_t buf[MSGSTR_MAX];
char flags_buf[MAXFLAGSBUF+4];
struct PoEntry* entry = &state->entries[state->msgid_number-1];
+ size_t dlen = 0;
strcpy(flags_buf, "[ ");
format_flags(flags_buf+2, MAXFLAGSBUF, entry);
@@ -417,12 +490,13 @@ draw_editbox(const struct DrawState* state)
state->info_first_shown_row+i];
if (!curline || !curline->text)
return NULL;
- if (curline->length > state->info_first_shown_column)
+ dlen = display_length(state, curline->text,
+ curline->length);
+ if (dlen > state->info_first_shown_column)
u32_draw_string(startx+1, starty+1+i,
curline->fg == 0 ? fg : curline->fg,
curline->bg == 0 ? bg : curline->bg,
- curline->text +
- state->info_first_shown_column,
+ curline->text,
w-2, w-2, 1, 1, LEFT,
state->info_first_shown_column);
}
@@ -430,8 +504,8 @@ draw_editbox(const struct DrawState* state)
if (state->info_first_shown_row > 0)
u32_draw_string(endx-2, starty+1, fg, bg, (uint32_t*)L"\u2191",
1, 1, 0, 0, LEFT, 0);
- if (state->info_first_shown_row + (h-3)/2 + 2 <= state->info_rows_count)
- u32_draw_string(endx-2, starty+(h-3)/2+1, fg, bg,
+ if (state->info_first_shown_row + (h-3)/2 < state->info_rows_count)
+ u32_draw_string(endx-2, starty+(h-3)/2, fg, bg,
(uint32_t*)L"\u2193", 1, 1, 0, 0, LEFT, 0);
fg = state->edit_info_focused ? EDIT_MSGSTR_FG :
@@ -447,10 +521,11 @@ draw_editbox(const struct DrawState* state)
state->input_first_shown_row+i];
if (!curline || !curline->text)
return NULL;
- if (curline->length > state->input_first_shown_column)
+ dlen = display_length(state, curline->text,
+ curline->length);
+ if (dlen > state->input_first_shown_column)
u32_draw_string(startx+1, endy-h/2-1+i,
- fg, bg, curline->text +
- state->input_first_shown_column,
+ fg, bg, curline->text,
w-2, w-2, 1, 1, LEFT,
state->input_first_shown_column);
}
diff --git a/draw.h b/draw.h
@@ -91,6 +91,10 @@ void init_drawstate(struct DrawState* state, char* error, char* filename,
char* prompt);
void free_drawstate(struct DrawState* state);
struct BufferLine* split(const uint32_t* s);
+size_t display_length(const struct DrawState* state, uint32_t* buffer,
+ const size_t max_index);
+size_t input_length(const struct DrawState* state, uint32_t* buffer,
+ const size_t max_index);
void draw_rect(const int startx, const int endx, const int starty,
const int endy, const uint16_t fg, const uint16_t bg);
void draw_box(const int startx, const int endx, const int starty,
diff --git a/poe.c b/poe.c
@@ -62,10 +62,6 @@ void move_list_page_down(struct DrawState* state);
void next_plural(struct DrawState* state);
void prev_plural(struct DrawState* state);
void toggle_fuzzy(struct DrawState* state);
-size_t display_length(const struct DrawState* state, uint32_t* buffer,
- const size_t max_index);
-size_t input_length(const struct DrawState* state, uint32_t* buffer,
- const size_t max_index);
void insert_char(struct DrawState* state, uint32_t ch);
void erase_backwards(struct DrawState* state);
void erase_forward(struct DrawState* state);
@@ -782,7 +778,7 @@ move_down(struct DrawState* state)
if (state->edit_info_focused)
{
if (state->info_first_shown_row + (EDIT_HEIGHT-3)/2 <
- state->info_rows_count-1)
+ state->info_rows_count)
state->info_first_shown_row++;
return;
}
@@ -893,7 +889,7 @@ move_end(struct DrawState* state)
if (state->edit_info_focused)
{
if (state->info_rows_count > (EDIT_HEIGHT-3)/2)
- state->info_first_shown_row = state->info_rows_count-1 -
+ state->info_first_shown_row = state->info_rows_count -
(EDIT_HEIGHT-3)/2;
state->info_first_shown_column = 0;
return;
@@ -1021,52 +1017,6 @@ toggle_fuzzy(struct DrawState* state)
}
}
-/* Calculates display length of an input buffer line.
- * max_index = the input index up to which the length is calculated
- */
-size_t
-display_length(const struct DrawState* state, uint32_t* buffer,
- const size_t max_index)
-{
- size_t len = 0;
- uint32_t* pbuf = buffer;
-
- for (size_t i = 0; *pbuf && i < max_index; i++)
- {
- pbuf = buffer + i;
- if (*pbuf == '\t')
- len += TAB_SIZE - (len % TAB_SIZE);
- else
- len++;
- }
-
- return len;
-}
-
-/* Calculates input length from a display length.
- * max_index = the display index up to which the length is calculated
- */
-size_t
-input_length(const struct DrawState* state, uint32_t* buffer,
- const size_t max_index)
-{
- size_t len = 0;
- uint32_t* pbuf = buffer;
- size_t i = 0;
-
- for (i = 0; *pbuf && len < max_index; i++)
- {
- pbuf = buffer + i;
- if (*pbuf == '\t')
- len += MIN(TAB_SIZE - (len % TAB_SIZE),
- max_index - len);
- else
- len++;
- }
-
- return i;
-}
-
void
insert_char(struct DrawState* state, uint32_t ch)
{
@@ -1090,13 +1040,18 @@ insert_char(struct DrawState* state, uint32_t ch)
state->input_display_column = display_length(state,
state->input_buffer[state->input_row].text,
state->input_column);
+ if (state->input_display_column - state->input_first_shown_column
+ > EDIT_WIDTH-5)
+ {
+ state->input_first_shown_column = state->input_display_column
+ - EDIT_WIDTH+5;
+ state->input_display_column = display_length(state,
+ state->input_buffer[state->input_row].text,
+ state->input_column);
+ }
if (add_null)
buffer_line->text[state->input_column] = 0;
}
- if (state->input_display_column - state->input_first_shown_column
- > EDIT_WIDTH-5)
- state->input_first_shown_column = state->input_display_column
- - EDIT_WIDTH+5;
state->dirty = 1;
}