Expose document history through pano cli

This commit is contained in:
2026-06-02 09:52:25 +02:00
parent b82cc1e4bd
commit a6aa31da79
4 changed files with 161 additions and 2 deletions

View File

@@ -53,6 +53,12 @@ struct SimulateStrokeScriptArgs {
std::string path;
};
struct SimulateDocumentHistoryArgs {
std::uint32_t width = 64;
std::uint32_t height = 32;
std::uint32_t history_entries = 4;
};
struct RecordRenderArgs {
std::uint32_t width = 64;
std::uint32_t height = 32;
@@ -116,6 +122,7 @@ void print_help()
<< " load-project --path FILE\n"
<< " parse-layout --path FILE\n"
<< " record-render [--width N] [--height N]\n"
<< " simulate-document-history [--width N] [--height N] [--history N]\n"
<< " simulate-stroke --x1 N --y1 N --x2 N --y2 N [--spacing N]\n"
<< " simulate-stroke-script --path FILE\n"
<< " --help\n";
@@ -631,6 +638,135 @@ int simulate_stroke_script(int argc, char** argv)
return 0;
}
pp::foundation::Status parse_simulate_document_history_args(
int argc,
char** argv,
SimulateDocumentHistoryArgs& args)
{
for (int i = 2; i < argc; ++i) {
const std::string_view key(argv[i]);
if (key == "--width" || key == "--height" || key == "--history") {
if (i + 1 >= argc) {
return pp::foundation::Status::invalid_argument("missing value for option");
}
const auto value = pp::foundation::parse_u32(argv[++i]);
if (!value) {
return value.status();
}
if (key == "--width") {
args.width = value.value();
} else if (key == "--height") {
args.height = value.value();
} else {
args.history_entries = value.value();
}
} else {
return pp::foundation::Status::invalid_argument("unknown option");
}
}
if (args.width == 0 || args.height == 0) {
return pp::foundation::Status::invalid_argument("width and height must be greater than zero");
}
return pp::foundation::Status::success();
}
int simulate_document_history(int argc, char** argv)
{
SimulateDocumentHistoryArgs args;
const auto status = parse_simulate_document_history_args(argc, argv, args);
if (!status.ok()) {
print_error("simulate-document-history", status.message);
return 2;
}
const auto document_result = pp::document::CanvasDocument::create(
pp::document::DocumentConfig {
.width = args.width,
.height = args.height,
.layer_count = 1,
});
if (!document_result) {
print_error("simulate-document-history", document_result.status().message);
return 2;
}
auto history_result = pp::document::DocumentHistory::create(
document_result.value(),
static_cast<std::size_t>(args.history_entries));
if (!history_result) {
print_error("simulate-document-history", history_result.status().message);
return 2;
}
auto history = history_result.value();
auto with_layer = history.current();
const auto layer_result = with_layer.add_layer("Paint");
if (!layer_result) {
print_error("simulate-document-history", layer_result.status().message);
return 2;
}
auto apply_status = history.apply(with_layer);
if (!apply_status.ok()) {
print_error("simulate-document-history", apply_status.message);
return 2;
}
auto with_frame = history.current();
const auto frame_result = with_frame.add_frame(250);
if (!frame_result) {
print_error("simulate-document-history", frame_result.status().message);
return 2;
}
apply_status = history.apply(with_frame);
if (!apply_status.ok()) {
print_error("simulate-document-history", apply_status.message);
return 2;
}
const auto undo_status = history.undo();
if (!undo_status.ok()) {
print_error("simulate-document-history", undo_status.message);
return 2;
}
const auto undo_layers = history.current().layers().size();
const auto undo_frames = history.current().frames().size();
const auto undo_can_redo = history.can_redo();
const auto undo_index = history.current_index();
const auto redo_status = history.redo();
if (!redo_status.ok()) {
print_error("simulate-document-history", redo_status.message);
return 2;
}
const auto& current = history.current();
std::cout << "{\"ok\":true,\"command\":\"simulate-document-history\""
<< ",\"history\":{\"size\":" << history.size()
<< ",\"currentIndex\":" << history.current_index()
<< ",\"canUndo\":" << (history.can_undo() ? "true" : "false")
<< ",\"canRedo\":" << (history.can_redo() ? "true" : "false")
<< "},\"undo\":{\"layers\":" << undo_layers
<< ",\"frames\":" << undo_frames
<< ",\"currentIndex\":" << undo_index
<< ",\"canRedo\":" << (undo_can_redo ? "true" : "false")
<< "},\"current\":{\"width\":" << current.width()
<< ",\"height\":" << current.height()
<< ",\"layers\":" << current.layers().size()
<< ",\"frames\":" << current.frames().size()
<< ",\"activeLayer\":" << current.active_layer_index()
<< ",\"activeFrame\":" << current.active_frame_index()
<< ",\"animationDurationMs\":" << current.animation_duration_ms()
<< "}}\n";
return 0;
}
pp::foundation::Status parse_record_render_args(int argc, char** argv, RecordRenderArgs& args)
{
for (int i = 2; i < argc; ++i) {
@@ -839,6 +975,10 @@ int main(int argc, char** argv)
return simulate_stroke_script(argc, argv);
}
if (command == "simulate-document-history") {
return simulate_document_history(argc, argv);
}
if (command == "parse-layout") {
return parse_layout(argc, argv);
}