Commit 1f8871f

mo khan <mo@mokhan.ca>
2026-01-22 00:26:54
feat: add a default edit tool
1 parent 658608d
Changed files (2)
lib
lib/elelem/tools/edit.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+Elelem::Plugins.register(:edit) do |toolbox|
+  toolbox.add("edit",
+    description: "Replace first occurrence of text in file",
+    params: { path: { type: "string" }, old: { type: "string" }, new: { type: "string" } },
+    required: ["path", "old", "new"]
+  ) do |a|
+    path = Pathname.new(a["path"]).expand_path
+    content = path.read
+    if content.include?(a["old"])
+      path.write(content.sub(a["old"], a["new"]))
+      { path: a["path"], bytes: path.write(content.sub(a["old"], a["new"])) }
+    else
+      { error: "text not found", content: content.lines.first(20).join }
+    end
+  end
+
+  toolbox.after("edit") do |_, result|
+    if result[:error]
+      $stdout.puts "  ! #{result[:error]}"
+    elsif !system("bat", "--paging=never", result[:path])
+      $stdout.puts "  -> #{result[:path]}"
+    end
+  end
+end
lib/elelem/tools/verify.rb
@@ -27,7 +27,7 @@ module Elelem
   end
 
   Plugins.register(:verify) do |toolbox|
-    toolbox.after("write") do |_, result|
+    toolbox.after("write") do |_args, result|
       next if result[:error]
 
       Verifiers.for(result[:path]).each do |cmd|