Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/commands/CmdInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,50 @@

#include <algorithm>
#include <sstream>
#include <string>

////////////////////////////////////////////////////////////////////////////////

static std::string wrapWithPrefixes(const std::string& text, size_t width,
const std::string& prefixFirst, const std::string& prefixCont) {
Comment on lines +51 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's existing wrapping support in src/libshared/src/shared.cpp, and I think that could be re-used for this purpose rather than re-implementing it.

if (width == 0) return prefixFirst + text;

std::ostringstream out;
std::istringstream is(text);
std::string word, line;
bool firstLine = true;

auto safeSub = [](size_t a, size_t b) { return (a > b) ? (a - b) : 0u; };
const size_t effFirst = safeSub(width, prefixFirst.size());
const size_t effCont = safeSub(width, prefixCont.size());

auto flush = [&](bool isFirst) {
if (!line.empty()) {
out << (isFirst ? prefixFirst : prefixCont) << line << '\n';
line.clear();
}
};

while (is >> word) {
size_t eff = firstLine ? effFirst : effCont;
if (line.empty()) {
line = word;
} else if (line.size() + 1 + word.size() <= eff) {
line += ' ';
line += word;
} else {
flush(firstLine);
firstLine = false;
line = word;
}
}
flush(firstLine);

std::string s = out.str();
if (!s.empty() && s.back() == '\n') s.pop_back();
return s;
}

CmdInfo::CmdInfo() {
_keyword = "information";
_usage = "task <filter> information";
Expand Down Expand Up @@ -116,9 +158,27 @@ int CmdInfo::execute(std::string& output) {
auto description = task.get("description");
auto indent = Context::getContext().config.getInteger("indent.annotation");

for (auto& anno : task.getAnnotations())
description += '\n' + std::string(indent, ' ') +
Datetime(anno.first.substr(11)).toString(dateformatanno) + ' ' + anno.second;
{
const size_t termWidth = Context::getContext().getWidth();
const size_t approxNameCol = 20; // conservative; covers labels like "Last modified"
const size_t valueWidth = termWidth > approxNameCol ? termWidth - approxNameCol : termWidth;
Comment on lines +162 to +164
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this seems a bit of a hack, as it's "guessing" the size of the [Table](https://github.com/GothenburgBitFactory/libshared/blob/master/src/Table.h) column, and wrapping to that length. I suppose if the guess is incorrect you just get some weirdly line-broken text, so not a horrible bug. But maybe we could do better?

If that requires improvements to the libshared repo, that's fine -- it's only used by Taskwarrior and Timewarrior so we can modify it pretty easily.


for (const auto& anno : task.getAnnotations()) {
// first line looks exactly like before: <indent><timestamp><space>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"before" won't mean much to a reader outside the context of this PR -- rephrase to the context of the code

Suggested change
// first line looks exactly like before: <indent><timestamp><space>
// first line looks like <indent><timestamp><space>

const std::string firstPrefix = std::string(indent, ' ') +
Datetime(anno.first.substr(11)).toString(dateformatanno) +
' ';

// continuation lines: same width, all spaces
const std::string contPrefix(firstPrefix.size(), ' ');

// wrap just the annotation body with our prefixes
const std::string wrapped =
wrapWithPrefixes(anno.second, valueWidth, firstPrefix, contPrefix);

description += '\n' + wrapped;
}
}

if (task.has("description")) {
row = view.addRow();
Expand Down
Loading