чување ca252ffb38a077f73634c261d2982e511fb823ac
родитељ a427e98528b31e834f50a4b48397ac75cc1107b6
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Mon, 14 Jun 2021 14:56:33 +0200
Added the display of flags inside the list and edit box; draw_string fixes; po.c: New file
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 9, додавања: 85(+), брисања: 26(-)
diff --git a/TODO b/TODO
@@ -1,7 +1,18 @@
TODO
====
- [ ] Flags display in list
+ [x] Flags display in list
[ ] Line editing with cursor
+
+ Legend
+ ------
+
+ ? = low priority/maybe
+ [ ] = todo
+ [~] = partially done
+ [x] = fully done
+ [/] = eventually decided not to do
+
+
diff --git a/config.def.h b/config.def.h
@@ -9,6 +9,10 @@
#define ERROR_BG TB_RED
#define FLAG_FG TB_YELLOW | TB_BOLD
#define FLAG_BG TB_DEFAULT
+#define LIST_FLAGS_FG TB_YELLOW | TB_BOLD
+#define LIST_FLAGS_BG TB_DEFAULT
+#define LIST_FLAGS_SEL_FG TB_WHITE
+#define LIST_FLAGS_SEL_BG TB_BLUE
#define LIST_MSGID_FG TB_WHITE
#define LIST_MSGID_BG TB_DEFAULT
#define LIST_MSGID_SEL_FG TB_WHITE
diff --git a/config.h b/config.h
@@ -9,6 +9,10 @@
#define ERROR_BG TB_RED
#define FLAG_FG TB_YELLOW | TB_BOLD
#define FLAG_BG TB_DEFAULT
+#define LIST_FLAGS_FG TB_YELLOW | TB_BOLD
+#define LIST_FLAGS_BG TB_DEFAULT
+#define LIST_FLAGS_SEL_FG TB_WHITE
+#define LIST_FLAGS_SEL_BG TB_BLUE
#define LIST_MSGID_FG TB_WHITE
#define LIST_MSGID_BG TB_DEFAULT
#define LIST_MSGID_SEL_FG TB_WHITE
diff --git a/draw.c b/draw.c
@@ -1,6 +1,7 @@
/* See the file LICENSE for copyright and license details. */
#include <bits/stdint-uintn.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include "termbox.h"
@@ -9,7 +10,7 @@
#include "draw.h"
#include "config.h"
-static const char* help_title = " poe - .po File Editor ";
+static const char* help_title = "poe - .po File Editor";
static const char* help[] = {
"ESC - Close dialog, clear error",
"ENTER - Edit entry",
@@ -20,7 +21,7 @@ static const char* help[] = {
"G or End - Last entry",
"q or C-Q - Quit"
};
-static const char* editbox_title = " Edit translation ";
+static const char* editbox_title = "Edit translation";
const char* errors[] = {
[ERR_UNKNOWN_KEY] = "Unknown key (press H for help)",
[ERR_EXIT_KEY] = "Press q or C-Q to quit",
@@ -74,10 +75,14 @@ draw_box(const int startx, const int endx, const int starty, const int endy,
tb_change_cell(x, endy-1, ' ', SHADOW_FG, SHADOW_BG);
}
+/* max_cols - pack everything into this many columns
+ * fill_cols - this many columns from the left/right/center will be filled
+ * padding_* - this many cells will be skipped from * side */
void
draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const char* s, const int max_cols, const int fill_cols,
- const int padding, const Alignment align)
+ const int padding_left, const int padding_right,
+ const Alignment align)
{
int col;
uint32_t* us = calloc(strlen(s)+1, sizeof(uint32_t));
@@ -94,9 +99,9 @@ draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
}
else if (align == RIGHT)
{
- for (col = 0; col < fill_cols; col++)
+ for (col = max_cols-fill_cols-1; col < max_cols; col++)
tb_change_cell(x+col, y, ' ', fg, bg);
- col = max_cols-us_len-padding-1;
+ col = max_cols-us_len-padding_right-1;
}
else if (align == CENTER)
{
@@ -112,13 +117,13 @@ draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
col = 0;
}
else
- col = (max_cols-us_len)/2-padding;
+ col = (max_cols-us_len)/2-padding_left;
}
}
- while (*pus && col < max_cols-padding)
+ while (*pus && col < max_cols-padding_right)
{
- tb_change_cell(x+col+padding, y, *pus, fg, bg);
+ tb_change_cell(x+col+padding_left, y, *pus, fg, bg);
pus++;
col++;
}
@@ -147,11 +152,11 @@ draw_help(const struct DrawState* state)
draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG);
draw_string(startx+1, starty, TITLE_FG, TITLE_BG,
- help_title, w-2, strlen(help_title)+2, 1, CENTER);
+ help_title, w-2, strlen(help_title)+2, 1, 1, CENTER);
while (row < LEN(help))
{
draw_string(startx+2, starty+3+row, DLG_FG, DLG_BG,
- help[row], w-4, w-4, 2, LEFT);
+ help[row], w-4, w-4, 2, 2, LEFT);
row++;
}
}
@@ -169,13 +174,18 @@ draw_editbox(const struct DrawState* state)
int maxx = state->maxx;
int maxy = state->maxy;
char buf[MSGSTR_MAX];
+ char flags_buf[MAXFLAGSBUF+4];
+ struct PoEntry* entry = &state->entries[state->msgid_number-1];
- if (state->entries[state->msgid_number-1].msgstr)
- strncpy(buf, state->entries[state->msgid_number-1].msgstr,
- MSGSTR_MAX);
+ if (entry->msgstr)
+ strncpy(buf, entry->msgstr, MSGSTR_MAX);
else
buf[0] = 0;
+ strcpy(flags_buf, "[ ");
+ format_flags(flags_buf+2, MAXFLAGSBUF, entry);
+ strcpy(flags_buf+2+MAXFLAGSBUF-1, " ]");
+
startx = w > maxx ? 0 : (maxx - w) / 2;
endx = w > maxx ? maxx-1 : maxx-startx;
@@ -184,19 +194,23 @@ draw_editbox(const struct DrawState* state)
draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG);
draw_string(startx+1, starty, TITLE_FG, TITLE_BG, editbox_title, w-2,
- strlen(editbox_title)+2, 1, CENTER);
+ strlen(editbox_title)+2, 1, 1, CENTER);
draw_rect(startx+2, endx-2, starty+1, endy-11, EDIT_MSGID_FG,
EDIT_MSGID_BG);
draw_string(startx+2, starty+1, EDIT_MSGID_FG, EDIT_MSGID_BG,
state->entries[state->msgid_number-1].msgid, w-2,
strlen(state->entries[state->msgid_number-1].msgid)+2,
- 1, LEFT);
+ 1, 1, LEFT);
draw_rect(startx+2, endx-2, endy-11, endy-2, EDIT_MSGSTR_FG,
EDIT_MSGSTR_BG);
draw_string(startx+2, endy-11, EDIT_MSGSTR_FG, EDIT_MSGSTR_BG,
- buf, w-2, strlen(buf)+2, 1, LEFT);
+ buf, w-2, strlen(buf)+2, 1, 1, LEFT);
+
+ if (entry->flags != FL_NONE)
+ draw_string(endx-MAXFLAGSBUF-4-5, endy-2, DLG_FG, DLG_BG,
+ flags_buf, w-2, strlen(flags_buf), 0, 0, LEFT);
}
void
@@ -212,7 +226,7 @@ draw_status(const struct DrawState* state)
if (state->error && state->error[0])
draw_string(0, state->maxy-1, ERROR_FG, ERROR_BG, state->error,
- state->maxx, state->maxx, 1, CENTER);
+ state->maxx, state->maxx, 1, 1, CENTER);
else
while (pseg < status_segments + LEN(status_segments))
{
@@ -224,7 +238,7 @@ draw_status(const struct DrawState* state)
strncpy(buf, pseg->format, MAXBUF);
buf[MAXBUF-1] = 0;
draw_string(current_start, state->maxy-1, pseg->fg, pseg->bg,
- buf, seg_size, seg_size, 1,
+ buf, seg_size, seg_size, 1, 1,
pseg->alignment);
pseg++;
current_start += seg_size;
@@ -246,23 +260,32 @@ draw_list_visible(const struct PoEntry* start, const struct DrawState* state)
{
draw_string(0, cy, LIST_MSGID_FG, LIST_MSGID_BG,
empty_file_text, state->maxx, state->maxx,
- 2, CENTER);
+ 2, 2, CENTER);
return;
}
while (i <= state->msgid_count && cy < max_screen_entries)
{
int sel = i == state->msgid_number;
- draw_string(0, cy, sel ? LIST_MSGID_SEL_FG : LIST_MSGID_FG,
+ char flags_buf[MAXFLAGSBUF];
+
+ format_flags(flags_buf, MAXFLAGSBUF, current);
+
+ draw_string(0, cy, sel ? LIST_FLAGS_SEL_FG : LIST_FLAGS_FG,
+ sel ? LIST_FLAGS_SEL_BG : LIST_FLAGS_BG,
+ flags_buf, MAXFLAGSBUF-1+2, MAXFLAGSBUF-1+2,
+ 2, 0, LEFT);
+ draw_string(MAXFLAGSBUF-1+2, cy, sel ? LIST_MSGID_SEL_FG :
+ LIST_MSGID_FG,
sel ? LIST_MSGID_SEL_BG : LIST_MSGID_BG,
- current->msgid,
- state->maxx/2, state->maxx/2, 2, LEFT);
+ current->msgid, state->maxx/2-MAXFLAGSBUF+1-2,
+ state->maxx/2-MAXFLAGSBUF+1-2, 2, 0, LEFT);
draw_string(state->maxx/2, cy,
sel ? LIST_MSGSTR_SEL_FG : LIST_MSGSTR_FG,
sel ? LIST_MSGSTR_SEL_BG : LIST_MSGSTR_BG,
current->msgstr
? current->msgstr : "",
- state->maxx/2, state->maxx/2, 2, LEFT);
+ state->maxx/2, state->maxx/2, 2, 2, LEFT);
current++;
i++;
cy++;
diff --git a/draw.h b/draw.h
@@ -42,7 +42,8 @@ void draw_box(const int startx, const int endx, const int starty,
const int endy, const uint16_t fg, const uint16_t bg);
void draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const char* s, const int max_cols, const int fill_cols,
- const int padding, const Alignment align);
+ const int padding_left, const int padding_right,
+ const Alignment align);
void draw_help(const struct DrawState* state);
void draw_editbox(const struct DrawState* state);
void draw_status(const struct DrawState* state);
diff --git a/po.c b/po.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+#include "po.h"
+
+char*
+format_flags(char* buffer, const size_t max, const struct PoEntry* entry)
+{
+ snprintf(buffer, max, "%c%c",
+ entry->flags & FL_C_FORMAT ? 'C' : ' ',
+ entry->flags & FL_FUZZY ? 'F' : ' ');
+ return buffer;
+}
+
diff --git a/po.h b/po.h
@@ -14,3 +14,5 @@ struct PoEntry {
char* msgstr;
};
+char* format_flags(char* buffer, const size_t max, const struct PoEntry* entry);
+
diff --git a/poe.do b/poe.do
@@ -1,5 +1,5 @@
for f in *.c; do
echo $f
done | xargs redo-ifchange
-gcc -g -Wall -std=c99 -o $3 poe.o draw.o termbox.o utf8.o util.o
+gcc -g -Wall -std=c99 -o $3 po.o poe.o draw.o termbox.o utf8.o util.o
diff --git a/util.h b/util.h
@@ -10,6 +10,7 @@ enum {
#define MAXBUF 255
#define MAXPATH 1024
+#define MAXFLAGSBUF 2+1
size_t u8_string_to_unicode(uint32_t* us, const char* s);