Fix wrong current column position in the status line.
[eco.git] / status.c
blob2fefe315a2d54abe923b443d1579854a2faad16f
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdarg.h>
10 #include "term.h"
11 #include "screen.h"
12 #include "buffer.h"
13 #include "view.h"
14 #include "eco.h"
15 #include "status.h"
18 void e_status_putc(E_Eco *ec, int c)
20 if (ec->view->stcol > ec->view->rcol)
21 return;
23 e_term_move(ec->view->strow, ec->view->stcol);
24 if (c == '*') {
25 e_term_fgcol(ec->tr, E_TR_RED);
26 e_term_bgcol(ec->tr, E_TR_BLACK);
28 else {
29 e_term_fgcol(ec->tr, ec->view->stfg);
30 e_term_bgcol(ec->tr, ec->view->stbg);
33 e_term_putc(c);
34 ec->view->stcol++;
37 void e_status_putc_string(E_Eco *ec, char *s)
39 int i;
41 for (i= 0; i < strlen (s); i++)
42 e_status_putc(ec, s[i]);
45 void e_status_putc_msg(E_Eco *ec)
47 int i;
49 if (!ec->view->stmsg)
50 return;
52 for (i= 0; i < ec->view->stused; i++)
53 e_status_putc(ec, ec->view->stbuf[i]);
56 void e_status_set_msg(E_Eco *ec, char *fmt, ...)
58 va_list ap;
60 va_start(ap, fmt);
61 vsnprintf(ec->view->stbuf, 79, fmt, ap);
62 ec->view->stused= strlen(ec->view->stbuf);
63 va_end(ap);
65 ec->view->stused++;
66 ec->view->stmsg= 1;
67 return;
70 char *e_status_get_msg(E_Eco *ec, char *prompt)
72 int i, c, val;
74 ec->view->stcol= ec->view->col;
75 ec->view->strow= ec->view->rrow;
77 /* need clean the status line. */
78 /* this work in all terms ? */
79 e_term_move(ec->view->strow, ec->view->stcol);
80 e_term_eeol();
81 i= 0;
82 val= 0;
84 if (prompt)
85 e_status_putc_string(ec, prompt);
86 else
87 e_status_putc(ec, ':');
89 for (;;) {
90 c= e_term_getc();
91 if (c == '\r') {
92 ec->view->stbuf[i]= '\0';
93 if (i)
94 val= 1;
95 break;
97 else if (c == '\b' && i) {
98 ec->view->stcol--;
99 e_status_putc(ec, ' ');
100 ec->view->stcol--;
101 e_term_move(ec->view->strow, ec->view->stcol);
102 ec->view->stbuf[i]= '\0';
103 i--;
104 e_term_flush();
106 else if (c > 0x00 && c < 0x20) {
107 c= c + '@';
108 if (c == 'G')
109 return(NULL);
111 else if ((c >= 0x20) && (c < 0x7f)) {
112 ec->view->stbuf[i]= c;
113 i++;
114 e_status_putc(ec, c);
115 e_term_flush();
119 if (val)
120 return(ec->view->stbuf);
121 return(NULL);
124 void e_status_draw(E_Eco *ec)
126 char buf[24];
127 int ocol, orow;
128 int rcol, ccol, i;
131 * The status draw is always call after e_update, so
132 * the physical and virtual cursor are already sync,
133 * we need save the current position to restore
134 * in the end.
136 orow= ec->tr->row;
137 ocol= ec->tr->col;
139 ec->view->stcol= ec->view->col;
140 ec->view->strow= ec->view->rrow;
142 if (ec->view->b->flag & BUFFER_FLUSH)
143 e_status_putc(ec, '*');
145 e_status_putc(ec, '(');
146 e_status_putc_string(ec, ec->view->b->name);
148 for (i= 0, rcol= 0; i < ec->view->b->dot; i++) {
149 if (ec->view->b->line->text[i] == '\t')
150 rcol+= 7;
151 rcol++;
154 for (i= 0, ccol= 0; i < ec->view->b->line->used; i++) {
155 if (ec->view->b->line->text[i] == '\t')
156 ccol+= 7;
157 ccol++;
160 sprintf(buf, ") L(%d/%d) C(%d/%d) ", ec->view->b->nl, ec->view->b->nlines,
161 rcol+1, ccol+1);
162 e_status_putc_string(ec, buf);
164 /* put the percent of the file. */
165 if (ec->view->b->line == ec->view->b->lines)
166 e_status_putc_string(ec, "Top ");
167 else if (!ec->view->b->line->next)
168 e_status_putc_string(ec, "Bottom ");
169 else {
170 int ratio= (100L * ec->view->b->nl)/ec->view->b->nlines;
172 if (ratio > 99)
173 ratio= 99;
175 sprintf(buf, "%2d%% ", ratio);
176 e_status_putc_string(ec, buf);
179 e_status_putc_msg(ec);
181 while (ec->view->stcol < ec->view->rcol)
182 e_status_putc(ec, ' ');
184 e_term_move(orow, ocol);
185 e_term_flush();