コード例 #1
0
ファイル: CmdReports.cpp プロジェクト: SEJeff/task
int CmdReports::execute (std::string& output)
{
  std::vector <std::string> reports;

  // Add custom reports.
  std::vector <std::string> vars;
  context.config.all (vars);

  std::vector <std::string>::iterator i;
  for (i = vars.begin (); i != vars.end (); ++i)
  {
    if (i->substr (0, 7) == "report.")
    {
      std::string report = i->substr (7);
      std::string::size_type columns = report.find (".columns");
      if (columns != std::string::npos)
        reports.push_back (report.substr (0, columns));
    }
  }

  // Add known reports.
  reports.push_back ("burndown.daily");
  reports.push_back ("burndown.monthly");
  reports.push_back ("burndown.weekly");
  reports.push_back ("ghistory.annual");
  reports.push_back ("ghistory.monthly");
  reports.push_back ("history.annual");
  reports.push_back ("history.monthly");
  reports.push_back ("information");
  reports.push_back ("projects");
  reports.push_back ("summary");
  reports.push_back ("tags");

  std::sort (reports.begin (), reports.end ());

  // Compose the output.
  std::stringstream out;
  ViewText view;
  view.width (context.getWidth ());
  view.add (Column::factory ("string", STRING_CMD_REPORTS_REPORT));
  view.add (Column::factory ("string", STRING_CMD_REPORTS_DESC));

  // If an alternating row color is specified, notify the table.
  if (context.color ())
  {
    Color alternate (context.config.get ("color.alternate"));
    view.colorOdd (alternate);
    view.intraColorOdd (alternate);
  }

  std::vector <std::string>::iterator report;
  for (report = reports.begin (); report != reports.end (); ++report)
  {
    int row = view.addRow ();
    view.set (row, 0, *report);
    view.set (row, 1, context.commands[*report]->description ());
  }

  out << optionalBlankLine ()
      << view.render ()
      << optionalBlankLine ()
      << format (STRING_CMD_REPORTS_SUMMARY, reports.size ())
      << "\n";

  output = out.str ();
  return 0;
}
コード例 #2
0
ファイル: CmdStatistics.cpp プロジェクト: SEJeff/task
int CmdStatistics::execute (std::string& output)
{
  int rc = 0;
  std::stringstream out;

  std::string dateformat = context.config.get ("dateformat");

  // Go get the file sizes.
  size_t dataSize = context.tdb2.pending._file.size ()
                  + context.tdb2.completed._file.size ()
                  + context.tdb2.undo._file.size ()
/*
                  // TODO Re-enable this once 2.1 has taskd support.
                  + context.tdb2.backlog._file.size ()
                  + context.tdb2.synch_key._file.size ()
*/
                  ;

  // Count the undo transactions.
  std::vector <std::string> undoTxns = context.tdb2.undo.get_lines ();
  int undoCount = 0;
  std::vector <std::string>::iterator tx;
  for (tx = undoTxns.begin (); tx != undoTxns.end (); ++tx)
    if (*tx == "---")
      ++undoCount;

  // Get all the tasks.
  std::vector <Task> all = context.tdb2.all_tasks ();
  std::vector <Task> filtered;
  filter (all, filtered);

  Date now;
  time_t earliest   = time (NULL);
  time_t latest     = 1;
  int totalT        = 0;
  int deletedT      = 0;
  int pendingT      = 0;
  int completedT    = 0;
  int waitingT      = 0;
  int taggedT       = 0;
  int annotationsT  = 0;
  int recurringT    = 0;
  float daysPending = 0.0;
  int descLength    = 0;
  std::map <std::string, int> allTags;
  std::map <std::string, int> allProjects;

  std::vector <Task>::iterator task;
  for (task = filtered.begin (); task != filtered.end (); ++task)
  {
    ++totalT;

    Task::status status = task->getStatus ();
    switch (status)
    {
    case Task::deleted:   ++deletedT;   break;
    case Task::pending:   ++pendingT;   break;
    case Task::completed: ++completedT; break;
    case Task::recurring: ++recurringT; break;
    case Task::waiting:   ++waitingT;   break;
    }

    time_t entry = strtol (task->get ("entry").c_str (), NULL, 10);
    if (entry < earliest) earliest = entry;
    if (entry > latest)   latest   = entry;

    if (status == Task::completed)
    {
      time_t end = strtol (task->get ("end").c_str (), NULL, 10);
      daysPending += (end - entry) / 86400.0;
    }

    if (status == Task::pending)
      daysPending += (now.toEpoch () - entry) / 86400.0;

    descLength += task->get ("description").length ();

    std::map <std::string, std::string> annotations;
    task->getAnnotations (annotations);
    annotationsT += annotations.size ();

    std::vector <std::string> tags;
    task->getTags (tags);
    if (tags.size ()) ++taggedT;

    std::vector <std::string>::iterator t;
    for (t = tags.begin (); t != tags.end (); ++t)
      allTags[*t] = 0;

    std::string project = task->get ("project");
    if (project != "")
      allProjects[project] = 0;
  }

  // Create a table for output.
  ViewText view;
  view.width (context.getWidth ());
  view.intraPadding (2);
  view.add (Column::factory ("string", STRING_CMD_STATS_CATEGORY));
  view.add (Column::factory ("string", STRING_CMD_STATS_DATA));

  int row = view.addRow ();
  view.set (row, 0, STRING_COLUMN_LABEL_STAT_PE);
  view.set (row, 1, pendingT);

  row = view.addRow ();
  view.set (row, 0, STRING_COLUMN_LABEL_STAT_WA);
  view.set (row, 1, waitingT);

  row = view.addRow ();
  view.set (row, 0, STRING_COLUMN_LABEL_STAT_RE);
  view.set (row, 1, recurringT);

  row = view.addRow ();
  view.set (row, 0, STRING_COLUMN_LABEL_STAT_CO);
  view.set (row, 1, completedT);

  row = view.addRow ();
  view.set (row, 0, STRING_COLUMN_LABEL_STAT_DE);
  view.set (row, 1, deletedT);

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_TOTAL);
  view.set (row, 1, totalT);

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_ANNOTATIONS);
  view.set (row, 1, annotationsT);

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_UNIQUE_TAGS);
  view.set (row, 1, (int)allTags.size ());

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_PROJECTS);
  view.set (row, 1, (int)allProjects.size ());

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_DATA_SIZE);
  view.set (row, 1, formatBytes (dataSize));

  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_UNDO_TXNS);
  view.set (row, 1, undoCount);

  if (totalT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_TAGGED);

    std::stringstream value;
    value << std::setprecision (3) << (100.0 * taggedT / totalT) << "%";
    view.set (row, 1, value.str ());
  }

  if (filtered.size ())
  {
    Date e (earliest);
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_OLDEST);
    view.set (row, 1, e.toString (dateformat));

    Date l (latest);
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_NEWEST);
    view.set (row, 1, l.toString (dateformat));

    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_USED_FOR);
    view.set (row, 1, Duration (latest - earliest).format ());
  }

  if (totalT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_ADD_EVERY);
    view.set (row, 1, Duration (((latest - earliest) / totalT)).format ());
  }

  if (completedT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_COMP_EVERY);
    view.set (row, 1, Duration ((latest - earliest) / completedT).format ());
  }

  if (deletedT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_DEL_EVERY);
    view.set (row, 1, Duration ((latest - earliest) / deletedT).format ());
  }

  if (pendingT || completedT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_AVG_PEND);
    view.set (row, 1, Duration ((int) ((daysPending / (pendingT + completedT)) * 86400)).format ());
  }

  if (totalT)
  {
    row = view.addRow ();
    view.set (row, 0, STRING_CMD_STATS_DESC_LEN);
    view.set (row, 1, format (STRING_CMD_STATS_CHARS, (int) (descLength / totalT)));
  }

