aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-05-06 15:12:39 -0400
committerYour Name <you@example.com>2021-05-06 15:12:39 -0400
commit5ed030a38810e4a3bb9c969db6892065581340c6 (patch)
treeeac8fa93a221d7b4cbccacd18adf7cb83282e04f
parent9f3802690f9dd9452e96d1d7a879291978d66e35 (diff)
downloaddmtool-5ed030a38810e4a3bb9c969db6892065581340c6.tar.gz
dmtool-5ed030a38810e4a3bb9c969db6892065581340c6.tar.bz2
dmtool-5ed030a38810e4a3bb9c969db6892065581340c6.zip
Implemented edit method
-rw-r--r--src/cmd.h13
-rw-r--r--src/cmd_manipulate.cc21
-rw-r--r--src/dmtool.cc1
-rw-r--r--src/entry.h3
-rw-r--r--src/settings.cc3
5 files changed, 37 insertions, 4 deletions
diff --git a/src/cmd.h b/src/cmd.h
index 6a8b738..92a3cc3 100644
--- a/src/cmd.h
+++ b/src/cmd.h
@@ -5,20 +5,29 @@
namespace cmd {
// Corresponds to commands
+
+ // Usage gets a category of its own
std::string usage(const std::string& exename);
+
+ // Filesystem operations
std::string list(std::vector<std::string> args);
std::string mkdir(std::vector<std::string> args);
std::string cp(std::vector<std::string> args);
std::string mv(std::vector<std::string> args);
std::string rm(std::vector<std::string> args);
- std::string attacks(std::vector<std::string> args);
- std::string roll(std::vector<std::string> args);
+
+ // Manipulators
std::string heal(std::vector<std::string> args);
std::string damage(std::vector<std::string> args, std::vector<std::string> flags);
std::string reset(std::vector<std::string> args);
std::string set(std::vector<std::string> args);
std::string add(std::vector<std::string> args);
std::string del(std::vector<std::string> args);
+ std::string edit(std::vector<std::string> args);
+
+ //Queries
+ std::string attacks(std::vector<std::string> args);
+ std::string roll(std::vector<std::string> args);
// Command-centric helpers
diff --git a/src/cmd_manipulate.cc b/src/cmd_manipulate.cc
index fe59ebf..1300008 100644
--- a/src/cmd_manipulate.cc
+++ b/src/cmd_manipulate.cc
@@ -3,8 +3,12 @@
#include "creature.h"
#include "item.h"
#include "spellcasting.h"
+#include "settings.h"
#include <sstream>
#include <memory>
+#include <cstdlib>
+#include <fstream>
+#include <iterator>
namespace fs = std::filesystem;
@@ -183,4 +187,21 @@ namespace cmd {
}
return text.str();
}
+
+ std::string edit(std::vector<std::string> args) {
+ auto p = getTruePath(args[0]);
+ auto e = utils::instantiate<entry::Entry>(p);
+ auto editor = settings::getString("editor");
+ // General workflow: copy notes (text) from e to a temp file, edit it, then copy back.
+ fs::path tmp("/tmp/dmtool.tmp");
+ std::ofstream out(tmp);
+ out << e->Entry::getText();
+ out.close();
+ std::system((editor + " " + tmp.string()).c_str());
+ std::ifstream in(tmp);
+ std::string newText(std::istreambuf_iterator<char>{in}, {});
+ e->setText(newText);
+ utils::saveJson(*e, p);
+ return "";
+ }
}
diff --git a/src/dmtool.cc b/src/dmtool.cc
index e9d8ed6..30f73b5 100644
--- a/src/dmtool.cc
+++ b/src/dmtool.cc
@@ -67,6 +67,7 @@ int main(int argc, char *argv[]) {
else if(cmd == "set") std::cout << cmd::set(args);
else if(cmd == "add") std::cout << cmd::add(args);
else if(cmd == "del") std::cout << cmd::del(args);
+ else if(cmd == "edit") std::cout << cmd::edit(args);
else if(cmd == "help") std::cout << cmd::usage(exename);
else std::cout << cmd::list(argsOrig);
} catch(std::exception& e) {
diff --git a/src/entry.h b/src/entry.h
index ced8544..c745097 100644
--- a/src/entry.h
+++ b/src/entry.h
@@ -21,6 +21,7 @@ namespace entry {
std::string getName(void) const {return name;}
std::string getType(void) const {return type;}
virtual std::string getText(void) const {return text;}
+ void setText(std::string t) {text = t;}
virtual std::string getText(const creature::Creature& c) const {
//return genText(*this, c);
return genText(*this, c) + ": " + getText();
@@ -44,6 +45,6 @@ namespace entry {
const std::string entry;
const std::string name;
const std::string type;
- const std::string text;
+ std::string text;
};
}
diff --git a/src/settings.cc b/src/settings.cc
index 1ae3f07..380ae62 100644
--- a/src/settings.cc
+++ b/src/settings.cc
@@ -10,7 +10,8 @@ namespace settings {
{"armor", "/usr/share/dmtool/armor/"},
{"spells", "/usr/share/dmtool/spells/"},
{"creatures", "/usr/share/dmtool/creatures/"},
- {"savedir", "~/.dmtool/"}
+ {"savedir", "~/.dmtool/"},
+ {"editor", "vi"}
};