aboutsummaryrefslogtreecommitdiff
path: root/lib/arbitrary_command.ml
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/arbitrary_command.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/arbitrary_command.ml b/lib/arbitrary_command.ml
new file mode 100644
index 0000000..920a82f
--- /dev/null
+++ b/lib/arbitrary_command.ml
@@ -0,0 +1,33 @@
+module Grep = Grep
+
+let run ~(on_return : string -> unit) t ~selected_file ~content ~command =
+ let command = String.split_on_char ' ' command in
+ (* Substitute after splitting to more easily deal with file names that have spaces *)
+ let command =
+ command
+ |> List.map (fun s ->
+ let s = Str.global_replace (Str.regexp "%(file)") selected_file s in
+ Str.global_replace (Str.regexp "%(content)") content s)
+ in
+ Common.Term.cursor t (Some (0, 0));
+ try
+ let result =
+ let open Shexp_process in
+ let open Shexp_process.Infix in
+ eval (chdir Grep.execution_directory (call command |- read_all))
+ in
+ Common.Term.cursor t None;
+ on_return result
+ with
+ | exn ->
+ let oc = open_out "/tmp/stitch-error" in
+ let commands_result = String.concat " " command in
+ Printf.fprintf
+ oc
+ "%s\n%s\n%s\n%s\n"
+ selected_file
+ commands_result
+ (Printexc.to_string exn)
+ (Printexc.get_backtrace ());
+ close_out oc;
+ on_return "ERROR: Failed to run command. Error written to /tmp/stitch-error"