aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--default.bld5
-rw-r--r--src/csvreader.cpp2
-rw-r--r--src/tools/viewcsv.cpp189
3 files changed, 193 insertions, 3 deletions
diff --git a/default.bld b/default.bld
index 15ed6fc..cb5a4d9 100644
--- a/default.bld
+++ b/default.bld
@@ -76,6 +76,11 @@ target files("src/tools/*.cpp").replace("src/tools/","").replace(".cpp","")
76 LDFLAGS += "-L. -lbu++"; 76 LDFLAGS += "-L. -lbu++";
77} 77}
78 78
79target "viewcsv"
80{
81 LDFLAGS += "-lncurses";
82}
83
79// 84//
80// General Tests 85// General Tests
81// 86//
diff --git a/src/csvreader.cpp b/src/csvreader.cpp
index edbb7f8..08803e7 100644
--- a/src/csvreader.cpp
+++ b/src/csvreader.cpp
@@ -21,7 +21,7 @@ Bu::CsvReader::CsvReader( Bu::Stream &sIn, Bu::CsvReader::Style eStyle ) :
21 break; 21 break;
22 22
23 case styleC: 23 case styleC:
24 sDecode = Bu::slot( &decodeExcel ); 24 sDecode = Bu::slot( &decodeC );
25 break; 25 break;
26 } 26 }
27} 27}
diff --git a/src/tools/viewcsv.cpp b/src/tools/viewcsv.cpp
index 69bc6a1..429c099 100644
--- a/src/tools/viewcsv.cpp
+++ b/src/tools/viewcsv.cpp
@@ -2,6 +2,8 @@
2#include <bu/optparser.h> 2#include <bu/optparser.h>
3#include <bu/csvreader.h> 3#include <bu/csvreader.h>
4#include <bu/file.h> 4#include <bu/file.h>
5#include <bu/util.h>
6#include <ncurses.h>
5 7
6using namespace Bu; 8using namespace Bu;
7 9
@@ -65,6 +67,137 @@ public:
65 IntArray aWidths; 67 IntArray aWidths;
66}; 68};
67 69
70class CsvView
71{
72public:
73 CsvView( CsvDoc &doc ) :
74 doc( doc ),
75 iXOff( 0 ),
76 iYOff( 0 ),
77 bHeaderRow( false )
78 {
79 }
80
81 virtual ~CsvView()
82 {
83 }
84
85 void render()
86 {
87 erase();
88 int maxx, maxy;
89 getmaxyx( stdscr, maxy, maxx );
90
91 int iRows = min( (int)doc.sgData.getSize(), maxy-1 );
92 int iCols = min( doc.iMaxCols, (int)maxx-1 );
93
94 int iHdrHeight = 1;
95 if( bHeaderRow )
96 iHdrHeight++;
97
98 // Draw the headers
99 for( int iRow = 0; iRow < iRows; iRow++ )
100 {
101 char buf[6];
102 snprintf( buf, 6, "%5d", iRow+iYOff );
103 mvaddnstr( iRow+iHdrHeight+1, 0, buf, 5 );
104 mvaddch( iRow+iHdrHeight+1, 6, ACS_VLINE );
105 }
106 int iXPos = 6;
107 try
108 {
109 for( int iCol = 0; iCol < iCols; iCol++ )
110 {
111 if( iXPos + doc.aWidths[iCol+iXOff]+2 >= maxx )
112 break;
113 char buf[6];
114 snprintf( buf, 6, "%d", iCol+iXOff );
115 mvaddch( 0, iXPos, ACS_VLINE );
116 mvaddch( iHdrHeight, iXPos, ACS_PLUS );
117 mvaddnstr( 0, iXPos+1, buf, 5 );
118 if( bHeaderRow )
119 {
120 mvaddstr( 1, iXPos+1, doc.sgData[0][iCol+iXOff].getStr() );
121 mvaddch( 1, iXPos, ACS_VLINE );
122 }
123 for( int j = 0; j <= doc.aWidths[iCol+iXOff]; j++ )
124 {
125 mvaddch( iHdrHeight, iXPos+j+1, ACS_HLINE );
126 }
127 iXPos += doc.aWidths[iCol+iXOff]+2;
128 }
129 }
130 catch(...) { }
131
132 // Draw some data
133 for( int iRow = 0; iRow < iRows; iRow++ )
134 {
135 try
136 {
137 int iXPos = 6;
138 for( int iCol = 0; iCol < iCols; iCol++ )
139 {
140 if( iXPos + doc.aWidths[iCol+iXOff]+2 >= maxx )
141 break;
142 mvaddch( iRow+iHdrHeight+1, iXPos, ACS_VLINE );
143 mvaddstr( iRow+iHdrHeight+1, iXPos+1,
144 doc.sgData[iRow+iYOff][iCol+iXOff].getStr() );
145 iXPos += doc.aWidths[iCol+iXOff]+2;
146 }
147 } catch(...) { }
148 }
149 }
150
151 void move( int iX, int iY )
152 {
153 iXOff += iX;
154 iYOff += iY;
155 if( iXOff < 0 )
156 iXOff = 0;
157
158 if( bHeaderRow )
159 {
160 if( iYOff < 1 )
161 iYOff = 1;
162 }
163 else
164 {
165 if( iYOff < 0 )
166 iYOff = 0;
167 }
168 }
169
170 void pageDown()
171 {
172 int maxx, maxy;
173 getmaxyx( stdscr, maxy, maxx );
174 move( 0, maxy-2 );
175 }
176
177 void pageUp()
178 {
179 int maxx, maxy;
180 getmaxyx( stdscr, maxy, maxx );
181 move( 0, -(maxy-2) );
182 }
183
184 void setHeaderRow( bool bOn )
185 {
186 bHeaderRow = bOn;
187 move( 0, 0 );
188 }
189
190 void toggleHeaderRow()
191 {
192 setHeaderRow( !bHeaderRow );
193 }
194
195 CsvDoc &doc;
196 int iXOff;
197 int iYOff;
198 bool bHeaderRow;
199};
200
68int main( int argc, char *argv[] ) 201int main( int argc, char *argv[] )
69{ 202{
70 Options opt( argc, argv ); 203 Options opt( argc, argv );
@@ -87,8 +220,60 @@ int main( int argc, char *argv[] )
87 doc.addRow( sa ); 220 doc.addRow( sa );
88 } 221 }
89 222
90 sio << "Csv grid: " << doc.iMaxCols << " x " << doc.sgData.getSize() 223 initscr();
91 << sio.nl; 224 cbreak();
225 noecho();
226 nonl();
227 intrflush( stdscr, FALSE );
228 keypad( stdscr, TRUE );
229 curs_set( 0 );
230
231 CsvView view( doc );
232 view.toggleHeaderRow();
233
234 bool bRun = true;
235 do
236 {
237 view.render();
238
239 int ch = getch();
240 switch( ch )
241 {
242 case 'q':
243 bRun = false;
244 break;
245
246 case KEY_DOWN:
247 view.move( 0, 1 );
248 break;
249
250 case KEY_UP:
251 view.move( 0, -1 );
252 break;
253
254 case KEY_LEFT:
255 view.move( -1, 0 );
256 break;
257
258 case KEY_RIGHT:
259 view.move( 1, 0 );
260 break;
261
262 case KEY_NPAGE:
263 view.pageDown();
264 break;
265
266 case KEY_PPAGE:
267 view.pageUp();
268 break;
269
270 case 'h':
271 view.toggleHeaderRow();
272 break;
273 }
274 } while( bRun );
275
276 endwin();
92 277
93 return 0; 278 return 0;
94} 279}