File: dirlist.c

package info (click to toggle)
ncdu 1.13-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 660 kB
  • sloc: ansic: 3,544; sh: 1,231; makefile: 41
file content (388 lines) | stat: -rw-r--r-- 9,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* ncdu - NCurses Disk Usage

  Copyright (c) 2007-2018 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "global.h"

#include <string.h>
#include <stdlib.h>


/* public variables */
struct dir *dirlist_parent = NULL,
           *dirlist_par    = NULL;
int64_t dirlist_maxs       = 0,
        dirlist_maxa       = 0;

int    dirlist_sort_desc   = 1,
       dirlist_sort_col    = DL_COL_SIZE,
       dirlist_sort_df     = 0,
       dirlist_hidden      = 0;

/* private state vars */
static struct dir *parent_alloc, *head, *head_real, *selected, *top = NULL;



#define ISHIDDEN(d) (dirlist_hidden && (d) != dirlist_parent && (\
    (d)->flags & FF_EXL || (d)->name[0] == '.' || (d)->name[strlen((d)->name)-1] == '~'\
  ))



static int dirlist_cmp(struct dir *x, struct dir *y) {
  int r;

  /* dirs are always before files when that option is set */
  if(dirlist_sort_df) {
    if(y->flags & FF_DIR && !(x->flags & FF_DIR))
      return 1;
    else if(!(y->flags & FF_DIR) && x->flags & FF_DIR)
      return -1;
  }

  /* sort columns:
   *           1   ->   2   ->   3   ->   4
   *   NAME: name  -> size  -> asize -> items
   *   SIZE: size  -> asize -> name  -> items
   *  ASIZE: asize -> size  -> name  -> items
   *  ITEMS: items -> size  -> asize -> name
   *
   * Note that the method used below is supposed to be fast, not readable :-)
   */
#define CMP_NAME  strcmp(x->name, y->name)
#define CMP_SIZE  (x->size  > y->size  ? 1 : (x->size  == y->size  ? 0 : -1))
#define CMP_ASIZE (x->asize > y->asize ? 1 : (x->asize == y->asize ? 0 : -1))
#define CMP_ITEMS (x->items > y->items ? 1 : (x->items == y->items ? 0 : -1))

  /* try 1 */
  r = dirlist_sort_col == DL_COL_NAME ? CMP_NAME :
      dirlist_sort_col == DL_COL_SIZE ? CMP_SIZE :
      dirlist_sort_col == DL_COL_ASIZE ? CMP_ASIZE :
      CMP_ITEMS;
  /* try 2 */
  if(!r)
    r = dirlist_sort_col == DL_COL_SIZE ? CMP_ASIZE : CMP_SIZE;
  /* try 3 */
  if(!r)
    r = (dirlist_sort_col == DL_COL_NAME || dirlist_sort_col == DL_COL_ITEMS) ?
         CMP_ASIZE : CMP_NAME;
  /* try 4 */
  if(!r)
    r = dirlist_sort_col == DL_COL_ITEMS ? CMP_NAME : CMP_ITEMS;

  /* reverse when sorting in descending order */
  if(dirlist_sort_desc && r != 0)
    r = r < 0 ? 1 : -1;

  return r;
}


static struct dir *dirlist_sort(struct dir *list) {
  struct dir *p, *q, *e, *tail;
  int insize, nmerges, psize, qsize, i;

  insize = 1;
  while(1) {
    p = list;
    list = NULL;
    tail = NULL;
    nmerges = 0;
    while(p) {
      nmerges++;
      q = p;
      psize = 0;
      for(i=0; i<insize; i++) {
        psize++;
        q = q->next;
        if(!q) break;
      }
      qsize = insize;
      while(psize > 0 || (qsize > 0 && q)) {
        if(psize == 0) {
          e = q; q = q->next; qsize--;
        } else if(qsize == 0 || !q) {
          e = p; p = p->next; psize--;
        } else if(dirlist_cmp(p,q) <= 0) {
          e = p; p = p->next; psize--;
        } else {
          e = q; q = q->next; qsize--;
        }
        if(tail) tail->next = e;
        else     list = e;
        e->prev = tail;
        tail = e;
      }
      p = q;
    }
    tail->next = NULL;
    if(nmerges <= 1) {
      if(list->parent)
        list->parent->sub = list;
      return list;
    }
    insize *= 2;
  }
}


/* passes through the dir listing once and:
 * - makes sure one, and only one, visible item is selected
 * - updates the dirlist_(maxs|maxa) values
 * - makes sure that the FF_BSEL bits are correct */
static void dirlist_fixup() {
  struct dir *t;

  /* we're going to determine the selected items from the list itself, so reset this one */
  selected = NULL;

  for(t=head; t; t=t->next) {
    /* not visible? not selected! */
    if(ISHIDDEN(t))
      t->flags &= ~FF_BSEL;
    else {
      /* visible and selected? make sure only one item is selected */
      if(t->flags & FF_BSEL) {
        if(!selected)
          selected = t;
        else
          t->flags &= ~FF_BSEL;
      }
    }

    /* update dirlist_(maxs|maxa) */
    if(t->size > dirlist_maxs)
      dirlist_maxs = t->size;
    if(t->asize > dirlist_maxa)
      dirlist_maxa = t->asize;
  }

  /* no selected items found after one pass? select the first visible item */
  if(!selected)
    if((selected = dirlist_next(NULL)))
      selected->flags |= FF_BSEL;
}


