From 6fdb5f76b3036e24dd3c99ec51c74405d8600bb7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 21 Jun 2010 18:26:58 +0000 Subject: The basic viewcsv program works, it uses ncurses to display a csv file in a tabular way, like a spreadsheet, only raw. It displays exactly what libbu++ reads from the csv file. --- default.bld | 5 ++ src/csvreader.cpp | 2 +- src/tools/viewcsv.cpp | 189 +++++++++++++++++++++++++++++++++++++++++++++++++- 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","") LDFLAGS += "-L. -lbu++"; } +target "viewcsv" +{ + LDFLAGS += "-lncurses"; +} + // // General Tests // 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 ) : break; case styleC: - sDecode = Bu::slot( &decodeExcel ); + sDecode = Bu::slot( &decodeC ); break; } } 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 @@ #include #include #include +#include +#include using namespace Bu; @@ -65,6 +67,137 @@ public: IntArray aWidths; }; +class CsvView +{ +public: + CsvView( CsvDoc &doc ) : + doc( doc ), + iXOff( 0 ), + iYOff( 0 ), + bHeaderRow( false ) + { + } + + virtual ~CsvView() + { + } + + void render() + { + erase(); + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + + int iRows = min( (int)doc.sgData.getSize(), maxy-1 ); + int iCols = min( doc.iMaxCols, (int)maxx-1 ); + + int iHdrHeight = 1; + if( bHeaderRow ) + iHdrHeight++; + + // Draw the headers + for( int iRow = 0; iRow < iRows; iRow++ ) + { + char buf[6]; + snprintf( buf, 6, "%5d", iRow+iYOff ); + mvaddnstr( iRow+iHdrHeight+1, 0, buf, 5 ); + mvaddch( iRow+iHdrHeight+1, 6, ACS_VLINE ); + } + int iXPos = 6; + try + { + for( int iCol = 0; iCol < iCols; iCol++ ) + { + if( iXPos + doc.aWidths[iCol+iXOff]+2 >= maxx ) + break; + char buf[6]; + snprintf( buf, 6, "%d", iCol+iXOff ); + mvaddch( 0, iXPos, ACS_VLINE ); + mvaddch( iHdrHeight, iXPos, ACS_PLUS ); + mvaddnstr( 0, iXPos+1, buf, 5 ); + if( bHeaderRow ) + { + mvaddstr( 1, iXPos+1, doc.sgData[0][iCol+iXOff].getStr() ); + mvaddch( 1, iXPos, ACS_VLINE ); + } + for( int j = 0; j <= doc.aWidths[iCol+iXOff]; j++ ) + { + mvaddch( iHdrHeight, iXPos+j+1, ACS_HLINE ); + } + iXPos += doc.aWidths[iCol+iXOff]+2; + } + } + catch(...) { } + + // Draw some data + for( int iRow = 0; iRow < iRows; iRow++ ) + { + try + { + int iXPos = 6; + for( int iCol = 0; iCol < iCols; iCol++ ) + { + if( iXPos + doc.aWidths[iCol+iXOff]+2 >= maxx ) + break; + mvaddch( iRow+iHdrHeight+1, iXPos, ACS_VLINE ); + mvaddstr( iRow+iHdrHeight+1, iXPos+1, + doc.sgData[iRow+iYOff][iCol+iXOff].getStr() ); + iXPos += doc.aWidths[iCol+iXOff]+2; + } + } catch(...) { } + } + } + + void move( int iX, int iY ) + { + iXOff += iX; + iYOff += iY; + if( iXOff < 0 ) + iXOff = 0; + + if( bHeaderRow ) + { + if( iYOff < 1 ) + iYOff = 1; + } + else + { + if( iYOff < 0 ) + iYOff = 0; + } + } + + void pageDown() + { + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + move( 0, maxy-2 ); + } + + void pageUp() + { + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + move( 0, -(maxy-2) ); + } + + void setHeaderRow( bool bOn ) + { + bHeaderRow = bOn; + move( 0, 0 ); + } + + void toggleHeaderRow() + { + setHeaderRow( !bHeaderRow ); + } + + CsvDoc &doc; + int iXOff; + int iYOff; + bool bHeaderRow; +}; + int main( int argc, char *argv[] ) { Options opt( argc, argv ); @@ -87,8 +220,60 @@ int main( int argc, char *argv[] ) doc.addRow( sa ); } - sio << "Csv grid: " << doc.iMaxCols << " x " << doc.sgData.getSize() - << sio.nl; + initscr(); + cbreak(); + noecho(); + nonl(); + intrflush( stdscr, FALSE ); + keypad( stdscr, TRUE ); + curs_set( 0 ); + + CsvView view( doc ); + view.toggleHeaderRow(); + + bool bRun = true; + do + { + view.render(); + + int ch = getch(); + switch( ch ) + { + case 'q': + bRun = false; + break; + + case KEY_DOWN: + view.move( 0, 1 ); + break; + + case KEY_UP: + view.move( 0, -1 ); + break; + + case KEY_LEFT: + view.move( -1, 0 ); + break; + + case KEY_RIGHT: + view.move( 1, 0 ); + break; + + case KEY_NPAGE: + view.pageDown(); + break; + + case KEY_PPAGE: + view.pageUp(); + break; + + case 'h': + view.toggleHeaderRow(); + break; + } + } while( bRun ); + + endwin(); return 0; } -- cgit v1.2.3