diff options
author | Marc Coquand <marc@mccd.space> | 2024-05-18 13:03:40 -0500 |
---|---|---|
committer | Marc Coquand <marc@mccd.space> | 2024-05-18 13:03:40 -0500 |
commit | 256397061fe5ae5649b4569c45bb53c8e45e0cbe (patch) | |
tree | 652e7b5833ea32d13b0ae1ea9cc32ba421fed827 /lib/arbitrary_command.ml | |
parent | 12ba5ed258b3a1ff30c3ece030e8eeed0dc417e9 (diff) | |
download | stitch-256397061fe5ae5649b4569c45bb53c8e45e0cbe.tar.gz stitch-256397061fe5ae5649b4569c45bb53c8e45e0cbe.tar.bz2 stitch-256397061fe5ae5649b4569c45bb53c8e45e0cbe.zip |
Add support for arbitrary command in the note view
Diffstat (limited to 'lib/arbitrary_command.ml')
-rw-r--r-- | lib/arbitrary_command.ml | 33 |
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" |