From 256397061fe5ae5649b4569c45bb53c8e45e0cbe Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Sat, 18 May 2024 13:03:40 -0500 Subject: Add support for arbitrary command in the note view --- lib/arbitrary_command.ml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/arbitrary_command.ml (limited to 'lib/arbitrary_command.ml') 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" -- cgit v1.2.3