/*
  // TODO Re-enable this when 2.1 has taskd support.  Until then, it makes no
  //      sense to include this.
  row = view.addRow ();
  view.set (row, 0, STRING_CMD_STATS_LAST_SYNCH);
  if (context.tdb2.synch_key._file.exists ())
    view.set (row, 1, Date (context.tdb2.synch_key._file.mtime ()).toISO ());
  else
    view.set (row, 1, "-");
*/

  // If an alternating row color is specified, notify the table.
  if (context.color ())
  {
    Color alternate (context.config.get ("color.alternate"));
    if (alternate.nontrivial ())
    {
      view.colorOdd (alternate);
      view.intraColorOdd (alternate);
      view.extraColorOdd (alternate);
    }
  }

  out << optionalBlankLine ()
      << view.render ()
      << optionalBlankLine ();

  output = out.str ();
  return rc;
}
コード例 #3
0
ファイル: CmdInfo.cpp プロジェクト: codito/task
int CmdInfo::execute (std::string& output)
{
  int rc = 0;

  // Apply filter.
  std::vector <Task> filtered;
  filter (filtered);

  if (! filtered.size ())
  {
    context.footnote (STRING_FEEDBACK_NO_MATCH);
    rc = 1;
  }

  // Get the undo data.
  std::vector <std::string> undo;
  if (context.config.getBoolean ("journal.info"))
    undo = context.tdb2.undo.get_lines ();

  // Determine the output date format, which uses a hierarchy of definitions.
  //   rc.dateformat.info
  //   rc.dateformat
  std::string dateformat = context.config.get ("dateformat.info");
  if (dateformat == "")
    dateformat = context.config.get ("dateformat");

  std::string dateformatanno = context.config.get ("dateformat.annotation");
  if (dateformatanno == "")
    dateformatanno = dateformat;

  // Render each task.
  std::stringstream out;
  std::vector <Task>::iterator task;
  for (task = filtered.begin (); task != filtered.end (); ++task)
  {
    ViewText view;
    view.width (context.getWidth ());
    view.add (Column::factory ("string", STRING_COLUMN_LABEL_NAME));
    view.add (Column::factory ("string", STRING_COLUMN_LABEL_VALUE));

    // If an alternating row color is specified, notify the table.
    if (context.color ())
    {
      Color alternate (context.config.get ("color.alternate"));
      view.colorOdd (alternate);
      view.intraColorOdd (alternate);
    }

    Date now;

    // id
    int row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_ID);
    view.set (row, 1, (task->id ? format (task->id) : "-"));

    std::string status = ucFirst (Task::statusToText (task->getStatus ()));

    // description
    Color c;
    autoColorize (*task, c);
    std::string description = task->get ("description");
    int indent = context.config.getInteger ("indent.annotation");

    std::map <std::string, std::string> annotations;
    task->getAnnotations (annotations);
    std::map <std::string, std::string>::iterator ann;
    for (ann = annotations.begin (); ann != annotations.end (); ++ann)
      description += "\n"
                   + std::string (indent, ' ')
                   + Date (ann->first.substr (11)).toString (dateformatanno)
                   + " "
                   + ann->second;

    row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_DESC);
    view.set (row, 1, description, c);

    // status
    row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_STATUS);
    view.set (row, 1, status);

    // project
    if (task->has ("project"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_PROJECT);
      view.set (row, 1, task->get ("project"));
    }

    // priority
    if (task->has ("priority"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_PRIORITY);
      view.set (row, 1, task->get ("priority"));
    }

    // dependencies: blocked
    {
      std::vector <Task> blocked;
      dependencyGetBlocking (*task, blocked);
      if (blocked.size ())
      {
        std::stringstream message;
        std::vector <Task>::const_iterator it;
        for (it = blocked.begin (); it != blocked.end (); ++it)
          message << it->id << " " << it->get ("description") << "\n";

        row = view.addRow ();
        view.set (row, 0, STRING_CMD_INFO_BLOCKED);
        view.set (row, 1, message.str ());
      }
    }

    // dependencies: blocking
    {
      std::vector <Task> blocking;
      dependencyGetBlocked (*task, blocking);
      if (blocking.size ())
      {
        std::stringstream message;
        std::vector <Task>::const_iterator it;
        for (it = blocking.begin (); it != blocking.end (); ++it)
          message << it->id << " " << it->get ("description") << "\n";

        row = view.addRow ();
        view.set (row, 0, STRING_CMD_INFO_BLOCKING);
        view.set (row, 1, message.str ());
      }
    }

    // recur
    if (task->has ("recur"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_RECUR_L);
      view.set (row, 1, task->get ("recur"));
    }

    // until
    if (task->has ("until"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_CMD_INFO_UNTIL);
      view.set (row, 1, Date (task->get_date ("until")).toString (dateformat));
    }

    // mask
    if (task->getStatus () == Task::recurring)
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_MASK);
      view.set (row, 1, task->get ("mask"));
    }

    if (task->has ("parent"))
    {
      // parent
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_PARENT);
      view.set (row, 1, task->get ("parent"));

      // imask
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_MASK_IDX);
      view.set (row, 1, task->get ("imask"));
    }

    // due (colored)
    if (task->has ("due"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_DUE);
      view.set (row, 1, Date (task->get_date ("due")).toString (dateformat));
    }

    // wait
    if (task->has ("wait"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_WAITING);
      view.set (row, 1, Date (task->get_date ("wait")).toString (dateformat));
    }

    // scheduled
    if (task->has ("scheduled"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_SCHED);
      view.set (row, 1, Date (task->get_date ("scheduled")).toString (dateformat));
    }

    // start
    if (task->has ("start"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_START);
      view.set (row, 1, Date (task->get_date ("start")).toString (dateformat));
    }

    // end
    if (task->has ("end"))
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_END);
      view.set (row, 1, Date (task->get_date ("end")).toString (dateformat));
    }

    // tags ...
    std::vector <std::string> tags;
    task->getTags (tags);
    if (tags.size ())
    {
      std::string allTags;
      join (allTags, " ", tags);

      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_TAGS);
      view.set (row, 1, allTags);
    }

    // uuid
    row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_UUID);
    std::string uuid = task->get ("uuid");
    view.set (row, 1, uuid);

    // entry
    row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_ENTERED);
    Date dt (task->get_date ("entry"));
    std::string entry = dt.toString (dateformat);

    std::string age;
    std::string created = task->get ("entry");
    if (created.length ())
    {
      Date dt (strtol (created.c_str (), NULL, 10));
      age = Duration (now - dt).format ();
    }

    view.set (row, 1, entry + " (" + age + ")");

    // fg TODO deprecated 2.0
    std::string color = task->get ("fg");
    if (color != "")
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_FG);
      view.set (row, 1, color);
    }

    // bg TODO deprecated 2.0
    color = task->get ("bg");
    if (color != "")
    {
      row = view.addRow ();
      view.set (row, 0, STRING_COLUMN_LABEL_BG);
      view.set (row, 1, color);
    }

    // Task::urgency
    row = view.addRow ();
    view.set (row, 0, STRING_COLUMN_LABEL_URGENCY);
    view.set (row, 1, trimLeft (format (task->urgency (), 4, 4)));

    // Show any UDAs
    std::vector <std::string> all = task->all ();
    std::vector <std::string>::iterator att;
    std::string type;
    for (att = all.begin (); att != all.end (); ++att)
    {
      type = context.config.get ("uda." + *att + ".type");
      if (type != "")
      {
        Column* col = context.columns[*att];
        if (col)
        {
          std::string value = task->get (*att);
          if (value != "")
          {
            row = view.addRow ();
            view.set (row, 0, col->label ());

            if (type == "date")
              value = Date (value).toString (dateformat);
            else if (type == "duration")
              value = Duration (value).formatCompact ();

            view.set (row, 1, value);
          }
        }
      }
    }

    // Show any orphaned UDAs, which are identified by not being represented in
    // the context.columns map.
    for (att = all.begin (); att != all.end (); ++att)
      if (att->substr (0, 11) != "annotation_" &&
          context.columns.find (*att) == context.columns.end ())
      {
         row = view.addRow ();
         view.set (row, 0, "[" + *att);
         view.set (row, 1, task->get (*att) + "]");
      }

    // Create a second table, containing undo log change details.
    ViewText journal;

    // If an alternating row color is specified, notify the table.
    if (context.color ())
    {
      Color alternate (context.config.get ("color.alternate"));
      journal.colorOdd (alternate);
      journal.intraColorOdd (alternate);
    }

    journal.width (context.getWidth ());
    journal.add (Column::factory ("string", STRING_COLUMN_LABEL_DATE));
    journal.add (Column::factory ("string", STRING_CMD_INFO_MODIFICATION));

    if (context.config.getBoolean ("journal.info") &&
        undo.size () > 3)
    {
      // Scan the undo data for entries matching this task.
      std::string when;
      std::string previous;
      std::string current;
      unsigned int i = 0;
      long total_time = 0;
      while (i < undo.size ())
      {
        when = undo[i++];
        previous = "";
        if (undo[i].substr (0, 3) == "old")
          previous = undo[i++];

        current = undo[i++];
        i++; // Separator

        if (current.find ("uuid:\"" + uuid) != std::string::npos)
        {
          if (previous != "")
          {
            int row = journal.addRow ();

            Date timestamp (strtol (when.substr (5).c_str (), NULL, 10));
            journal.set (row, 0, timestamp.toString (dateformat));

            Task before (previous.substr (4));
            Task after (current.substr (4));
            journal.set (row, 1, taskInfoDifferences (before, after, dateformat));

            // calculate the total active time
            if (before.get ("start") == ""
              && after.get ("start") != "")
            {
              // task started
              total_time -= timestamp.toEpoch ();
            }
            else if (((before.get ("start") != "" &&
                       after.get ("start") == "") ||
                      (before.get ("status") != "completed" &&
                       after.get ("status") == "completed")) &&
                     total_time < 0)
            {
              // task stopped or done
              total_time += timestamp.toEpoch ();
            }
          }
        }
      }

      // add now() if task is still active
      if (total_time < 0)
        total_time += Date ().toEpoch ();

      // print total active time
      if (total_time > 0)
      {
        row = journal.addRow ();
        journal.set (row, 0, STRING_CMD_INFO_TOTAL_ACTIVE);
        journal.set (row, 1, Duration (total_time).formatPrecise (),
                     (context.color () ? Color ("bold") : Color ()));
      }
    }

    out << optionalBlankLine ()
        << view.render ()
        << "\n";

    if (journal.rows () > 0)
      out << journal.render ()
          << "\n";
  }

  output = out.str ();
  return rc;
}