tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

listnav.c (1078B)


      1#include "listnav.h"
      2#include "util.h"
      3
      4#include <string.h>
      5
      6static void listnav_update_win(struct listnav *nav);
      7
      8void
      9listnav_update_win(struct listnav *nav)
     10{
     11	nav->wmin = MAX(nav->wmax - nav->wlen, nav->min);
     12	nav->wmax = MIN(nav->wmin + nav->wlen, nav->max);
     13	nav->sel = MAX(MIN(nav->sel, nav->wmax - 1), nav->wmin);
     14}
     15
     16void
     17listnav_init(struct listnav *nav)
     18{
     19	nav->sel = 0;
     20	nav->min = 0;
     21	nav->max = 0;
     22	nav->wlen = 0;
     23	nav->wmin = 0;
     24	nav->wmax = 0;
     25}
     26
     27void
     28listnav_update_bounds(struct listnav *nav, int min, int max)
     29{
     30	ASSERT(max >= min);
     31
     32	nav->min = min;
     33	nav->max = max;
     34
     35	listnav_update_win(nav);
     36}
     37
     38void
     39listnav_update_wlen(struct listnav *nav, int wlen)
     40{
     41	ASSERT(wlen >= 0);
     42
     43	nav->wlen = wlen;
     44	listnav_update_win(nav);
     45}
     46
     47void
     48listnav_update_sel(struct listnav *nav, int sel)
     49{
     50	nav->sel = MAX(MIN(sel, nav->max - 1), nav->min);
     51	if (nav->sel >= nav->wmax) {
     52		nav->wmax = nav->sel + 1;
     53		nav->wmin = MAX(nav->min, nav->wmax - nav->wlen);
     54	} else if (nav->sel < nav->wmin) {
     55		nav->wmin = nav->sel;
     56		nav->wmax = MIN(nav->wmin + nav->wlen, nav->max);
     57	}
     58}
     59