vip

VI Plus
git clone https://codeberg.org/night0721/vip
Log | Files | Refs | README | LICENSE

bar.c (3387B)


      1 #include <stdio.h>
      2 #include <stdarg.h>
      3 #include <string.h>
      4 #include <time.h>
      5 
      6 #include "vip.h"
      7 
      8 extern editor vip;
      9 
     10 void draw_status_bar(struct abuf *ab)
     11 {
     12 	abAppend(ab, "\x1b[7m", 4);
     13 	abAppend(ab, "\x1b[1m", 4);
     14 
     15 	int mode_len;
     16 	if (vip.mode == NORMAL) {
     17 		abAppend(ab, BLUE_BG, COLOR_LEN);
     18 		char mode[9] = " NORMAL ";
     19 		abAppend(ab, mode, 8);
     20 		mode_len = 8;
     21 	} else if (vip.mode == INSERT) {
     22 		abAppend(ab, GREEN_BG, COLOR_LEN);
     23 		char mode[9] = " INSERT ";
     24 		abAppend(ab, mode, 8);
     25 		mode_len = 8;
     26 	} else if (vip.mode == COMMAND) {
     27 		abAppend(ab, PEACH_BG, COLOR_LEN);
     28 		char mode[10] = " COMMAND ";
     29 		abAppend(ab, mode, 9);
     30 		mode_len = 9;
     31 	}
     32 
     33 	abAppend(ab, "\x1b[22m", 5);
     34 
     35 	char git_branch[80], git_diff[80], file[80], info[80], lines[80], coord[80];
     36 	int gitb_len = snprintf(git_branch, sizeof(git_branch), " %s ", "master");
     37 	int gitd_len = snprintf(git_diff, sizeof(git_diff), " %s ", "+1");
     38 	int file_len = snprintf(file, sizeof(file), " %.20s %s",
     39 			vip.filename ? vip.filename : "[No Name]", vip.dirty ? "[+]" : "");
     40 	int info_len = snprintf(info, sizeof(info), " %s ", vip.syntax ? vip.syntax->filetype : "");
     41 	int lines_len;
     42 	if (vip.rows == 0 || vip.rows == vip.cy + 1) {
     43 		lines_len = snprintf(lines, sizeof(lines), " %s ", vip.rows == 0 ? "Top" : "Bot");
     44 	} else {
     45 		lines_len = snprintf(lines, sizeof(lines), " %d%% ", ((vip.cy + 1) * 100 / vip.rows));
     46 	}
     47 	int coord_len = snprintf(coord, sizeof(coord), " %d:%d ", vip.cy + 1, vip.rx + 1);
     48 
     49 	abAppend(ab, SURFACE_1_BG, COLOR_LEN); /* background */
     50 	if (vip.mode == NORMAL) {
     51 		abAppend(ab, BLUE_FG, COLOR_LEN); /* text */
     52 	} else if (vip.mode == INSERT) {
     53 		abAppend(ab, GREEN_FG, COLOR_LEN);
     54 	} else if (vip.mode == COMMAND) {
     55 		abAppend(ab, PEACH_FG, COLOR_LEN);
     56 	}
     57 	abAppend(ab, git_branch, gitb_len);
     58 	abAppend(ab, "|", 1);
     59 	abAppend(ab, GREEN_FG, COLOR_LEN);
     60 	abAppend(ab, git_diff, gitd_len);
     61 	abAppend(ab, BLACK_BG, COLOR_LEN);
     62 	abAppend(ab, WHITE_FG, COLOR_LEN);
     63 	abAppend(ab, file, file_len);
     64 
     65 
     66 	while (file_len < vip.screencols) {
     67 		if (vip.screencols - mode_len - file_len - gitb_len - gitd_len - 1 == info_len + lines_len + coord_len) {
     68 			abAppend(ab, info, info_len);
     69 			abAppend(ab, SURFACE_1_BG, COLOR_LEN);
     70 			if (vip.mode == NORMAL) {
     71 				abAppend(ab, BLUE_FG, COLOR_LEN);
     72 			} else if (vip.mode == INSERT) {
     73 				abAppend(ab, GREEN_FG, COLOR_LEN);
     74 			} else if (vip.mode == COMMAND) {
     75 				abAppend(ab, PEACH_FG, COLOR_LEN);
     76 			}
     77 			abAppend(ab, lines, lines_len);
     78 			if (vip.mode == NORMAL) {
     79 				abAppend(ab, BLUE_BG, COLOR_LEN);
     80 			} else if (vip.mode == INSERT) {
     81 				abAppend(ab, GREEN_BG, COLOR_LEN);
     82 			} else if (vip.mode == COMMAND) {
     83 				abAppend(ab, PEACH_BG, COLOR_LEN);
     84 			}
     85 			abAppend(ab, BLACK_FG, COLOR_LEN);
     86 			abAppend(ab, "\x1b[1m", 4);
     87 			abAppend(ab, coord, coord_len);
     88 			abAppend(ab, "\x1b[22m", 5);
     89 			break;
     90 		} else {
     91 			abAppend(ab, " ", 1);
     92 			file_len++;
     93 		}
     94 	}
     95 	abAppend(ab, "\x1b[m", 3);
     96 	abAppend(ab, "\r\n", 2);
     97 }
     98 
     99 void draw_message_bar(struct abuf *ab)
    100 {
    101 	abAppend(ab, "\x1b[K", 3);
    102 	int msglen = strlen(vip.statusmsg);
    103 	if (msglen > vip.screencols) msglen = vip.screencols;
    104 	if (msglen && time(NULL) - vip.statusmsg_time < 5)
    105 		abAppend(ab, vip.statusmsg, msglen);
    106 }
    107 
    108 void set_status_bar_message(const char *fmt, ...)
    109 {
    110 	va_list ap;
    111 	va_start(ap, fmt);
    112 	vsnprintf(vip.statusmsg, sizeof(vip.statusmsg), fmt, ap);
    113 	va_end(ap);
    114 	vip.statusmsg_time = time(NULL);
    115 }