Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert --display tui #6780

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ Unreleased
PPX-rewriters can be taken into account by providing the
`--with-pps` flag. (#6727, fixes #6486, @esope)

- Print missing newline after `$ dune exec`. (#6654, fixes #6700, @rgrinberg,
@Alizter)

- Fix binary corruption when installing or promoting in parallel (#6669, fixes
#6668, @edwintorok)

Expand Down
2 changes: 1 addition & 1 deletion bench/bench.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let () =
let module Scheduler = Dune_engine.Scheduler in
let config =
{ Scheduler.Config.concurrency = 10
; display = Simple { verbosity = Quiet; status_line = false }
; display = { verbosity = Quiet; status_line = false }
; stats = None
; insignificant_changes = `React
; signal_watcher = `No
Expand Down
2 changes: 1 addition & 1 deletion bench/micro/dune_bench/scheduler_bench.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Caml = Stdlib

let config =
{ Scheduler.Config.concurrency = 1
; display = Simple { verbosity = Short; status_line = false }
; display = { verbosity = Short; status_line = false }
; stats = None
; insignificant_changes = `React
; signal_watcher = `No
Expand Down
6 changes: 3 additions & 3 deletions bin/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,18 @@ module Options_implied_by_dash_p = struct
end

let display_term =
let module Display = Dune_engine.Display in
one_of
(let+ verbose =
Arg.(
value & flag
& info [ "verbose" ] ~docs:copts_sect
~doc:"Same as $(b,--display verbose)")
in
Option.some_if verbose
(Dune_engine.Display.Simple { verbosity = Verbose; status_line = true }))
Option.some_if verbose { Display.verbosity = Verbose; status_line = true })
Arg.(
value
& opt (some (enum Dune_engine.Display.all)) None
& opt (some (enum Display.all)) None
& info [ "display" ] ~docs:copts_sect ~docv:"MODE"
~doc:
{|Control the display mode of Dune.
Expand Down
2 changes: 1 addition & 1 deletion bin/subst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ let term =
and+ debug_backtraces = Common.debug_backtraces in
let config : Dune_config.t =
{ Dune_config.default with
display = Simple { verbosity = Quiet; status_line = false }
display = { verbosity = Quiet; status_line = false }
; concurrency = Fixed 1
}
in
Expand Down
6 changes: 2 additions & 4 deletions bin/target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ let resolve_targets root (config : Dune_config.t)
let+ targets =
Action_builder.List.map user_targets ~f:(resolve_target root ~setup)
in
(match config.display with
| Simple { verbosity = Verbose; _ } ->
if config.display.verbosity = Verbose then
Log.info
[ Pp.text "Actual targets:"
; Pp.enumerate
Expand All @@ -172,8 +171,7 @@ let resolve_targets root (config : Dune_config.t)
~f:(function
| File p -> Pp.verbatim (Path.to_string_maybe_quoted p)
| Alias a -> Alias.pp a)
]
| _ -> ());
];
targets

let resolve_targets_exn root config setup user_targets =
Expand Down
3 changes: 0 additions & 3 deletions boot/libs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ let local_libraries =
; ("vendor/incremental-cycles/src", Some "Incremental_cycles", false, None)
; ("src/dag", Some "Dag", false, None)
; ("otherlibs/fiber", Some "Fiber", false, None)
; ("vendor/uutf", None, false, None)
; ("vendor/notty/src", None, true, None)
; ("vendor/notty/src-unix", None, true, None)
; ("src/dune_console", Some "Dune_console", false, None)
; ("src/memo", Some "Memo", false, None)
; ("src/dune_metrics", Some "Dune_metrics", false, None)
Expand Down
17 changes: 7 additions & 10 deletions src/dune_config/dune_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ let hash = Poly.hash
let equal a b = Poly.equal a b

let default =
{ display = Simple { verbosity = Quiet; status_line = not Config.inside_dune }
{ display = { verbosity = Quiet; status_line = not Config.inside_dune }
; concurrency = (if Config.inside_dune then Fixed 1 else Auto)
; terminal_persistence = Clear_on_rebuild
; sandboxing_preference = []
Expand Down Expand Up @@ -370,11 +370,11 @@ let adapt_display config ~output_is_a_tty =
(* Progress isn't meaningful if inside a terminal (or emacs), so disable it if
the output is getting piped to a file or something. *)
let config =
match config.display with
| Simple { verbosity; _ }
when (not output_is_a_tty) && not Config.inside_emacs ->
{ config with display = Simple { verbosity; status_line = false } }
| _ -> config
if
config.display.status_line && (not output_is_a_tty)
&& not Config.inside_emacs
then { config with display = { config.display with status_line = false } }
else config
in
(* Similarly, terminal clearing is meaningless if stderr doesn't support ANSI
codes, so revert-back to Preserve in that case *)
Expand All @@ -384,10 +384,7 @@ let adapt_display config ~output_is_a_tty =

let init t =
Console.Backend.set (Display.console_backend t.display);
Log.verbose :=
match t.display with
| Simple { verbosity = Verbose; _ } -> true
| _ -> false
Log.verbose := t.display.verbosity = Verbose

let auto_concurrency =
lazy
Expand Down
2 changes: 1 addition & 1 deletion src/dune_console/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name dune_console)
(libraries stdune dune_notty dune_notty_unix threads.posix)
(libraries stdune threads.posix)
(instrumentation
(backend bisect_ppx)))
165 changes: 24 additions & 141 deletions src/dune_console/dune_console.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ open Stdune

module Backend = struct
module type S = sig
val start : unit -> unit

val print_user_message : User_message.t -> unit

val set_status_line : User_message.Style.t Pp.t option -> unit
Expand All @@ -20,8 +18,6 @@ module Backend = struct
type t = (module S)

module Dumb_no_flush : S = struct
let start () = ()

let finish () = ()

let print_user_message msg =
Expand Down Expand Up @@ -65,7 +61,7 @@ module Backend = struct
module Progress_no_flush : S = struct
let status_line = ref Pp.nop

let start () = ()
let finish () = ()

let status_line_len = ref 0

Expand Down Expand Up @@ -97,64 +93,9 @@ module Backend = struct

let reset () = Dumb.reset ()

let finish () = set_status_line None

let reset_flush_history () = Dumb.reset_flush_history ()
end

type state =
{ messages : User_message.t Queue.t
; mutable finish_requested : bool
; mutable finished : bool
; mutable status_line : User_message.Style.t Pp.t option
}

module Tui () = struct
module Term = Notty_unix.Term

let term = Term.create ~nosig:false ()

let start () = Unix.set_nonblock Unix.stdin

let image ~status_line ~messages =
let status =
match (status_line : User_message.Style.t Pp.t option) with
| None -> []
| Some message ->
[ Notty_console.image_of_user_message_style_pp message ]
in
let messages =
List.map messages ~f:(fun msg ->
Notty_console.image_of_user_message_style_pp (User_message.pp msg))
in
Notty.I.vcat (messages @ status)

let render state =
let messages = Queue.to_list state.messages in
let image = image ~status_line:state.status_line ~messages in
Term.image term image

let handle_user_events ~now ~timeout mutex =
let input_fds, _, _ = Unix.select [ Unix.stdin ] [] [] timeout in
match input_fds with
| [] -> now +. timeout
| _ :: _ ->
Mutex.lock mutex;
(try
match Term.event term with
| `Key (`ASCII 'q', _) -> Unix.kill (Unix.getpid ()) Sys.sigterm
| _ -> ()
with Unix.Unix_error ((EAGAIN | EWOULDBLOCK), _, _) -> ());
Mutex.unlock mutex;
Unix.gettimeofday ()

let reset () = ()

let reset_flush_history () = ()

let finish () = Notty_unix.Term.release term
end

let dumb = (module Dumb : S)

let main = ref dumb
Expand All @@ -163,10 +104,6 @@ module Backend = struct

let compose (module A : S) (module B : S) : (module S) =
(module struct
let start () =
A.start ();
B.start ()

let print_user_message msg =
A.print_user_message msg;
B.print_user_message msg
Expand Down Expand Up @@ -194,68 +131,26 @@ module Backend = struct

let spawn_thread = Fdecl.create Dyn.opaque

(** [Threaded] is the interface for user interfaces that are rendered in a
separate thread. *)
module type Threaded = sig
(** [start] is called by the main thread to start broadcasting the user
interface. Any initial setup should be performed here. *)
val start : unit -> unit

(** [render state] is called by the main thread to render the current state
of the user interface. *)
val render : state -> unit

(** [handle_user_events ~now ~timeout mutex] is called by the main thread to
handle user events such as keypresses. The function should return the
time at which the next event should be handled. A [mutex] is provided in
order to lock the state of the UI.*)
val handle_user_events : now:float -> timeout:float -> Mutex.t -> float

(** [reset] is called by the main thread to reset the user interface. *)
val reset : unit -> unit

(** [reset_flush_history] is called by the main thread to reset and flush
the user interface. *)
val reset_flush_history : unit -> unit

(** [finish] is called finally by the main thread to finish broadcasting the
user interface. Any locks on the terminal should be released here. *)
val finish : unit -> unit
end

module Progress_no_flush_threaded : Threaded = struct
include Progress_no_flush

let render state =
while not (Queue.is_empty state.messages) do
print_user_message (Queue.pop_exn state.messages)
done;
set_status_line state.status_line;
flush stderr

let handle_user_events ~now ~timeout _ =
Unix.sleepf timeout;
now +. timeout
end

let threaded (module Base : Threaded) : (module S) =
let threaded (module Base : S) : (module S) =
let module T = struct
let mutex = Mutex.create ()

let finish_cv = Condition.create ()

type state =
{ messages : User_message.t Queue.t
; mutable finish_requested : bool
; mutable finished : bool
; mutable status_line : User_message.Style.t Pp.t option
}

let state =
{ messages = Queue.create ()
; status_line = None
; finished = false
; finish_requested = false
}

let start () =
Mutex.lock mutex;
Base.start ();
Mutex.unlock mutex

let finish () =
Mutex.lock mutex;
state.finish_requested <- true;
Expand Down Expand Up @@ -294,45 +189,33 @@ module Backend = struct
let open T in
let last = ref (Unix.gettimeofday ()) in
let frame_rate = 1. /. 60. in
let cleanup () =
state.finished <- true;
Base.finish ();
Condition.broadcast finish_cv;
Mutex.unlock mutex
in
try
Base.start ();
while true do
Mutex.lock mutex;
Base.render state;
while not (Queue.is_empty state.messages) do
Base.print_user_message (Queue.pop_exn state.messages)
done;
Base.set_status_line state.status_line;
flush stderr;
let finish_requested = state.finish_requested in
if finish_requested then raise_notrace Exit;
Mutex.unlock mutex;
let now = Unix.gettimeofday () in
let elapsed = now -. !last in
let new_time =
if elapsed >= frame_rate then
Base.handle_user_events ~now ~timeout:0.0 mutex
else
let delta = frame_rate -. elapsed in
Base.handle_user_events ~now ~timeout:delta mutex
in
last := new_time
if elapsed >= frame_rate then last := now
else
let delta = frame_rate -. elapsed in
Unix.sleepf delta;
last := delta +. now
done
with
| Exit -> cleanup ()
| exn ->
let exn = Exn_with_backtrace.capture exn in
cleanup ();
Exn_with_backtrace.reraise exn );
with Exit ->
state.finished <- true;
Condition.broadcast finish_cv;
Mutex.unlock mutex );
(module T)

let progress =
let t = lazy (threaded (module Progress_no_flush_threaded)) in
fun () -> Lazy.force t

let tui =
let t = lazy (threaded (module Tui ())) in
let t = lazy (threaded (module Progress_no_flush)) in
fun () -> Lazy.force t
end

Expand Down
9 changes: 4 additions & 5 deletions src/dune_console/dune_console.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ module Backend : sig
val compose : t -> t -> t

(** A dumb backend that hides the status line and simply dump the messages to
the terminal. *)
the terminal *)
val dumb : t

(** A backend that just displays the status line in the terminal. *)
(** A backend that just displays the status line in the terminal *)
val progress : unit -> t

(** A backend that uses Notty to display the status line in the terminal. *)
val tui : unit -> t

val spawn_thread : ((unit -> unit) -> unit) Fdecl.t

val threaded : t -> t
end

(** Format and print a user message to the console *)
Expand Down
Loading