Parcourir la source

Make --el argument of list command working

 + Complete 5fc8bce1d024677d362a76ecb35b8b9c5313d8a3.
 + Add a test.
 + Update comments.
 + Fix condition and string cuts, as well as some variable reuse.
Leo il y a 9 ans
Parent
commit
b620d39c4e
2 fichiers modifiés avec 23 ajouts et 18 suppressions
  1. 21 18
      src/list_rc.ml
  2. 2 0
      src/test/listrc_t.ml

+ 21 - 18
src/list_rc.ml

@@ -38,31 +38,34 @@ open Core.Std;;
 
 (* This modules contains function to list the content of the rc file *)
 
-(* Characters to add to show a command was truncated *)
+(* Characters to append to show a command was truncated *)
 let trunc_indicator = "...";;
 
-(* Truncate to elength, and add … after entries longer than elength or let them going
- * through unmodified. Special case when elength < length(trunc_indicator),
- * nothing is done *)
+(* Truncate to elength, and add [trunc_indicator] after entries longer than
+ * elength or let them going through unmodified. *)
+(* Special case when elength < length(trunc_indicator), nothing is done *)
 let truncate ?elength str =
-  (* Characters appended to show we have truncated *)
   let trunc_ind_l = String.length trunc_indicator in
   let elength =
-    (* TODO Set it in Const *)
+    (* TODO Set 80 in Const *)
     Option.value ~default:80 elength
-    |> fun l -> l - trunc_ind_l
   in
-  (* We add a character, so we truncate on the length +1, to avoid making
-   * short enough entry appearing to be truncated *)
-  (* For instance, with elength=4
-   * task -> task, tas -> tas and task1 -> task...*)
-   if (elength <= trunc_ind_l) || (String.length str) > (elength + trunc_ind_l)
-  (* Keep only elengthth chars (String.prefix is inclusive but incompatible with
-   * 0 to keep whole string) *)
-   then String.prefix str elength
-    |> (* To show we truncate, using '...' instead of '…' to limit display
-    problems *) fun short_entry ->
-        String.concat [ short_entry ; "..." ]
+
+  (* Cache it, to debug and for the condition later *)
+  let str_length = String.length str in
+  sprintf "Length of the command: %i" str_length |> Messages.debug;
+  sprintf "Elength: %i" elength |> Messages.debug;
+
+  (* We test separately, before truncating, that:
+   * - elength is not <= to the length of the indicator, otherwise the command
+   * should pass untouched
+   * - the command is longer than elength *)
+   if not(elength <= trunc_ind_l) && str_length > elength
+  (* String.prefix is inclusive but incompatible with
+   * 0 to keep whole string. Truncate to elength - trunc_ind_l since we add the
+   * trunc_indicator (we need to cut a bit more) *)
+   then String.prefix str (elength - trunc_ind_l) |> fun short_entry ->
+        String.concat [ short_entry ; trunc_indicator ]
    else str
 ;;
 

+ 2 - 0
src/test/listrc_t.ml

@@ -57,6 +57,8 @@ let data = [
 
   ( ("cmd1", 0, "cmd1"), "Keep as-is" );
   ( ("cmd1", -5, "cmd1"), "Negative figure, keep as-is" );
+  ( ("cmd1", (String.length List_rc.trunc_indicator), "cmd1"), "On the \
+  indicator, keep as-is" );
   ( ("cmd1", (String.length List_rc.trunc_indicator) - 1, "cmd1"), "Under \
   indicator, keep as-is" );
 ];;