aboutsummaryrefslogtreecommitdiff
path: root/src/Draw.c
blob: 1132f10e164e915860a623b21cfa9232d25b22e5 (plain)
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
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>

#include "Globals.h"
#include "Terminal.h"
#include "Draw.h"

void draw(void) {
  int rows, cols;
  struct winsize w;
  ioctl(tty_out_fd, TIOCGWINSZ, &w);
  rows = w.ws_row;
  cols = w.ws_col;

  if (rows < 1) rows = 24;
  if (cols < 1) cols = 80;

  int prompt_len = strlen(opts.prompt);
  int input_pos = prompt_len + 2;
  int filter_line = rows;
  int item_end = rows - 1;

  dprintf(tty_out_fd, "\033[H\033[J");

  dprintf(tty_out_fd, "\033[%d;1H", filter_line);
  dprintf(tty_out_fd, "\033[7m");
  dprintf(tty_out_fd, "%-*s", cols, "");
  dprintf(tty_out_fd, "\033[%d;1H", filter_line);
  dprintf(tty_out_fd, "%s", opts.prompt);
  if (input_len > 0) {
    dprintf(tty_out_fd, " %s", input);
  }
  dprintf(tty_out_fd, "\033[0m");

  for (int i = 1; i < item_end; i++) {
    int idx = i - 1 + scroll;
    dprintf(tty_out_fd, "\033[%d;1H", i);
    if (idx < (int)filtered.count) {
      if (idx == cursor) {
        dprintf(tty_out_fd, "\033[7m");
      }
      dprintf(tty_out_fd, "%s\033[0m", filtered.items[idx]);
    }
  }
  dprintf(tty_out_fd, "\033[%d;%dH", filter_line, input_pos);
  fsync(tty_out_fd);
}