void dirlist_open(struct dir *d) {
  dirlist_par = d;

  /* set the head of the list */
  head_real = head = d == NULL ? NULL : d->sub;

  /* reset internal status */
  dirlist_maxs = dirlist_maxa = 0;

  /* stop if this is not a directory list we can work with */
  if(d == NULL) {
    dirlist_parent = NULL;
    return;
  }

  /* sort the dir listing */
  if(head)
    head_real = head = dirlist_sort(head);

  /* set the reference to the parent dir */
  if(d->parent) {
    if(!parent_alloc)
      parent_alloc = calloc(1, dir_memsize(".."));
    dirlist_parent = parent_alloc;
    strcpy(dirlist_parent->name, "..");
    dirlist_parent->next = head;
    dirlist_parent->parent = d;
    dirlist_parent->sub = d;
    dirlist_parent->flags = FF_DIR;
    head = dirlist_parent;
  } else
    dirlist_parent = NULL;

  dirlist_fixup();
}


struct dir *dirlist_next(struct dir *d) {
  if(!head)
    return NULL;
  if(!d) {
    if(!ISHIDDEN(head))
      return head;
    else
      d = head;
  }
  while((d = d->next)) {
    if(!ISHIDDEN(d))
      return d;
  }
  return NULL;
}


static struct dir *dirlist_prev(struct dir *d) {
  if(!head || !d)
    return NULL;
  while((d = d->prev)) {
    if(!ISHIDDEN(d))
      return d;
  }
  if(dirlist_parent)
    return dirlist_parent;
  return NULL;
}


struct dir *dirlist_get(int i) {
  struct dir *t = selected, *d;

  if(!head)
    return NULL;

  if(ISHIDDEN(selected)) {
    selected = dirlist_next(NULL);
    return selected;
  }

  /* i == 0? return the selected item */
  if(!i)
    return selected;

  /* positive number? simply move forward */
  while(i > 0) {
    d = dirlist_next(t);
    if(!d)
      return t;
    t = d;
    if(!--i)
      return t;
  }

  /* otherwise, backward */
  while(1) {
    d = dirlist_prev(t);
    if(!d)
      return t;
    t = d;
    if(!++i)
      return t;
  }
}


void dirlist_select(struct dir *d) {
  if(!d || !head || ISHIDDEN(d) || d->parent != head->parent)
    return;

  selected->flags &= ~FF_BSEL;
  selected = d;
  selected->flags |= FF_BSEL;
}



/* We need a hint in order to figure out which item should be on top:
 *  0 = only get the current top, don't set anything
 *  1 = selected has moved down
 * -1 = selected has moved up
 * -2 = selected = first item in the list (faster version of '1')
 * -3 = top should be considered as invalid (after sorting or opening an other dir)
 * -4 = an item has been deleted
 * -5 = hidden flag has been changed
 *
 * Actions:
 *  hint = -1 or -4 -> top = selected_is_visible ? top : selected
 *  hint = -2 or -3 -> top = selected-(winrows-3)/2
 *  hint =  1       -> top = selected_is_visible ? top : selected-(winrows-4)
 *  hint =  0 or -5 -> top = selected_is_visible ? top : selected-(winrows-3)/2
 *
 * Regardless of the hint, the returned top will always be chosen such that the
 * selected item is visible.
 */
struct dir *dirlist_top(int hint) {
  struct dir *t;
  int i = winrows-3, visible = 0;

  if(hint == -2 || hint == -3)
    top = NULL;

  /* check whether the current selected item is within the visible window */
  if(top) {
    i = winrows-3;
    t = dirlist_get(0);
    while(t && i--) {
      if(t == top) {
        visible++;
        break;
      }
      t = dirlist_prev(t);
    }
  }

  /* otherwise, get a new top */
  if(!visible)
    top = hint == -1 || hint == -4 ? dirlist_get(0) :
          hint ==  1               ? dirlist_get(-1*(winrows-4)) :
                                     dirlist_get(-1*(winrows-3)/2);

  /* also make sure that if the list is longer than the window and the last
   * item is visible, that this last item is also the last on the window */
  t = top;
  i = winrows-3;
  while(t && i--)
    t = dirlist_next(t);
  t = top;
  do {
    top = t;
    t = dirlist_prev(t);
  } while(t && i-- > 0);

  return top;
}


void dirlist_set_sort(int col, int desc, int df) {
  /* update config */
  if(col != DL_NOCHANGE)
    dirlist_sort_col = col;
  if(desc != DL_NOCHANGE)
    dirlist_sort_desc = desc;
  if(df != DL_NOCHANGE)
    dirlist_sort_df = df;

  /* sort the list (excluding the parent, which is always on top) */
  head_real = dirlist_sort(head_real);
  if(dirlist_parent)
    dirlist_parent->next = head_real;
  else
    head = head_real;
  dirlist_top(-3);
}


void dirlist_set_hidden(int hidden) {
  dirlist_hidden = hidden;
  dirlist_fixup();
  dirlist_top(-5);
}