PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/task-2.0.0/src/columns/ColUDA.cpp

#
C++ | 162 lines | 105 code | 15 blank | 42 comment | 41 complexity | 63258082ca2239de65692e09399b0f8a MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // taskwarrior - a command line task list manager.
  3. //
  4. // Copyright 2006-2012, Paul Beckingham, Federico Hernandez.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included
  14. // in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. // http://www.opensource.org/licenses/mit-license.php
  25. //
  26. ////////////////////////////////////////////////////////////////////////////////
  27. #define L10N // Localization complete.
  28. #include <Context.h>
  29. #include <Duration.h>
  30. #include <Date.h>
  31. #include <ColUDA.h>
  32. #include <text.h>
  33. #include <i18n.h>
  34. #include <stdlib.h>
  35. extern Context context;
  36. ////////////////////////////////////////////////////////////////////////////////
  37. ColumnUDA::ColumnUDA ()
  38. {
  39. _name = "<uda>";
  40. _type = "string";
  41. _style = "default";
  42. _label = "";
  43. _hyphenate = (_type == "string") ? true : false;
  44. _styles.push_back (_style);
  45. // TODO _examples.push_back ("?");
  46. }
  47. ////////////////////////////////////////////////////////////////////////////////
  48. ColumnUDA::~ColumnUDA ()
  49. {
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Set the minimum and maximum widths for the value.
  53. //
  54. void ColumnUDA::measure (Task& task, int& minimum, int& maximum)
  55. {
  56. minimum = maximum = 0;
  57. if (_style == "default")
  58. {
  59. std::string value = task.get (_name);
  60. if (value != "")
  61. {
  62. if (_type == "date")
  63. {
  64. // Determine the output date format, which uses a hierarchy of definitions.
  65. // rc.report.<report>.dateformat
  66. // rc.dateformat.report
  67. // rc.dateformat.
  68. Date date ((time_t) strtol (value.c_str (), NULL, 10));
  69. std::string format = context.config.get ("report." + _report + ".dateformat");
  70. if (format == "")
  71. format = context.config.get ("dateformat.report");
  72. if (format == "")
  73. format = context.config.get ("dateformat");
  74. minimum = maximum = date.toString (format).length ();
  75. }
  76. else if (_type == "duration")
  77. {
  78. minimum = maximum = Duration (value).formatCompact ().length ();
  79. }
  80. else if (_type == "string")
  81. {
  82. std::string stripped = Color::strip (value);
  83. maximum = longestLine (stripped);
  84. minimum = longestWord (stripped);
  85. }
  86. else if (_type == "numeric")
  87. {
  88. minimum = maximum = value.length ();
  89. }
  90. }
  91. }
  92. else
  93. throw format (STRING_COLUMN_BAD_FORMAT, _name, _style);
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////
  96. void ColumnUDA::render (
  97. std::vector <std::string>& lines,
  98. Task& task,
  99. int width,
  100. Color& color)
  101. {
  102. if (_style == "default")
  103. {
  104. std::string value = task.get (_name);
  105. if (value != "")
  106. {
  107. if (_type == "date")
  108. {
  109. // Determine the output date format, which uses a hierarchy of definitions.
  110. // rc.report.<report>.dateformat
  111. // rc.dateformat.report
  112. // rc.dateformat.
  113. std::string format = context.config.get ("report." + _report + ".dateformat");
  114. if (format == "")
  115. format = context.config.get ("dateformat.report");
  116. if (format == "")
  117. format = context.config.get ("dateformat");
  118. lines.push_back (
  119. color.colorize (
  120. leftJustify (
  121. Date ((time_t) strtol (value.c_str (), NULL, 10))
  122. .toString (format), width)));
  123. }
  124. else if (_type == "duration")
  125. {
  126. lines.push_back (
  127. color.colorize (
  128. rightJustify (
  129. Duration (value).formatCompact (),
  130. width)));
  131. }
  132. else if (_type == "string")
  133. {
  134. std::vector <std::string> raw;
  135. wrapText (raw, value, width, _hyphenate);
  136. std::vector <std::string>::iterator i;
  137. for (i = raw.begin (); i != raw.end (); ++i)
  138. lines.push_back (color.colorize (leftJustify (*i, width)));
  139. }
  140. else if (_type == "numeric")
  141. {
  142. lines.push_back (color.colorize (rightJustify (value, width)));
  143. }
  144. }
  145. }
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////