11 static struct sbuf
*term_sbuf
;
12 static int rows
, cols
;
13 static struct termios termios
;
18 struct termios newtermios
;
19 tcgetattr(0, &termios
);
21 newtermios
.c_lflag
&= ~(ICANON
| ISIG
);
22 newtermios
.c_lflag
&= ~ECHO
;
23 tcsetattr(0, TCSAFLUSH
, &newtermios
);
25 rows
= atoi(getenv("LINES"));
26 if (getenv("COLUMNS"))
27 cols
= atoi(getenv("COLUMNS"));
28 if (!ioctl(0, TIOCGWINSZ
, &win
)) {
29 cols
= cols
? cols
: win
.ws_col
;
30 rows
= rows
? rows
: win
.ws_row
;
32 cols
= cols
? cols
: 80;
33 rows
= rows
? rows
: 25;
39 tcsetattr(0, 0, &termios
);
42 void term_suspend(void)
45 kill(getpid(), SIGSTOP
);
49 void term_record(void)
52 term_sbuf
= sbuf_make();
55 void term_commit(void)
58 write(1, sbuf_buf(term_sbuf
), sbuf_len(term_sbuf
));
64 static void term_out(char *s
)
67 sbuf_str(term_sbuf
, s
);
69 write(1, s
, strlen(s
));
72 void term_str(char *s
)
92 sprintf(cmd
, "\33[%dM", -n
);
94 sprintf(cmd
, "\33[%dL", n
);
99 void term_pos(int r
, int c
)
107 sprintf(buf
, "\r\33[%d%c", abs(c
), c
> 0 ? 'C' : 'D');
109 sprintf(buf
, "\33[%d;%dH", r
+ 1, c
+ 1);
123 static char ibuf
[4096]; /* input character buffer */
124 static char icmd
[4096]; /* read after the last term_cmd() */
125 static int ibuf_pos
, ibuf_cnt
; /* ibuf[] position and length */
126 static int icmd_pos
; /* icmd[] position */
128 /* read s before reading from the terminal */
129 void term_push(char *s
, int n
)
131 n
= MIN(n
, sizeof(ibuf
) - ibuf_cnt
);
132 memcpy(ibuf
+ ibuf_cnt
, s
, n
);
136 /* return a static buffer containing inputs read since the last term_cmd() */
137 char *term_cmd(int *n
)
144 int term_read(int ms
)
146 struct pollfd ufds
[1];
148 if (ibuf_pos
>= ibuf_cnt
) {
150 ufds
[0].events
= POLLIN
;
151 if (poll(ufds
, 1, ms
* 1000) <= 0)
153 /* read a single input character */
154 if ((n
= read(0, ibuf
, 1)) <= 0)
159 c
= ibuf_pos
< ibuf_cnt
? (unsigned char) ibuf
[ibuf_pos
++] : -1;
160 if (icmd_pos
< sizeof(icmd
))
161 icmd
[icmd_pos
++] = c
;
165 /* return a static string that changes text attributes from old to att */
166 char *term_att(int att
, int old
)
168 static char buf
[128];
170 int fg
= SYN_FG(att
);
171 int bg
= SYN_BG(att
);
174 s
+= sprintf(s
, "\33[m\33[");
176 s
+= sprintf(s
, "1;");
178 s
+= sprintf(s
, "3;");
179 else if (fg
& SYN_RV
)
180 s
+= sprintf(s
, "7;");
182 s
+= sprintf(s
, "%d;", 30 + (fg
& 0xff));
184 s
+= sprintf(s
, "38;5;%d;", (fg
& 0xff));
187 s
+= sprintf(s
, "%d;", 40 + (bg
& 0xff));
189 s
+= sprintf(s
, "48;5;%d;", (bg
& 0xff));
191 s
+= sprintf(s
, "m");