From 2da4f8ba7a29ab791414839f1e980cae95da24e1 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 10 Jun 2009 00:35:37 -0400 Subject: [PATCH] Enhancements - Context::getWidth - Added help function to reduce general code size. --- src/Context.h | 2 + src/command.cpp | 12 +--- src/interactive.cpp | 17 ++++++ src/report.cpp | 136 +++++++------------------------------------- 4 files changed, 39 insertions(+), 128 deletions(-) diff --git a/src/Context.h b/src/Context.h index 8bfdf26e2..5d3d6e0d8 100644 --- a/src/Context.h +++ b/src/Context.h @@ -49,6 +49,8 @@ public: void dispatch (); // command handler dispatch void shadow (); // shadow file update + int getWidth (); // determine terminal width + void message (const std::string&); // Message sink void footnote (const std::string&); // Footnote sink diff --git a/src/command.cpp b/src/command.cpp index 25396c1db..1aea1dae7 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -271,18 +271,8 @@ std::string handleVersion () { std::stringstream out; - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif - // Create a table for the disclaimer. + int width = context.getWidth (); Table disclaimer; disclaimer.setTableWidth (width); disclaimer.addColumn (" "); diff --git a/src/interactive.cpp b/src/interactive.cpp index 303b1924a..d50dcce6d 100644 --- a/src/interactive.cpp +++ b/src/interactive.cpp @@ -126,3 +126,20 @@ int Context::interactive () } //////////////////////////////////////////////////////////////////////////////// +int Context::getWidth () +{ + // Determine window size, and set table accordingly. + int width = config.get ("defaultwidth", (int) 80); +#ifdef HAVE_LIBNCURSES + if (config.get ("curses", true)) + { + WINDOW* w = initscr (); + width = w->_maxx + 1; + endwin (); + } +#endif + + return width; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/report.cpp b/src/report.cpp index 36eaefd80..4ae458549 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -51,6 +51,7 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// +// TODO Obsolete void filterSequence (std::vector& all, T& task) { std::vector sequence = task.getAllIds (); @@ -98,6 +99,7 @@ void filterSequence (std::vector& all, T& task) } //////////////////////////////////////////////////////////////////////////////// +// TODO Obsolete void filter (std::vector& all, T& task) { std::vector filtered; @@ -207,18 +209,8 @@ void filter (std::vector& all, T& task) std::string handleCompleted () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get the pending tasks. std::vector tasks; tdb.completedT (tasks); @@ -228,7 +220,7 @@ std::string handleCompleted () // Create a table for output. Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); table.addColumn ("Done"); table.addColumn ("Project"); @@ -313,18 +305,8 @@ std::string handleCompleted () std::string handleInfo () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get all the tasks. std::vector tasks; tdb.allPendingT (tasks); @@ -730,17 +712,6 @@ std::string handleReportNext () std::vector matching; gatherNextTasks (tdb, task, pending, matching); - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif - // Get the pending tasks. std::vector tasks; tdb.pendingT (tasks); @@ -750,7 +721,7 @@ std::string handleReportNext () // Create a table for output. Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); table.addColumn ("ID"); table.addColumn ("Project"); @@ -1098,17 +1069,8 @@ std::string handleReportHistory () std::string handleReportGHistory () { std::stringstream out; + /* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif int widthOfBar = width - 15; // 15 == strlen ("2008 September ") std::map groups; @@ -1343,18 +1305,8 @@ std::string handleReportGHistory () std::string handleReportTimesheet () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get all the tasks. std::vector tasks; tdb.allT (tasks); @@ -1393,7 +1345,7 @@ std::string handleReportTimesheet () // Render the completed table. Table completed; - completed.setTableWidth (width); + completed.setTableWidth (context.getWidth ()); completed.addColumn (" "); completed.addColumn ("Project"); completed.addColumn ("Due"); @@ -1464,7 +1416,7 @@ std::string handleReportTimesheet () // Now render the started table. Table started; - started.setTableWidth (width); + started.setTableWidth (context.getWidth ()); started.addColumn (" "); started.addColumn ("Project"); started.addColumn ("Due"); @@ -1707,20 +1659,11 @@ std::string renderMonths ( std::string handleReportCalendar () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Each month requires 28 text columns width. See how many will actually // fit. But if a preference is specified, and it fits, use it. + int width = context.getWidth (); int preferredMonthsPerLine = (context.config.get (std::string ("monthsperline"), 0)); int monthsThatFit = width / 26; @@ -1833,18 +1776,8 @@ std::string handleReportCalendar () std::string handleReportActive () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get all the tasks. std::vector tasks; tdb.pendingT (tasks); @@ -1854,7 +1787,7 @@ std::string handleReportActive () // Create a table for output. Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); table.addColumn ("ID"); table.addColumn ("Project"); @@ -1967,18 +1900,8 @@ std::string handleReportActive () std::string handleReportOverdue () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get all the tasks. std::vector tasks; tdb.pendingT (tasks); @@ -1988,7 +1911,7 @@ std::string handleReportOverdue () // Create a table for output. Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); table.addColumn ("ID"); table.addColumn ("Project"); @@ -2091,18 +2014,8 @@ std::string handleReportOverdue () std::string handleReportStats () { std::stringstream out; -/* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif +/* // Get all the tasks. std::vector tasks; tdb.allT (tasks); @@ -2165,7 +2078,7 @@ std::string handleReportStats () // Create a table for output. Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setTableIntraPadding (2); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); table.addColumn ("Category"); @@ -2480,17 +2393,6 @@ void gatherNextTasks ( std::string handleCustomReport (const std::string& report) { /* - // Determine window size, and set table accordingly. - int width = context.config.get ("defaultwidth", (int) 80); -#ifdef HAVE_LIBNCURSES - if (context.config.get ("curses", true)) - { - WINDOW* w = initscr (); - width = w->_maxx + 1; - endwin (); - } -#endif - // Load report configuration. std::string columnList = context.config.get ("report." + report + ".columns"); std::vector columns; @@ -2538,7 +2440,7 @@ std::string handleCustomReport (const std::string& report) initializeColorRules (); Table table; - table.setTableWidth (width); + table.setTableWidth (context.getWidth ()); table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); for (unsigned int i = 0; i < tasks.size (); ++i)