Spaces:
Paused
Paused
Create pencil_utils.hpp
Browse files- pencil_utils.hpp +73 -0
pencil_utils.hpp
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// pencil_utils.hpp – Notepad logic: directories, templates, session log
|
| 2 |
+
#ifndef PENCIL_UTILS_HPP
|
| 3 |
+
#define PENCIL_UTILS_HPP
|
| 4 |
+
|
| 5 |
+
#include <iostream>
|
| 6 |
+
#include <fstream>
|
| 7 |
+
#include <string>
|
| 8 |
+
#include <filesystem>
|
| 9 |
+
#include <map>
|
| 10 |
+
|
| 11 |
+
namespace pencil {
|
| 12 |
+
const std::string PENCIL_DIR = "./pencil_data/";
|
| 13 |
+
const std::string SESSION_LOG = "session.log";
|
| 14 |
+
const std::string BOOK_FILE = "book.txt";
|
| 15 |
+
|
| 16 |
+
// Ensure the working directory exists. Returns true on success or if already exists.
|
| 17 |
+
inline bool init_workspace() {
|
| 18 |
+
std::error_code ec;
|
| 19 |
+
bool created = std::filesystem::create_directory(PENCIL_DIR, ec);
|
| 20 |
+
if (ec) {
|
| 21 |
+
std::cerr << "Error creating directory " << PENCIL_DIR << ": " << ec.message() << std::endl;
|
| 22 |
+
return false;
|
| 23 |
+
}
|
| 24 |
+
return true; // either created or already existed (create_directory returns false if existed, but no error)
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// Append a line to the session log. Returns true on success.
|
| 28 |
+
inline bool append_to_session(const std::string& text) {
|
| 29 |
+
std::ofstream log(PENCIL_DIR + SESSION_LOG, std::ios::app);
|
| 30 |
+
if (!log) return false;
|
| 31 |
+
log << text << std::endl;
|
| 32 |
+
return !log.fail();
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Read entire file content
|
| 36 |
+
inline std::string read_file(const std::string& path) {
|
| 37 |
+
std::ifstream f(path);
|
| 38 |
+
if (!f) return "";
|
| 39 |
+
std::string content((std::istreambuf_iterator<char>(f)),
|
| 40 |
+
std::istreambuf_iterator<char>());
|
| 41 |
+
return content;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
// Save text to a file (overwrite). Returns true on success.
|
| 45 |
+
inline bool save_text(const std::string& path, const std::string& text) {
|
| 46 |
+
std::ofstream f(path);
|
| 47 |
+
if (!f) return false;
|
| 48 |
+
f << text;
|
| 49 |
+
return !f.fail();
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// Append to the book file. Returns true on success.
|
| 53 |
+
inline bool append_to_book(const std::string& text) {
|
| 54 |
+
std::ofstream book(PENCIL_DIR + BOOK_FILE, std::ios::app);
|
| 55 |
+
if (!book) return false;
|
| 56 |
+
book << text << std::endl;
|
| 57 |
+
return !book.fail();
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Return a prompt template for each ADA command
|
| 61 |
+
inline std::string get_template(const std::string& cmd) {
|
| 62 |
+
static std::map<std::string, std::string> templates = {
|
| 63 |
+
{"/STORY", "Write a creative story with the following title. Use vivid descriptions and a clear narrative."},
|
| 64 |
+
{"/POEM", "Compose a poem about the given subject. Use rhythm and imagery."},
|
| 65 |
+
{"/BOOK", "You are a novelist. Continue the existing book by writing a new chapter. Maintain style and characters."}
|
| 66 |
+
};
|
| 67 |
+
auto it = templates.find(cmd);
|
| 68 |
+
if (it != templates.end()) return it->second;
|
| 69 |
+
return "";
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
#endif // PENCIL_UTILS_HPP
|