-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(console): split Dune_console.Backend (#7000
Move the dumb and progress backends to their own files. Move flushing/composition to a [Combinators] module Finally, [Console.Backend.progress] is now flushing. Previously, it wouldn't flush and was therefore buggy. This didn't affect dune, as we only used [progress_threaded]. Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
19 changed files
with
220 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
stdune | ||
xdg | ||
dune_console | ||
dune_threaded_console | ||
dune_lang | ||
dune_cache | ||
dune_cache_storage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
let flush (module Backend : Backend_intf.S) : Backend_intf.t = | ||
(module struct | ||
include Backend | ||
|
||
let print_if_no_status_line msg = | ||
print_if_no_status_line msg; | ||
flush stderr | ||
|
||
let print_user_message msg = | ||
print_user_message msg; | ||
flush stderr | ||
|
||
let reset () = | ||
reset (); | ||
flush stderr | ||
|
||
let reset_flush_history () = | ||
reset_flush_history (); | ||
flush stderr | ||
end : Backend_intf.S) | ||
|
||
let compose (module A : Backend_intf.S) (module B : Backend_intf.S) : | ||
(module Backend_intf.S) = | ||
(module struct | ||
let start () = | ||
A.start (); | ||
B.start () | ||
|
||
let print_user_message msg = | ||
A.print_user_message msg; | ||
B.print_user_message msg | ||
|
||
let set_status_line x = | ||
A.set_status_line x; | ||
B.set_status_line x | ||
|
||
let finish () = | ||
A.finish (); | ||
B.finish () | ||
|
||
let print_if_no_status_line msg = | ||
A.print_if_no_status_line msg; | ||
B.print_if_no_status_line msg | ||
|
||
let reset () = | ||
A.reset (); | ||
B.reset () | ||
|
||
let reset_flush_history () = | ||
A.reset_flush_history (); | ||
B.reset_flush_history () | ||
end : Backend_intf.S) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(** Add a [flush stderr] after every UI operation *) | ||
val flush : Backend_intf.t -> Backend_intf.t | ||
|
||
(** Creates a backend that writes to both backends in sequence *) | ||
val compose : Backend_intf.t -> Backend_intf.t -> Backend_intf.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
open Stdune | ||
|
||
module No_flush : Backend_intf.S = struct | ||
let start () = () | ||
|
||
let finish () = () | ||
|
||
let print_user_message msg = | ||
Option.iter msg.User_message.loc ~f:(fun loc -> | ||
Loc.render Format.err_formatter (Loc.pp loc)); | ||
User_message.prerr { msg with loc = None } | ||
|
||
let set_status_line _ = () | ||
|
||
let print_if_no_status_line msg = | ||
(* [Pp.cut] seems to be enough to force the terminating newline to | ||
appear. *) | ||
Ansi_color.prerr | ||
(Pp.seq (Pp.map_tags msg ~f:User_message.Print_config.default) Pp.cut) | ||
|
||
let reset () = prerr_string "\x1b[H\x1b[2J" | ||
|
||
let reset_flush_history () = prerr_string "\x1b[1;1H\x1b[2J\x1b[3J" | ||
end | ||
|
||
let flush = Combinators.flush (module No_flush) | ||
|
||
include (val flush) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(** Output to a terminal without any capbilities and without flushing *) | ||
module No_flush : Backend_intf.S | ||
|
||
(** Output to a terminal without any capbilities with flushing *) | ||
include Backend_intf.S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
(library | ||
(name dune_console) | ||
(libraries stdune threads.posix) | ||
(libraries stdune) | ||
(instrumentation | ||
(backend bisect_ppx))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
open Stdune | ||
|
||
module No_flush = struct | ||
let status_line = ref Pp.nop | ||
|
||
let start () = () | ||
|
||
let status_line_len = ref 0 | ||
|
||
let hide_status_line () = | ||
if !status_line_len > 0 then Printf.eprintf "\r%*s\r" !status_line_len "" | ||
|
||
let show_status_line () = | ||
if !status_line_len > 0 then Ansi_color.prerr !status_line | ||
|
||
let set_status_line = function | ||
| None -> | ||
hide_status_line (); | ||
status_line := Pp.nop; | ||
status_line_len := 0 | ||
| Some line -> | ||
let line = Pp.map_tags line ~f:User_message.Print_config.default in | ||
let line_len = String.length (Format.asprintf "%a" Pp.to_fmt line) in | ||
hide_status_line (); | ||
status_line := line; | ||
status_line_len := line_len; | ||
show_status_line () | ||
|
||
let print_if_no_status_line _msg = () | ||
|
||
let print_user_message msg = | ||
hide_status_line (); | ||
Dumb.No_flush.print_user_message msg; | ||
show_status_line () | ||
|
||
let reset () = Dumb.reset () | ||
|
||
let finish () = set_status_line None | ||
|
||
let reset_flush_history () = Dumb.reset_flush_history () | ||
end | ||
|
||
let no_flush = (module No_flush : Backend_intf.S) | ||
|
||
let flush = Combinators.flush no_flush |
Oops, something went wrong.