File: view.c

package info (click to toggle)
tina 0.1.14-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: ansic: 1,653; sh: 1,415; python: 1,196; makefile: 42
file content (127 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (2)
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
/*
 * tina - a personal information manager
 * SPDX-FileCopyrightText: 2001  Matt Kraai
 * SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

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

#include "curslib.h"
#include "memory.h"
#include "selection.h"
#include "view.h"

struct view *
view_new_with_selection (struct selection *s)
{
  struct view *v;

  v = xcalloc (1, sizeof (struct view));
  v->s = s;

  return v;
}

void
view_delete (struct view *v)
{
  free (v->search_pattern);
  free (v);
}

/* Display the help line.  */
static void
show_help_line (void)
{
  highlight ();
  mvaddstr (0, 0, "q:Quit  O:New  d:Delete  y:Yank  P:Paste  ?:Help");
  pad_to_eol ();
  lowlight ();
}

/* Display ITEM from V at LINE.  */
static void
show_item (struct view *v, int line, int item)
{
  if (item == v->selected)
    {
      if (has_colors ())
	color_set (color_selected, NULL);
      else
	standout ();
    }

  mvaddnstr (line, 0, v->s->items[item]->description, COLS);

  if (item == v->selected)
    {
      pad_to_eol ();

      if (has_colors ())
	color_set (color_default, NULL);
      else
	standend ();
    }
  else
    clrtoeol ();
}

/* Display the mode line for V.  */
static void
show_mode_line (struct view *v)
{
  int x, y;

  highlight ();
  mvaddch (LINES - 2, 0, '-');
  addch (v->s->db->readonly ? '%' : '-');
  printw ("-Tina: %s [", v->s->db->path);
  if (v->s->category != NULL)
    {
      size_t i;

      for (i = 0; i < v->s->db->nitems; i++)
	if (strcmp (v->s->category, v->s->db->items[i]->identifier) == 0)
	  {
	    addstr ("Category:");
	    getyx (stdscr, y, x);
	    if (y != -1) {
		    addnstr (v->s->db->items[i]->description, COLS - x - 20);
		    addch (' ');
	    }
	    break;
	  }
    }
  printw ("Items:%zu]", v->s->nitems);
  for (getyx (stdscr, y, x); y != -1 && x < COLS - 8; x++)
    addch ('-');
  addch('(');
  if ((int)v->s->nitems <= LINES - 3)
    addstr ("all");
  else if (v->selected / (LINES - 3) == ((int)v->s->nitems - 1) / (LINES - 3))
    addstr ("end");
  else
    printw ("%zu%%",
	    (v->selected / (LINES - 3) + 1) * (LINES - 3) * 100 / v->s->nitems);
  addstr (")---");
  lowlight ();
}

void
view_show (struct view *v)
{
  size_t i, first;

  show_help_line ();

  first = v->selected - v->selected % (LINES - 3);
  for (i = 0; (int)i < LINES - 3 && first + i < v->s->nitems; i++)
    show_item (v, i + 1, first + i);
  for (; (int)i < LINES - 3; i++)
    CLEARLINE (i + 1);

  show_mode_line (v);
}