Better undo output (and remove undo.style config) (#3672)

This commit is contained in:
Dustin J. Mitchell 2024-11-07 14:56:34 -05:00 committed by GitHub
parent dcc8a8cdde
commit a2f9b92d6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 105 additions and 331 deletions

View file

@ -202,7 +202,6 @@ int CmdShow::execute(std::string& output) {
" sync.server.url"
" sync.server.origin"
" tag.indicator"
" undo.style"
" urgency.active.coefficient"
" urgency.scheduled.coefficient"
" urgency.annotations.coefficient"

View file

@ -29,6 +29,13 @@
#include <CmdUndo.h>
#include <Context.h>
#include <Operation.h>
#include <Task.h>
#include <iostream>
#include <sstream>
#include "shared.h"
////////////////////////////////////////////////////////////////////////////////
CmdUndo::CmdUndo() {
@ -47,8 +54,94 @@ CmdUndo::CmdUndo() {
////////////////////////////////////////////////////////////////////////////////
int CmdUndo::execute(std::string&) {
Context::getContext().tdb2.revert();
auto& replica = Context::getContext().tdb2.replica();
rust::Vec<tc::Operation> undo_ops = replica->get_undo_operations();
if (confirm_revert(Operation::operations(undo_ops))) {
// Note that commit_reversed_operations rebuilds the working set, so that
// need not be done here.
if (!replica->commit_reversed_operations(std::move(undo_ops))) {
std::cout << "Could not undo: other operations have occurred.";
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
bool CmdUndo::confirm_revert(const std::vector<Operation>& undo_ops) {
// Count non-undo operations
int ops_count = 0;
for (auto& op : undo_ops) {
if (!op.is_undo_point()) {
ops_count++;
}
}
if (ops_count == 0) {
std::cout << "No operations to undo.\n";
return true;
}
std::cout << "The following " << ops_count << " operations would be reverted:\n";
Table view;
if (Context::getContext().config.getBoolean("obfuscate")) view.obfuscate();
view.width(Context::getContext().getWidth());
view.add("Uuid");
view.add("Modification");
std::string last_uuid;
std::stringstream mods;
for (auto& op : undo_ops) {
if (op.is_undo_point()) {
continue;
}
if (last_uuid != op.get_uuid()) {
if (last_uuid.size() != 0) {
int row = view.addRow();
view.set(row, 0, last_uuid);
view.set(row, 1, mods.str());
}
last_uuid = op.get_uuid();
mods.clear();
}
if (op.is_create()) {
mods << "Create task\n";
} else if (op.is_delete()) {
mods << "Delete (purge) task";
} else if (op.is_update()) {
auto property = op.get_property();
auto old_value = op.get_old_value();
auto value = op.get_value();
if (Task::isTagAttr(property)) {
if (value && *value == "x") {
mods << "Add tag '" << Task::attr2Tag(property) << "'\n";
continue;
} else if (!value && old_value && *old_value == "x") {
mods << "Remove tag '" << Task::attr2Tag(property) << "'\n";
continue;
}
}
if (old_value && value) {
mods << "Update property '" << property << "' from '" << *old_value << "' to '" << *value
<< "'\n";
} else if (old_value) {
mods << "Delete property '" << property << "' (was '" << *old_value << "')\n";
} else if (value) {
mods << "Add property '" << property << "' with value '" << *value << "'\n";
}
}
}
int row = view.addRow();
view.set(row, 0, last_uuid);
view.set(row, 1, mods.str());
std::cout << view.render() << "\n";
return !Context::getContext().config.getBoolean("confirmation") ||
confirm(
"The undo command is not reversible. Are you sure you want to revert to the previous "
"state?");
return true;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -28,13 +28,18 @@
#define INCLUDED_CMDUNDO
#include <Command.h>
#include <Operation.h>
#include <string>
#include <vector>
class CmdUndo : public Command {
public:
CmdUndo();
int execute(std::string&);
int execute(std::string &);
private:
bool confirm_revert(const std::vector<Operation> &);
};
#endif