Changeset 9fb09da in mainline for uspace/app/edit/edit.c


Ignore:
Timestamp:
2017-11-29T16:57:59Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
48b77ed
Parents:
cc40af4
Message:

Fix text editor status overflowing due to long file names or paths.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/edit/edit.c

    rcc40af4 r9fb09da  
    955955        coord_t coord;
    956956        int last_row;
     957        char *fname;
     958        char *p;
     959        char *text;
     960        size_t n;
     961        int pos;
     962        size_t nextra;
     963        size_t fnw;
    957964
    958965        tag_get_pt(&pane.caret_pos, &caret_pt);
     
    961968        sheet_get_num_rows(doc.sh, &last_row);
    962969
    963         const char *fname = (doc.file_name != NULL) ? doc.file_name : "<unnamed>";
     970        if (doc.file_name != NULL) {
     971                /* Remove directory component */
     972                p = str_rchr(doc.file_name, '/');
     973                if (p != NULL)
     974                        fname = str_dup(p + 1);
     975                else
     976                        fname = str_dup(doc.file_name);
     977        } else {
     978                fname = str_dup("<unnamed>");
     979        }
     980
     981        if (fname == NULL)
     982                return;
    964983
    965984        console_set_pos(con, 0, scr_rows - 1);
    966985        console_set_style(con, STYLE_INVERTED);
    967         int n = printf(" %d, %d (%d): File '%s'. Ctrl-Q Quit  Ctrl-S Save  "
    968             "Ctrl-E Save As", coord.row, coord.column, last_row, fname);
    969        
    970         int pos = scr_columns - 1 - n;
     986
     987        /*
     988         * Make sure the status fits on the screen. This loop should
     989         * be executed at most twice.
     990         */
     991        while (true) {
     992                int rc = asprintf(&text, " %d, %d (%d): File '%s'. Ctrl-Q Quit  Ctrl-S Save  "
     993                    "Ctrl-E Save As", coord.row, coord.column, last_row, fname);
     994                if (rc < 0) {
     995                        n = 0;
     996                        goto finish;
     997                }
     998
     999                /* If it already fits, we're done */
     1000                n = str_width(text);
     1001                if (n <= scr_columns - 2)
     1002                        break;
     1003
     1004                /* Compute number of excess characters */
     1005                nextra = n - (scr_columns - 2);
     1006                /** With of the file name part */
     1007                fnw = str_width(fname);
     1008
     1009                /*
     1010                 * If reducing file name to two characters '..' won't help,
     1011                 * just give up and print a blank status.
     1012                 */
     1013                if (nextra > fnw - 2)
     1014                        goto finish;
     1015
     1016                /* Compute position where we overwrite with '..\0' */
     1017                if (fnw >= nextra + 2) {
     1018                        p = fname + str_lsize(fname, fnw - nextra - 2);
     1019                } else {
     1020                        p = fname;
     1021                }
     1022
     1023                /* Shorten the string */
     1024                p[0] = p[1] = '.';
     1025                p[2] = '\0';
     1026
     1027                /* Need to format the string once more. */
     1028                free(text);
     1029        }
     1030
     1031        printf("%s", text);
     1032        free(text);
     1033finish:
     1034        /* Fill the rest of the line */
     1035        pos = scr_columns - 1 - n;
    9711036        printf("%*s", pos, "");
    9721037        console_flush(con);
Note: See TracChangeset for help on using the changeset viewer.