1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
open! Core
open! Import
module Unix = Core_unix
module Io = Io
module Streaming = struct
type 'a lookup =
| Ident : string lookup
| Cache : 'a String.Table.t -> 'a lookup
type 'a t =
| T :
{ codec : _ Io.t
; inputs : Io.Input.t Async.Pipe.Reader.t
; lookup : 'a lookup
}
-> 'a t
let of_escaped_strings reader =
T
{ codec = Io.of_escaped_string
; inputs =
Async.Pipe.map reader ~f:(fun x ->
match Io.(encode of_escaped_string x) with
| Error _ -> .
| Ok x -> x)
; lookup = Ident
}
;;
let of_strings_raise_on_newlines reader =
T
{ codec = Io.of_human_friendly_string
; inputs =
Async.Pipe.map reader ~f:(fun x ->
match Io.(encode of_human_friendly_string x) with
| Error `String_contains_newline ->
raise_s [%message "string unexpectedly contains newline" ~_:x]
| Ok x -> x)
; lookup = Ident
}
;;
let of_escaped_strings_assoc reader ~on_collision =
let cache = String.Table.create () in
T
{ codec = Io.of_escaped_string
; inputs =
Async.Pipe.filter_map reader ~f:(fun (str, x) ->
match Hashtbl.find cache str with
| None ->
Hashtbl.set cache ~key:str ~data:x;
(match Io.encode Io.of_escaped_string str with
| Error _ -> .
| Ok x -> Some x)
| Some old_item ->
(match on_collision ~old_item ~new_item:x with
| `Raise error -> Error.raise error
| `Ignore -> ()
| `Update -> Hashtbl.set cache ~key:str ~data:x);
None)
; lookup = Cache cache
}
;;
let lookup_selection (type a) (T t : a t) (selection : string) =
match t.lookup with
| Ident -> Some (selection : a)
| Cache cache -> Hashtbl.find cache selection
;;
end
module Pick_from : sig
type _ t =
| Map : 'a String.Map.t -> 'a t
| Assoc : (string * 'a) list -> 'a t
| Inputs : string list -> string t
| Command_output : string -> string t
| Streaming : 'a Streaming.t -> 'a t
val map : 'a String.Map.t -> 'a t
val assoc : (string * 'a) list -> 'a t
val inputs : string list -> string t
val command_output : string -> string t
val streaming : 'a Streaming.t -> 'a t
module Of_stringable : sig
val map : (module Stringable with type t = 't) -> ('t, 'a, _) Map.t -> 'a t
val assoc : (module Stringable with type t = 't) -> ('t, 'a) List.Assoc.t -> 'a t
val inputs : (module Stringable with type t = 't) -> 't List.t -> 't t
end
(** A [Pick_from.Encoded.t] takes care to convert client provided keys into
'fzf-friendly' strings (i.e., not containing any newlines), and maps the
'fzf-friendly' output from Fzf back into client-provided keys.
*)
module Encoded : sig
type 'a unencoded := 'a t
type 'a t
val create : 'a unencoded -> 'a t
val to_list : _ t -> Io.Input.t list
val lookup_selection : 'a t -> Io.Output.t -> 'a
end
end = struct
type _ t =
| Map : 'a String.Map.t -> 'a t
| Assoc : (string * 'a) list -> 'a t
| Inputs : string list -> string t
| Command_output : string -> string t
| Streaming : ('a Streaming.t[@sexp.opaque]) -> 'a t
[@@deriving sexp_of]
let to_list (type a) (t : a t) : string list =
match t with
| Map values -> Map.keys values
| Assoc entries -> List.map ~f:fst entries
| Inputs l -> l
| Command_output (_ : string) -> []
| Streaming (_ : _ Streaming.t) -> []
;;
let inputs x = Inputs x
let map x = Map x
let assoc x = Assoc x
let command_output x = Command_output x
let streaming s = Streaming s
module Of_stringable = struct
let assoc (type t) (module S : Stringable with type t = t) assoc =
let map =
List.map assoc ~f:(fun (key, v) -> S.to_string key, v) |> String.Map.of_alist_exn
in
Map map
;;
let map s map = Map.to_alist map |> assoc s
let inputs s inputs = List.map inputs ~f:(fun s -> s, s) |> assoc s
let%expect_test "assoc" =
let module T = struct
module T = struct
type t =
| Thing
| Amabob
[@@deriving enumerate, sexp]
end
include T
include Sexpable.To_stringable (T)
end
in
print_s [%sexp (assoc (module T) [ Thing, "t"; T.Amabob, "a" ] : string t)];
[%expect {| (Map ((Amabob a) (Thing t))) |}]
;;
end
let lookup_selection (type a) (t : a t) (selection : string) : a =
match t with
| Map map ->
(match Map.find map selection with
| Some x -> x
| None ->
raise_s
[%message
"Fzf bug: String selected that was not a map key"
selection
(map : _ String.Map.t)])
| Assoc alist ->
(match List.Assoc.find ~equal:String.equal alist selection with
| Some x -> x
| None ->
raise_s
[%message
"Fzf bug: string selected was not in selections"
selection
~selections:(alist : (string * _) list)])
| Inputs _ -> (selection : a)
| Command_output (_ : string) -> (selection : a)
| Streaming streaming ->
(match Streaming.lookup_selection streaming selection with
| Some x -> x
| None ->
raise_s
[%message "Fzf bug: String selected was not on the streaming pipe" selection])
;;
module Encoded = struct
type 'a pick_from = 'a t
type 'a t =
{ pick_from : 'a pick_from
; encoded : Io.Input.t list
; decode_exn : Io.Output.t -> string
}
let decode_exn codec selection ~message =
match Io.decode codec selection with
| Ok decoded -> decoded
| Error (`Decoded_with_inconsistent_codec err) ->
raise_s [%message message (selection : Io.Output.t) (err : Error.t)]
;;
let create (type a) (pick_from : a pick_from) : a t =
let unencoded = to_list pick_from in
let encode_all (type err) (codec : err Io.t) : (a t, err) Result.t =
let encode_results = List.map unencoded ~f:(Io.encode codec) in
match List.partition_result encode_results with
| encoded, [] ->
let decode_exn =
decode_exn
codec
~message:"Fzf bug: string selected that was not in expected format"
in
Ok { pick_from; encoded; decode_exn }
| (_ : Io.Input.t list), err :: (_ : err list) -> Error err
in
match pick_from with
| Streaming (T { codec; _ }) ->
{ pick_from
; encoded = []
; decode_exn =
decode_exn
codec
~message:"string selected that was not encoded with the right Fzf.Io format"
}
| _ ->
(match encode_all Io.of_human_friendly_string with
| Ok t -> t
| Error `String_contains_newline ->
(match encode_all Io.of_escaped_string with
| Ok t -> t
| Error _ -> .))
;;
let lookup_selection (type a) (t : a t) (selection : Io.Output.t) : a =
let selection = t.decode_exn selection in
lookup_selection t.pick_from selection
;;
let to_list t = t.encoded
end
end
module Tiebreak = struct
module T = struct
type t =
| Length
| Begin
| End
| Index
[@@deriving compare, enumerate, equal, sexp]
end
include T
include Sexpable.To_stringable (T)
let to_string = Fn.compose String.lowercase to_string
let%test_unit "roundtrip" =
List.iter all ~f:(fun t -> [%test_result: t] ~expect:t (of_string (to_string t)))
;;
let%expect_test "demonstrate to_string" =
List.iter all ~f:(fun t -> print_endline (to_string t));
[%expect {|
length
begin
end
index
|}]
;;
end
module Expect = struct
type t =
{ expect_keys : string Nonempty_list.t
; key_pressed : string Set_once.t
}
let create expect_keys = { expect_keys; key_pressed = Set_once.create () }
end
type ('a, 'return) pick_fun =
?fzf_path:string
-> ?select1:unit
-> ?query:string
-> ?header:string
-> ?preview:string
-> ?preview_window:string
-> ?no_sort:unit
-> ?reverse_input:unit
-> ?prompt_at_top:unit
-> ?with_nth:string
-> ?nth:string
-> ?delimiter:string
-> ?height:int
-> ?bind:string Nonempty_list.t
-> ?tiebreak:Tiebreak.t Nonempty_list.t
-> ?filter:string
-> ?border:[ `rounded | `sharp | `horizontal ]
-> ?info:[ `default | `inline | `hidden ]
-> ?exact_match:unit
-> ?no_hscroll:unit
-> ?case_match:[ `case_sensitive | `case_insensitive | `smart_case ]
-> ?expect:Expect.t
-> 'a Pick_from.t
-> 'return
module Blocking = struct
let default_fzf_prog = "fzf"
let really_write_with_newline fd str =
let str = str ^ "\n" in
let rec loop pos =
let pos = pos + Unix.single_write_substring fd ~pos ~buf:str in
if String.length str > pos then loop pos
in
loop 0
;;
let make_command_option ~key value = sprintf "--%s=%s" key value
let shuttle_pipe_strings_to_fd_then_close pipe stdin_wr =
let open Async in
let shuttle_strings =
let%map () =
Async.Pipe.iter pipe ~f:(fun (str : Io.Input.t) ->
match really_write_with_newline stdin_wr (str :> string) with
| exception Core_unix.Unix_error _ -> Deferred.unit
| () -> Deferred.unit)
in
Core_unix.close stdin_wr
in
don't_wait_for shuttle_strings
;;
let pick
?(fzf_path = default_fzf_prog)
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?select_many
?no_hscroll
?(case_match = `smart_case)
(entries :
[ `Streaming of Io.Input.t Async.Pipe.Reader.t
| `List of Io.Input.t Nonempty_list.t
| `Command_output of string
])
?(expect : Expect.t option)
~buffer_size
~on_result
~pid_ivar
=
let stdout_rd, stdout_wr = Unix.pipe () in
let stdin_rd, stdin_wr = Unix.pipe () in
match Unix.fork () with
| `In_the_child ->
Unix.dup2 ~dst:Unix.stdin ~src:stdin_rd ();
Unix.dup2 ~dst:Unix.stdout ~src:stdout_wr ();
Unix.close stdin_wr;
let args =
([ Option.map query ~f:(make_command_option ~key:"query")
; Option.map header ~f:(make_command_option ~key:"header")
; Option.map select1 ~f:(Fn.const "--select-1")
; Option.map preview ~f:(make_command_option ~key:"preview")
; Option.map preview_window ~f:(make_command_option ~key:"preview-window")
; Option.map no_sort ~f:(Fn.const "--no-sort")
; Option.map reverse_input ~f:(Fn.const "--tac")
; Option.map prompt_at_top ~f:(Fn.const "--reverse")
; Option.map with_nth ~f:(make_command_option ~key:"with-nth")
; Option.map nth ~f:(make_command_option ~key:"nth")
; Option.map delimiter ~f:(make_command_option ~key:"delimiter")
; Option.map height ~f:(fun h ->
make_command_option ~key:"height" (Int.to_string h))
; Option.map filter ~f:(make_command_option ~key:"filter")
; Option.map border ~f:(fun x ->
[%sexp_of: [ `rounded | `sharp | `horizontal ]] x
|> Sexp.to_string
|> make_command_option ~key:"border")
; Option.map info ~f:(fun x ->
[%sexp_of: [ `default | `inline | `hidden ]] x
|> Sexp.to_string
|> make_command_option ~key:"info")
; Option.map exact_match ~f:(Fn.const "--exact")
; Option.map select_many ~f:(Fn.const "-m")
; Option.map
select_many
~f:(Fn.const (make_command_option ~key:"bind" "ctrl-a:toggle-all"))
; Option.map tiebreak ~f:(fun tiebreaks ->
let value =
Nonempty_list.to_list tiebreaks
|> List.map ~f:Tiebreak.to_string
|> String.concat ~sep:","
in
make_command_option ~key:"tiebreak" value)
; Option.map no_hscroll ~f:(Fn.const "--no-hscroll")
; (match case_match with
| `case_insensitive -> Some "-i"
| `case_sensitive -> Some "+i"
| `smart_case -> None)
; (match entries with
| `List (_ : Io.Input.t Nonempty_list.t) -> None
| `Streaming (_ : Io.Input.t Async.Pipe.Reader.t) -> None
| `Command_output command ->
Some (make_command_option ~key:"bind" [%string "change:reload:%{command}"]))
; Option.map expect ~f:(fun expect ->
make_command_option
~key:"expect"
(Nonempty_list.to_list expect.expect_keys |> String.concat ~sep:","))
]
|> List.filter_opt)
@ Option.value_map bind ~default:[] ~f:(fun bindings ->
Nonempty_list.to_list bindings
|> List.map ~f:(fun binding -> make_command_option ~key:"bind" binding))
in
Exn.handle_uncaught ~exit:false (fun () ->
never_returns (Unix.exec ~prog:fzf_path ~argv:(fzf_path :: args) ()));
Unix.exit_immediately 127
| `In_the_parent pid ->
Unix.close stdin_rd;
Unix.close stdout_wr;
(match entries with
| `List entries ->
let entries = (entries :> string Nonempty_list.t) in
Nonempty_list.iter entries ~f:(really_write_with_newline stdin_wr);
Unix.close stdin_wr
| `Streaming pipe -> shuttle_pipe_strings_to_fd_then_close pipe stdin_wr
| `Command_output (_ : string) -> Unix.close stdin_wr);
Option.iter pid_ivar ~f:(fun ivar -> Async.Ivar.fill_exn ivar pid);
let output_rev = ref Reversed_list.[] in
let buf = Bytes.create buffer_size in
let rec read () =
let count = Unix.read ~restart:true stdout_rd ~buf in
if count = 0
then ()
else (
output_rev := Bytes.To_string.sub buf ~pos:0 ~len:count :: !output_rev;
read ())
in
read ();
let output = Reversed_list.rev !output_rev |> String.concat in
let exit_status = Unix.waitpid pid in
Unix.close stdout_rd;
(match exit_status with
| Ok () | Error (`Exit_non_zero (1 | 130)) ->
()
| Error failure_exit_status ->
raise_s
[%message
"fzf terminated with failure exit status"
~_:(failure_exit_status : Unix.Exit_or_signal.error)]);
on_result output
;;
let entries_and_buffer_size (type a) (pick_from : a Pick_from.t) ~buffer_size =
let large_enough_for_a_reasonable_string_selectable_by_a_human =
Byte_units.(bytes_int_exn (of_kilobytes 16.))
in
match pick_from with
| Command_output command ->
Some
( `Command_output command
, large_enough_for_a_reasonable_string_selectable_by_a_human )
| Streaming (T { inputs; _ }) ->
Some (`Streaming inputs, large_enough_for_a_reasonable_string_selectable_by_a_human)
| pick_from ->
let pick_from = Pick_from.Encoded.create pick_from in
let%map.Option entries =
Nonempty_list.of_list (Pick_from.Encoded.to_list pick_from)
in
`List entries, buffer_size entries
;;
let get_max_key_pressed_size expect =
match expect with
| None -> 0
| Some (expect : Expect.t) ->
Nonempty_list.map expect.expect_keys ~f:String.length
|> Nonempty_list.reduce ~f:Int.max
|> succ
;;
let expect output =
match expect with
| None -> output
| Some (expect : Expect.t) ->
let lines = String.split_lines output in
(match lines with
| [] -> raise_s [%message "fzf bug: got empty output"]
| [ _ ] ->
raise_s [%message "fzf bug: only got one line of output" (output : string)]
| key_pressed :: selections ->
(match Set_once.set expect.key_pressed [%here] key_pressed with
| Error e -> raise_s [%message "BUG: already set key_pressed" (e : Error.t)]
| Ok () -> String.concat ~sep:"\n" selections))
;;
let pick_one_with_pid_ivar
(type a)
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
(pick_from : a Pick_from.t)
: a option
=
let result =
let%bind.Option entries, buffer_size =
entries_and_buffer_size pick_from ~buffer_size:(fun entries ->
Nonempty_list.map (entries :> string Nonempty_list.t) ~f:String.length
|> Nonempty_list.reduce ~f:Int.max
|> succ
|> Int.( + ) (get_max_key_pressed_size expect))
in
let on_result output =
if String.length output = 0
then None
else
String.subo output ~len:(String.length output - 1)
|> extract_key_pressed expect
|> Io.Output.of_string
|> Some
in
pick
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
entries
~buffer_size
~pid_ivar
~on_result
in
let pick_from = Pick_from.Encoded.create pick_from in
Option.map result ~f:(fun selection ->
Pick_from.Encoded.lookup_selection pick_from selection)
;;
let pick_one = pick_one_with_pid_ivar ~pid_ivar:None
let pick_many_with_pid_ivar
(type a)
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
(pick_from : a Pick_from.t)
: a list option
=
let result =
let%bind.Option entries, buffer_size =
entries_and_buffer_size pick_from ~buffer_size:(fun entries ->
let each_entry_has_a_trailing_newline = 1 in
(entries :> string Nonempty_list.t)
|> Nonempty_list.map ~f:(fun entry ->
String.length entry + each_entry_has_a_trailing_newline)
|> Nonempty_list.reduce ~f:Int.( + )
|> ( + ) (get_max_key_pressed_size expect))
in
let on_result output =
if String.length output = 0
then None
else
String.subo output ~len:(String.length output - 1)
|> extract_key_pressed expect
|> String.split ~on:'\n'
|> List.map ~f:Io.Output.of_string
|> Option.some
in
pick
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
entries
~buffer_size
~select_many:()
~on_result
~pid_ivar
in
let pick_from = Pick_from.Encoded.create pick_from in
Option.map result ~f:(fun keys ->
List.map keys ~f:(fun key -> Pick_from.Encoded.lookup_selection pick_from key))
;;
let pick_many = pick_many_with_pid_ivar ~pid_ivar:None
end
open Async
let with_abort ~abort ~f =
let pid_ivar = Ivar.create () in
let abort_deferred =
let%map (), pid = Deferred.both abort (Ivar.read pid_ivar) in
pid
in
let abort_choice =
choice abort_deferred (fun pid ->
Signal_unix.send_i Signal.int (`Pid pid);
Or_error.return (Second `Aborted))
in
let fzf_deferred =
let%map.Deferred.Or_error x = f ~pid_ivar:(Some pid_ivar) in
First x
in
let fzf_choice = choice fzf_deferred Fn.id in
let%bind result = choose [ abort_choice; fzf_choice ] in
let%bind.Deferred (_ : _ Either.t Or_error.t) = fzf_deferred in
return result
;;
let pick_one_with_pid_ivar
(type a)
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
(entries : a Pick_from.t)
: a option Deferred.Or_error.t
=
Deferred.Or_error.try_with
~run:`Schedule
~rest:`Log
(fun () ->
In_thread.run (fun () ->
Blocking.pick_one_with_pid_ivar
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
entries))
;;
let pick_one = pick_one_with_pid_ivar ~pid_ivar:None
let pick_one_abort
(type a)
~abort
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
(entries : a Pick_from.t)
: (a option, [ `Aborted ]) Either.t Deferred.Or_error.t
=
with_abort ~abort ~f:(fun ~pid_ivar ->
pick_one_with_pid_ivar
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
entries)
;;
let pick_many_with_pid_ivar
(type a)
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
(entries : a Pick_from.t)
: a list option Deferred.Or_error.t
=
Deferred.Or_error.try_with
~run:`Schedule
~rest:`Log
(fun () ->
In_thread.run (fun () ->
Blocking.pick_many_with_pid_ivar
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
entries))
;;
let pick_many = pick_many_with_pid_ivar ~pid_ivar:None
let pick_many_abort
(type a)
~abort
?fzf_path
?select1
?query
?
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
(entries : a Pick_from.t)
: (a list option, [ `Aborted ]) Either.t Deferred.Or_error.t
=
with_abort ~abort ~f:(fun ~pid_ivar ->
pick_many_with_pid_ivar
?fzf_path
?select1
?query
?header
?preview
?preview_window
?no_sort
?reverse_input
?prompt_at_top
?with_nth
?nth
?delimiter
?height
?bind
?tiebreak
?filter
?border
?info
?exact_match
?no_hscroll
?case_match
?expect
~pid_ivar
entries)
;;
let complete_subcommands ~show_help ~path ~part subcommands =
let preview =
if show_help
then (
let exe = Core_unix.readlink "/proc/self/exe" in
let name, args =
match path with
| name :: args -> name, String.concat ~sep:" " args
| [] -> failwith "complete_subcommands: Unexpected empty list for path"
in
let command_prefix = [%string "exec -a %{name} %{exe} %{args}"] in
Some (sprintf "eval '%s '{}' -help'" command_prefix))
else None
in
let prompt_at_top =
Option.some_if (Option.is_some preview) ()
in
Blocking.pick_one
(Inputs (List.map ~f:(String.concat ~sep:" ") subcommands))
~query:part
?preview
?prompt_at_top
|> Option.map ~f:List.return
;;
let complete ~choices (univ_map : Univ_map.t) ~part =
Blocking.pick_one ~query:part (Inputs (choices univ_map)) |> Option.to_list
;;
let complete_enumerable (module E : Command.Enumerable_stringable) =
let choices (_ : Univ_map.t) = List.map E.all ~f:E.to_string in
complete ~choices
;;
let complete_enumerable_sexpable (module E : Command.Enumerable_sexpable) =
let choices (_ : Univ_map.t) =
List.map E.all ~f:(fun t -> Sexp.to_string [%sexp (t : E.t)])
in
complete ~choices
;;
let key_with_hidden_part visible ~hidden =
let whitespace = String.make 500 ' ' in
visible ^ whitespace ^ hidden
|> String.substr_replace_all ~pattern:"\r\n" ~with_:" "
|> String.substr_replace_all ~pattern:"\n" ~with_:" "
;;