Commit 07311e9

mo khan <mo@mokhan.ca>
2026-01-22 00:26:54
feat: add a default edit tool
1 parent 5304430
Changed files (4)
lib/elelem/builtins/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/builtins/execute.rb
@@ -5,7 +5,7 @@ Elelem::Plugins.register(:execute) do |toolbox|
     description: "Run shell command (supports pipes and redirections)",
     params: { command: { type: "string" } },
     required: ["command"],
-    aliases: ["bash", "sh", "exec"]
+    aliases: ["bash", "sh", "exec", "execute<|channel|>"]
   ) do |a|
     Elelem.sh("bash", args: ["-c", a["command"]]) { |x| $stdout.print(x) }
   end
lib/elelem/builtins/read.rb
@@ -14,7 +14,7 @@ Elelem::Plugins.register(:read) do |toolbox|
   toolbox.after("read") do |_, result|
     if result[:error]
       $stdout.puts "  ! #{result[:error]}"
-    elsif !system("bat", "--style=plain", "--paging=never", result[:path])
+    elsif !system("bat", "--paging=never", result[:path])
       $stdout.puts result[:content]
     end
   end
lib/elelem/builtins/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|