Improve suffix handling for commands.

This commit is contained in:
2026-08-23 13:16:33 +01:00
parent 6f1c87400a
commit c8a270378a
4 changed files with 11 additions and 11 deletions
+4
View File
@@ -16,9 +16,13 @@ struct Buffer {
uint64_t line = 0; uint64_t line = 0;
bool modified; bool modified;
std::filesystem::path save_path = ""; std::filesystem::path save_path = "";
struct { struct {
uint64_t start{0}; uint64_t start{0};
uint64_t end{0}; uint64_t end{0};
std::span<const uint64_t> values() const {
return {&start, 2};
}
} prev_range; } prev_range;
Buffer(); Buffer();
+1 -1
View File
@@ -6,7 +6,7 @@
namespace bed::internal::commands { namespace bed::internal::commands {
struct Suffix { struct Suffix {
std::string desc; std::string desc;
void (*handle)(BEd &); void (*handle)(BEd &, std::span<const uint64_t>);
static void register_suffixes(BEd &ctx); static void register_suffixes(BEd &ctx);
}; };
+2 -2
View File
@@ -90,12 +90,12 @@ void BEd::handle(std::string_view cmd_, bool eof) {
break; break;
} }
if (suffix) if (suffix)
suffix->handle(*this); suffix->handle(*this, active->prev_range.values());
return; return;
} }
void BEd::suffix_handle(char s) { void BEd::suffix_handle(char s) {
if (suffixes[s - 'a'].has_value()) if (suffixes[s - 'a'].has_value())
suffixes[s - 'a'].value().handle(*this); suffixes[s - 'a'].value().handle(*this, active->prev_range.values());
} }
} // namespace bed } // namespace bed
+4 -8
View File
@@ -6,18 +6,14 @@ namespace bed::internal::commands {
void Suffix::register_suffixes(BEd &ctx) { void Suffix::register_suffixes(BEd &ctx) {
ctx.suffixes['p' - 'a'] = Suffix{ ctx.suffixes['p' - 'a'] = Suffix{
.desc = "Prints current line.", .desc = "Prints current line.",
.handle = [](BEd &ctx) { .handle = [](BEd &ctx, std::span<const uint64_t> addresses) {
auto line = ctx.active->line; ctx.active->print(ctx, addresses[0], addresses[1]);
ctx.active->print(ctx, line, line);
ctx.active->jump(line);
} }
}; };
ctx.suffixes['n' - 'a'] = Suffix{ ctx.suffixes['n' - 'a'] = Suffix{
.desc = "Prints current line with line number.", .desc = "Prints current line with line number.",
.handle = [](BEd &ctx) { .handle = [](BEd &ctx, std::span<const uint64_t> addresses) {
auto line = ctx.active->line; ctx.active->number_print(ctx, addresses[0], addresses[1]);
ctx.active->number_print(ctx, line, line);
ctx.active->jump(line);
} }
}; };
} }