l***@mailoo.org
2014-05-02 21:37:57 UTC
Hello everyone,
I started using abook a while ago and I felt, while viewing the index,
that I was loosing of my terminal width. So I made a patch that scale
the size of the fields of the index to fill the whole terminal width,
while keeping their respective size.
The patch should apply on master : if that is not the right branch to
develop on, please tell me.
Once the patch applied, you need to add the option "extend_index" to
true to see its effects.
Here is the patch (you can also find the code at my github [1], in the
percents branch) :
---
diff --git a/abook.c b/abook.c
index 3749561..4f7cf94 100644
--- a/abook.c
+++ b/abook.c
@@ -138,11 +138,11 @@ init_abook()
signal(SIGTERM, quit_abook_sig);
- init_index();
-
if(init_ui())
exit(EXIT_FAILURE);
+ init_index();
+
umask(DEFAULT_UMASK);
if(!datafile_writeable()) {
diff --git a/list.c b/list.c
index 1f0af79..04c1fd2 100644
--- a/list.c
+++ b/list.c
@@ -34,6 +34,32 @@ static WINDOW *list = NULL;
static void
+extend_index()
+{
+ if(!index_elements)
+ return;
+
+ struct index_elem* cur;
+ int total = 0, size = COLS;
+ float fact, nlen;
+
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type == INDEX_TEXT)
+ size -= strlen(cur->d.text);
+ else
+ total += cur->d.field.len;
+ }
+
+ fact = (float)size / (float)total;
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type != INDEX_TEXT) {
+ nlen = (float)cur->d.field.len * fact;
+ cur->d.field.len = (int)nlen;
+ }
+ }
+}
+
+static void
index_elem_add(int type, char *a, char *b)
{
struct index_elem *tmp = NULL, *cur, *cur2;
@@ -82,7 +108,7 @@ index_elem_add(int type, char *a, char *b)
}
static void
-parse_index_format(char *s)
+parse_index_format(char *s, bool extend)
{
char *p, *start, *lstart = NULL;
int in_field = 0, in_alternate = 0, in_length = 0, type;
@@ -117,13 +143,16 @@ parse_index_format(char *s)
}
if(!in_field)
index_elem_add(INDEX_TEXT, start, NULL);
+
+ if(extend == TRUE)
+ extend_index();
}
void
init_index()
{
assert(!index_elements);
- parse_index_format(opt_get_str(STR_INDEX_FORMAT));
+ parse_index_format(opt_get_str(STR_INDEX_FORMAT), opt_get_bool(BOOL_EXTEND_INDEX));
}
void
@@ -132,6 +161,8 @@ init_list()
list = newwin(LIST_LINES, LIST_COLS, LIST_TOP, 0);
scrollok(list, TRUE);
scroll_speed = abs(opt_get_int(INT_SCROLL_SPEED));
+ if(opt_get_bool(BOOL_EXTEND_INDEX))
+ extend_index();
}
void
diff --git a/options.c b/options.c
index 6f75a06..08cab63 100644
--- a/options.c
+++ b/options.c
@@ -51,6 +51,7 @@ static struct option abook_vars[] = {
{ "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE },
{ "index_format", OT_STR, STR_INDEX_FORMAT, UL " {name:22} {email:40} {phone:12|workphone|mobile}" },
+ { "extend_index", OT_BOOL, BOOL_EXTEND_INDEX, FALSE},
{ "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" },
{ "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS,
TRUE },
diff --git a/options.h b/options.h
index 78515a9..6b0dddd 100644
--- a/options.h
+++ b/options.h
@@ -28,6 +28,7 @@ enum bool_opts {
BOOL_SHOW_CURSOR,
BOOL_USE_COLORS,
BOOL_USE_MOUSE,
+ BOOL_EXTEND_INDEX,
BOOL_MAX
};
---
If the patch is too big, or not in the right format, please tell me.
[1] https://github.com/lucas8/abook_extended_index
I started using abook a while ago and I felt, while viewing the index,
that I was loosing of my terminal width. So I made a patch that scale
the size of the fields of the index to fill the whole terminal width,
while keeping their respective size.
The patch should apply on master : if that is not the right branch to
develop on, please tell me.
Once the patch applied, you need to add the option "extend_index" to
true to see its effects.
Here is the patch (you can also find the code at my github [1], in the
percents branch) :
---
diff --git a/abook.c b/abook.c
index 3749561..4f7cf94 100644
--- a/abook.c
+++ b/abook.c
@@ -138,11 +138,11 @@ init_abook()
signal(SIGTERM, quit_abook_sig);
- init_index();
-
if(init_ui())
exit(EXIT_FAILURE);
+ init_index();
+
umask(DEFAULT_UMASK);
if(!datafile_writeable()) {
diff --git a/list.c b/list.c
index 1f0af79..04c1fd2 100644
--- a/list.c
+++ b/list.c
@@ -34,6 +34,32 @@ static WINDOW *list = NULL;
static void
+extend_index()
+{
+ if(!index_elements)
+ return;
+
+ struct index_elem* cur;
+ int total = 0, size = COLS;
+ float fact, nlen;
+
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type == INDEX_TEXT)
+ size -= strlen(cur->d.text);
+ else
+ total += cur->d.field.len;
+ }
+
+ fact = (float)size / (float)total;
+ for(cur = index_elements; cur; cur = cur->next) {
+ if(cur->type != INDEX_TEXT) {
+ nlen = (float)cur->d.field.len * fact;
+ cur->d.field.len = (int)nlen;
+ }
+ }
+}
+
+static void
index_elem_add(int type, char *a, char *b)
{
struct index_elem *tmp = NULL, *cur, *cur2;
@@ -82,7 +108,7 @@ index_elem_add(int type, char *a, char *b)
}
static void
-parse_index_format(char *s)
+parse_index_format(char *s, bool extend)
{
char *p, *start, *lstart = NULL;
int in_field = 0, in_alternate = 0, in_length = 0, type;
@@ -117,13 +143,16 @@ parse_index_format(char *s)
}
if(!in_field)
index_elem_add(INDEX_TEXT, start, NULL);
+
+ if(extend == TRUE)
+ extend_index();
}
void
init_index()
{
assert(!index_elements);
- parse_index_format(opt_get_str(STR_INDEX_FORMAT));
+ parse_index_format(opt_get_str(STR_INDEX_FORMAT), opt_get_bool(BOOL_EXTEND_INDEX));
}
void
@@ -132,6 +161,8 @@ init_list()
list = newwin(LIST_LINES, LIST_COLS, LIST_TOP, 0);
scrollok(list, TRUE);
scroll_speed = abs(opt_get_int(INT_SCROLL_SPEED));
+ if(opt_get_bool(BOOL_EXTEND_INDEX))
+ extend_index();
}
void
diff --git a/options.c b/options.c
index 6f75a06..08cab63 100644
--- a/options.c
+++ b/options.c
@@ -51,6 +51,7 @@ static struct option abook_vars[] = {
{ "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE },
{ "index_format", OT_STR, STR_INDEX_FORMAT, UL " {name:22} {email:40} {phone:12|workphone|mobile}" },
+ { "extend_index", OT_BOOL, BOOL_EXTEND_INDEX, FALSE},
{ "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" },
{ "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS,
TRUE },
diff --git a/options.h b/options.h
index 78515a9..6b0dddd 100644
--- a/options.h
+++ b/options.h
@@ -28,6 +28,7 @@ enum bool_opts {
BOOL_SHOW_CURSOR,
BOOL_USE_COLORS,
BOOL_USE_MOUSE,
+ BOOL_EXTEND_INDEX,
BOOL_MAX
};
---
If the patch is too big, or not in the right format, please tell me.
[1] https://github.com/lucas8/abook_extended_index