Changeset 9468680 in mainline


Ignore:
Timestamp:
2021-10-19T19:48:40Z (2 years ago)
Author:
Jiri Svoboda <jiri@…>
Children:
bdb2a72f
Parents:
516c160
Message:

When moving up a dir, seek to the directory just exited

Location:
uspace/app/nav
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/panel.c

    r516c160 r9468680  
    519519        char *ndir = NULL;
    520520        panel_entry_attr_t attr;
     521        panel_entry_t *next;
     522        panel_entry_t *prev;
     523        char *olddn;
     524        size_t pg_size;
     525        size_t i;
    521526        errno_t rc;
    522527
     
    582587        panel->page = panel_first(panel);
    583588        panel->page_idx = 0;
     589
     590        /* Moving up? */
     591        if (str_cmp(dirname, "..") == 0) {
     592                /* Get the last component of old path */
     593                olddn = str_rchr(panel->dir, '/');
     594                if (olddn != NULL && *olddn != '\0') {
     595                        /* Find corresponding entry */
     596                        ++olddn;
     597                        next = panel_next(panel->cursor);
     598                        while (next != NULL && str_cmp(next->name, olddn) <= 0 &&
     599                            next->isdir) {
     600                                panel->cursor = next;
     601                                ++panel->cursor_idx;
     602                                next = panel_next(panel->cursor);
     603                        }
     604
     605                        /* Move page so that cursor is in the center */
     606                        panel->page = panel->cursor;
     607                        panel->page_idx = panel->cursor_idx;
     608
     609                        pg_size = panel_page_size(panel);
     610
     611                        for (i = 0; i < pg_size / 2; i++) {
     612                                prev = panel_prev(panel->page);
     613                                if (prev == NULL)
     614                                        break;
     615
     616                                panel->page = prev;
     617                                --panel->page_idx;
     618                        }
     619                }
     620        }
     621
    584622        free(panel->dir);
    585623        panel->dir = ndir;
     624
    586625        return EOK;
    587626error:
     
    9741013{
    9751014        gfx_context_t *gc = ui_window_get_gc(panel->window);
     1015        char *dirname;
    9761016        errno_t rc;
    9771017
    9781018        assert(entry->isdir);
    9791019
     1020        /*
     1021         * Need to copy out name before we free the entry below
     1022         * via panel_clear_entries().
     1023         */
     1024        dirname = str_dup(entry->name);
     1025        if (dirname == NULL)
     1026                return ENOMEM;
     1027
    9801028        panel_clear_entries(panel);
    9811029
    982         rc = panel_read_dir(panel, entry->name);
    983         if (rc != EOK)
    984                 return rc;
     1030        rc = panel_read_dir(panel, dirname);
     1031        if (rc != EOK) {
     1032                free(dirname);
     1033                return rc;
     1034        }
     1035
     1036        free(dirname);
    9851037
    9861038        rc = panel_paint(panel);
  • uspace/app/nav/test/panel.c

    r516c160 r9468680  
    471471        rv = remove(p);
    472472        PCUT_ASSERT_INT_EQUALS(0, rv);
     473
    473474        free(fname);
     475}
     476
     477/** When moving to parent directory from a subdir, we seek to the
     478 * coresponding entry
     479 */
     480PCUT_TEST(read_dir_up)
     481{
     482        panel_t *panel;
     483        char buf[L_tmpnam];
     484        char *subdir_a;
     485        char *subdir_b;
     486        char *subdir_c;
     487        char *p;
     488        errno_t rc;
     489        int rv;
     490
     491        /* Create name for temporary directory */
     492        p = tmpnam(buf);
     493        PCUT_ASSERT_NOT_NULL(p);
     494
     495        /* Create temporary directory */
     496        rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
     497        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     498
     499        /* Create some subdirectories */
     500
     501        rv = asprintf(&subdir_a, "%s/%s", p, "a");
     502        PCUT_ASSERT_TRUE(rv >= 0);
     503        rc = vfs_link_path(subdir_a, KIND_DIRECTORY, NULL);
     504        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     505
     506        rv = asprintf(&subdir_b, "%s/%s", p, "b");
     507        PCUT_ASSERT_TRUE(rv >= 0);
     508        rc = vfs_link_path(subdir_b, KIND_DIRECTORY, NULL);
     509        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     510
     511        rv = asprintf(&subdir_c, "%s/%s", p, "c");
     512        PCUT_ASSERT_TRUE(rv >= 0);
     513        rc = vfs_link_path(subdir_c, KIND_DIRECTORY, NULL);
     514        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     515
     516        rc = panel_create(NULL, true, &panel);
     517        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     518
     519        /* Start in subdirectory "b" */
     520        rc = panel_read_dir(panel, subdir_b);
     521        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     522
     523        /* Now go up (into p) */
     524
     525        rc = panel_read_dir(panel, "..");
     526        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     527
     528        PCUT_ASSERT_NOT_NULL(panel->cursor);
     529        PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
     530
     531        panel_destroy(panel);
     532
     533        rv = remove(subdir_a);
     534        PCUT_ASSERT_INT_EQUALS(0, rv);
     535
     536        rv = remove(subdir_b);
     537        PCUT_ASSERT_INT_EQUALS(0, rv);
     538
     539        rv = remove(subdir_c);
     540        PCUT_ASSERT_INT_EQUALS(0, rv);
     541
     542        rv = remove(p);
     543        PCUT_ASSERT_INT_EQUALS(0, rv);
     544
     545        free(subdir_a);
     546        free(subdir_b);
     547        free(subdir_c);
    474548}
    475549
     
    594668        /* Add another entry */
    595669        attr.name = "b";
    596         attr.size= 2;
     670        attr.size = 2;
    597671        rc = panel_entry_append(panel, &attr);
    598672        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
Note: See TracChangeset for help on using the changeset viewer.