Make UTF-8 handling optional but still default
[tig] / tig.c
diff --git a/tig.c b/tig.c
index cfdf026..49c2492 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -236,6 +236,8 @@ static int opt_num_interval = NUMBER_INTERVAL;
 static int opt_tab_size                = TABSIZE;
 static enum request opt_request = REQ_VIEW_MAIN;
 static char opt_cmd[SIZEOF_CMD]        = "";
+static char opt_encoding[20]   = "";
+static bool opt_utf8           = TRUE;
 static FILE *opt_pipe          = NULL;
 
 /* Returns the index of log or diff command or -1 to exit. */
@@ -1652,7 +1654,7 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
        int col = 0;
        size_t timelen;
        size_t authorlen;
-       int trimmed;
+       int trimmed = 1;
 
        if (!*commit->author)
                return FALSE;
@@ -1680,8 +1682,15 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
        if (type != LINE_CURSOR)
                wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
 
-       /* FIXME: Make this optional, and add i18n.commitEncoding support. */
-       authorlen = utf8_length(commit->author, AUTHOR_COLS - 2, &col, &trimmed);
+       if (opt_utf8) {
+               authorlen = utf8_length(commit->author, AUTHOR_COLS - 2, &col, &trimmed);
+       } else {
+               authorlen = strlen(commit->author);
+               if (authorlen > AUTHOR_COLS - 2) {
+                       authorlen = AUTHOR_COLS - 2;
+                       trimmed = 1;
+               }
+       }
 
        if (trimmed) {
                waddnstr(view->win, commit->author, authorlen);
@@ -2381,6 +2390,44 @@ load_refs(void)
        return OK;
 }
 
+static int
+load_config(void)
+{
+       FILE *pipe = popen("git repo-config --list", "r");
+       char buffer[BUFSIZ];
+       char *name;
+
+       if (!pipe)
+               return ERR;
+
+       while ((name = fgets(buffer, sizeof(buffer), pipe))) {
+               char *value = strchr(name, '=');
+               int valuelen, namelen;
+
+               /* No boolean options, yet */
+               if (!value)
+                       continue;
+
+               namelen  = value - name;
+
+               *value++ = 0;
+               valuelen = strlen(value);
+               if (valuelen > 0)
+                       value[valuelen - 1] = 0;
+
+               if (!strcmp(name, "i18n.commitencoding")) {
+                       string_copy(opt_encoding, value);
+               }
+       }
+
+       if (ferror(pipe))
+               return ERR;
+
+       pclose(pipe);
+
+       return OK;
+}
+
 /*
  * Main
  */
@@ -2435,9 +2482,15 @@ main(int argc, char *argv[])
        if (refs_size == 0 && opt_request != REQ_VIEW_PAGER)
                die("Not a git repository");
 
+       if (load_config() == ERR)
+               die("Failed to load repo config.");
+
        for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
                view->cmd_env = getenv(view->cmd_env);
 
+       if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
+               opt_utf8 = FALSE;
+
        request = opt_request;
 
        init_display();
@@ -2624,10 +2677,12 @@ main(int argc, char *argv[])
  *
  * SEE ALSO
  * --------
- * [verse]
- * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
- * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
- * gitk(1): git repository browser written using tcl/tk,
- * qgit(1): git repository browser written using c++/Qt,
- * gitview(1): git repository browser written using python/gtk.
+ * - link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
+ * - link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
+ *
+ * Other git repository browsers:
+*
+ *  - gitk(1)
+ *  - qgit(1)
+ *  - gitview(1)
  **/