1
- #include <ctype.h> // for isalpha
2
- #include <stdbool.h> // for bool, true, false
3
- #include <stdio.h> // for fputs, stdout, printf, putchar, sscanf
4
- #include <string.h> // for memset
5
- #include <sys/ioctl.h> // for winsize
6
- #include "stats.h" // for printStats
1
+ #include "stats.h" // for printStats
2
+ #include <ctype.h> // for isalpha
3
+ #include <stdbool.h> // for bool, true, false
4
+ #include <stdio.h> // for fputs, stdout, printf, putchar, sscanf
5
+ #include <string.h> // for memset
6
+ #include <sys/ioctl.h> // for winsize
7
+
8
+ #include "graphics.h"
9
+ #include "main.h"
7
10
8
- extern unsigned numCharacters ;
9
- extern volatile struct winsize termSize ;
10
- extern bool alternateBuffer ;
11
11
12
12
static char csiCommandBuf [16 ] = {0 };
13
13
static char * pBuf = csiCommandBuf ;
14
- static unsigned char currentTextFormat [8 ] = {0 }; // This should be plenty of simultaneous styles
14
+ static unsigned char currentTextFormat [8 ] = {
15
+ 0 }; // This should be plenty of simultaneous styles
15
16
16
17
17
18
@@ -32,9 +33,10 @@ void unsetTextFormat(void)
32
33
}
33
34
34
35
35
- void returnToStartLine (bool clearText )
36
+ void returnToStartLine (bool clearText , window_t * window )
36
37
{
37
- unsigned numLines = (numCharacters + termSize .ws_col - 1 ) / termSize .ws_col ;
38
+ unsigned numLines = (window -> numCharacters + window -> termSize .ws_col - 1 ) /
39
+ window -> termSize .ws_col ;
38
40
39
41
if (clearText )
40
42
{
@@ -51,36 +53,36 @@ void returnToStartLine(bool clearText)
51
53
}
52
54
53
55
54
- void gotoStatLine (void )
56
+ void gotoStatLine (window_t * window )
55
57
{
56
58
// Clear screen below cursor, move to bottom of screen
57
- printf ("\e[0J\e[%u;1H" , termSize .ws_row + 1U );
59
+ printf ("\e[0J\e[%u;1H" , window -> termSize .ws_row + 1U );
58
60
}
59
61
60
62
61
- void tidyStats (void )
63
+ void tidyStats (window_t * window )
62
64
{
63
65
unsetTextFormat ();
64
66
fputs ("\e[s" , stdout );
65
- gotoStatLine ();
67
+ gotoStatLine (window );
66
68
fputs ("\e[u" , stdout );
67
69
setTextFormat ();
68
70
}
69
71
70
- void clearScreen (void )
72
+ void clearScreen (window_t * window )
71
73
{
72
- if (alternateBuffer )
74
+ if (window -> alternateBuffer )
73
75
{
74
76
// Erase screen + saved lines
75
77
fputs ("\e[2J\e[3J\e[1;1H" , stdout );
76
78
}
77
79
else
78
80
{
79
- returnToStartLine (false);
81
+ returnToStartLine (false, window );
80
82
// Clears screen from cursor to end, switches to Alternate Screen Buffer
81
83
// Erases saved lines, sets cursor to top left
82
84
fputs ("\e[0J\e[?1049h\e[3J\e[1;1H" , stdout );
83
- alternateBuffer = true;
85
+ window -> alternateBuffer = true;
84
86
}
85
87
}
86
88
@@ -93,23 +95,23 @@ static void resetTextFormat(void)
93
95
94
96
static void addTextFormat (char * csi_command )
95
97
{
96
- unsigned int format ;
98
+ unsigned format ;
99
+
100
+ if (sscanf (csi_command , "\e[%u" , & format ) != 1 )
101
+ return ;
97
102
98
- if (sscanf ( csi_command , "\e[%u" , & format ) > 0 )
103
+ if (format == 0 )
99
104
{
100
- if ( format == 0 )
101
- {
102
- resetTextFormat ();
103
- }
104
- else
105
+ resetTextFormat ();
106
+ }
107
+ else
108
+ {
109
+ for ( unsigned i = 0 ; i < sizeof ( currentTextFormat ); i ++ )
105
110
{
106
- for ( unsigned i = 0 ; i < sizeof ( currentTextFormat ); i ++ )
111
+ if ( currentTextFormat [ i ] == 0 )
107
112
{
108
- if (currentTextFormat [i ] == 0 )
109
- {
110
- currentTextFormat [i ] = format ;
111
- break ;
112
- }
113
+ currentTextFormat [i ] = format ;
114
+ break ;
113
115
}
114
116
}
115
117
}
@@ -147,14 +149,14 @@ static bool checkCsiCommand(const unsigned char inputChar, bool* escaped)
147
149
{
148
150
switch (inputChar )
149
151
{
150
- case 'C' : // Cursor forward
151
- case 'D' : // Cursor back
152
- case 'G' : // Cursor horizontal position
153
- case 'K' : // Erase in line
154
- case 'n' : // Device Status Report
152
+ case 'C' : // Cursor forward
153
+ case 'D' : // Cursor back
154
+ case 'G' : // Cursor horizontal position
155
+ case 'K' : // Erase in line
156
+ case 'n' : // Device Status Report
155
157
validCommand = true;
156
158
break ;
157
- case 'm' : // Text formatting
159
+ case 'm' : // Text formatting
158
160
validCommand = true;
159
161
addTextFormat (csiCommandBuf );
160
162
break ;
@@ -170,32 +172,35 @@ static bool checkCsiCommand(const unsigned char inputChar, bool* escaped)
170
172
}
171
173
172
174
173
- static void checkStats (void )
175
+ static void checkStats (window_t * window )
174
176
{
175
- if (numCharacters > termSize .ws_col )
177
+ if (window -> numCharacters > window -> termSize .ws_col )
176
178
{
177
- if ((numCharacters % termSize .ws_col ) == 0 )
178
- printStats (true, true);
179
+ if ((window -> numCharacters % window -> termSize .ws_col ) == 0 )
180
+ printStats (true, true, window );
179
181
}
180
- else if (numCharacters == termSize .ws_col )
181
- printStats (true, true);
182
+ else if (window -> numCharacters == window -> termSize .ws_col )
183
+ printStats (true, true, window );
182
184
}
183
185
184
186
185
- void printChar (unsigned char character , bool verbose , unsigned char * inputBuffer )
187
+ void printChar (unsigned char character , unsigned char * inputBuffer , options_t * options ,
188
+ window_t * window )
186
189
{
187
- if (!verbose )
190
+ if (!options -> verbose )
188
191
{
189
- if ((inputBuffer ) && (numCharacters < 2048 ))
190
- * (inputBuffer + numCharacters ) = character ;
192
+ if ((inputBuffer ) && (window -> numCharacters < 2048 ))
193
+ * (inputBuffer + window -> numCharacters ) = character ;
191
194
}
192
- checkStats ();
195
+ checkStats (window );
193
196
putchar (character );
194
- numCharacters ++ ;
197
+
198
+ window -> numCharacters += 1 ;
195
199
}
196
200
197
201
198
- void processChar (unsigned char character , bool verbose , unsigned char * inputBuffer )
202
+ void processChar (unsigned char character , unsigned char * inputBuffer , options_t * options ,
203
+ window_t * window )
199
204
{
200
205
static bool escaped ;
201
206
if (character == '\e' )
@@ -214,6 +219,24 @@ void processChar(unsigned char character, bool verbose, unsigned char* inputBuff
214
219
}
215
220
else
216
221
{
217
- printChar (character , verbose , inputBuffer );
222
+ printChar (character , inputBuffer , options , window );
223
+ }
224
+ }
225
+
226
+
227
+ void tabToSpaces (unsigned char * inputBuffer , options_t * options , window_t * window )
228
+ {
229
+ printChar (' ' , inputBuffer , options , window );
230
+
231
+ if (window -> numCharacters > window -> termSize .ws_col )
232
+ {
233
+ while ((window -> numCharacters - window -> termSize .ws_col ) % 8 )
234
+ printChar (' ' , inputBuffer , options , window );
235
+ }
236
+ else
237
+ {
238
+ while ((window -> numCharacters % 8 ) &&
239
+ (window -> numCharacters < window -> termSize .ws_col ))
240
+ printChar (' ' , inputBuffer , options , window );
218
241
}
219
242
}
0 commit comments