-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add terminal ui backend based on NoTTY
<!-- ps-id: a91fcabe-9c8d-485c-a1e8-c781ed4658bd --> Signed-off-by: Ali Caglayan <alizter@gmail.com>
- Loading branch information
Showing
34 changed files
with
4,641 additions
and
39 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
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ val to_string : t -> string | |
val compare : t -> t -> Ordering.t | ||
|
||
val max : t -> t -> t | ||
|
||
val min : t -> t -> 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 |
---|---|---|
@@ -1,37 +1,44 @@ | ||
module Display = Dune_engine.Display | ||
|
||
type t = | ||
{ status_line : bool | ||
; verbosity : Display.t | ||
} | ||
| Simple of | ||
{ status_line : bool | ||
; verbosity : Display.t | ||
} | ||
| Tui | ||
|
||
let progress = { status_line = true; verbosity = Quiet } | ||
let progress = Simple { status_line = true; verbosity = Quiet } | ||
|
||
let verbose = { status_line = true; verbosity = Verbose } | ||
let verbose = Simple { status_line = true; verbosity = Verbose } | ||
|
||
let short = { status_line = true; verbosity = Short } | ||
let short = Simple { status_line = true; verbosity = Short } | ||
|
||
let quiet = { status_line = false; verbosity = Quiet } | ||
let quiet = Simple { status_line = false; verbosity = Quiet } | ||
|
||
let short_no_status = { status_line = false; verbosity = Short } | ||
let short_no_status = Simple { status_line = false; verbosity = Short } | ||
|
||
(* Even though [status_line] is true by default in most of these, the status | ||
line is actually not shown if the output is redirected to a file or a | ||
pipe. *) | ||
line is actually not shown if the output is redirected to a file or a | ||
pipe. *) | ||
let all = | ||
[ ("progress", progress) | ||
; ("verbose", verbose) | ||
; ("short", short) | ||
; ("quiet", quiet) | ||
; ("tui", Tui) | ||
] | ||
|
||
let to_dyn { status_line; verbosity } : Dyn.t = | ||
Record | ||
[ ("status_line", Dyn.Bool status_line) | ||
; ("verbosity", Display.to_dyn verbosity) | ||
] | ||
|
||
let console_backend t = | ||
match t.status_line with | ||
| false -> Dune_console.Backend.dumb | ||
| true -> Dune_console.Backend.progress_threaded () | ||
let to_dyn : t -> Dyn.t = function | ||
| Simple { verbosity; status_line } -> | ||
Dyn.Record | ||
[ ("verbosity", Display.to_dyn verbosity) | ||
; ("status_line", Dyn.Bool status_line) | ||
] | ||
| Tui -> Variant ("Tui", []) | ||
|
||
let console_backend = function | ||
| Tui -> Dune_tui.backend () | ||
| Simple { status_line; _ } -> ( | ||
match status_line with | ||
| false -> Dune_console.Backend.dumb | ||
| true -> Dune_console.Backend.progress_threaded ()) |
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
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,5 @@ | ||
(library | ||
(name dune_tui) | ||
(libraries stdune dune_notty dune_notty_unix dune_console threads.posix) | ||
(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
open Stdune | ||
|
||
let attr_of_ansi_color_style (s : Ansi_color.Style.t) = | ||
let module A = Notty.A in | ||
match s with | ||
| `Fg_black -> A.(fg black) | ||
| `Fg_red -> A.(fg red) | ||
| `Fg_green -> A.(fg green) | ||
| `Fg_yellow -> A.(fg yellow) | ||
| `Fg_blue -> A.(fg blue) | ||
| `Fg_magenta -> A.(fg magenta) | ||
| `Fg_cyan -> A.(fg cyan) | ||
| `Fg_white -> A.(fg white) | ||
| `Fg_default -> A.empty | ||
| `Fg_bright_black -> A.(fg lightblack) | ||
| `Fg_bright_red -> A.(fg lightred) | ||
| `Fg_bright_green -> A.(fg lightgreen) | ||
| `Fg_bright_yellow -> A.(fg lightyellow) | ||
| `Fg_bright_blue -> A.(fg lightblue) | ||
| `Fg_bright_magenta -> A.(fg lightmagenta) | ||
| `Fg_bright_cyan -> A.(fg lightcyan) | ||
| `Fg_bright_white -> A.(fg lightwhite) | ||
| `Bg_black -> A.(bg black) | ||
| `Bg_red -> A.(bg red) | ||
| `Bg_green -> A.(bg green) | ||
| `Bg_yellow -> A.(bg yellow) | ||
| `Bg_blue -> A.(bg blue) | ||
| `Bg_magenta -> A.(bg magenta) | ||
| `Bg_cyan -> A.(bg cyan) | ||
| `Bg_white -> A.(bg white) | ||
| `Bg_default -> A.empty | ||
| `Bg_bright_black -> A.(bg lightblack) | ||
| `Bg_bright_red -> A.(bg lightred) | ||
| `Bg_bright_green -> A.(bg lightgreen) | ||
| `Bg_bright_yellow -> A.(bg lightyellow) | ||
| `Bg_bright_blue -> A.(bg lightblue) | ||
| `Bg_bright_magenta -> A.(bg lightmagenta) | ||
| `Bg_bright_cyan -> A.(bg lightcyan) | ||
| `Bg_bright_white -> A.(bg lightwhite) | ||
| `Bold -> A.(st bold) | ||
| `Italic -> A.(st italic) | ||
| `Dim -> A.(st dim) | ||
| `Underline -> A.(st underline) | ||
|
||
let attr_of_user_message_style fmt t (pp : User_message.Style.t Pp.t) : unit = | ||
let attr = | ||
let module A = Notty.A in | ||
match (t : User_message.Style.t) with | ||
| Loc -> A.(st bold) | ||
| Error -> A.(st bold ++ fg red) | ||
| Warning -> A.(st bold ++ fg magenta) | ||
| Kwd -> A.(st bold ++ fg blue) | ||
| Id -> A.(st bold ++ fg yellow) | ||
| Prompt -> A.(st bold ++ fg green) | ||
| Hint -> A.(st italic ++ fg white) | ||
| Details -> A.(st dim ++ fg white) | ||
| Ok -> A.(st italic ++ fg green) | ||
| Debug -> A.(st underline ++ fg lightcyan) | ||
| Success -> A.(st bold ++ fg green) | ||
| Ansi_styles l -> | ||
List.fold_left ~init:A.empty l ~f:(fun attr s -> | ||
A.(attr ++ attr_of_ansi_color_style s)) | ||
in | ||
Notty.I.pp_attr attr Pp.to_fmt fmt pp | ||
|
||
let image_of_user_message_style_pp = | ||
Notty.I.strf "%a@." | ||
(Pp.to_fmt_with_tags ~tag_handler:attr_of_user_message_style) | ||
|
||
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 -> [ image_of_user_message_style_pp message ] | ||
in | ||
let messages = | ||
List.map messages ~f:(fun msg -> | ||
image_of_user_message_style_pp (User_message.pp msg)) | ||
in | ||
Notty.I.vcat (messages @ status) | ||
|
||
let render (state : Dune_console.Threaded.state) = | ||
let messages = Queue.to_list state.messages in | ||
let image = image ~status_line:state.status_line ~messages in | ||
Term.image term image | ||
|
||
let rec handle_user_events ~now ~time_budget _mutex = | ||
(* We check for any user input and handle it. If we go over the | ||
[time_budget] we give up and continue. *) | ||
let input_fds, _, _ = Unix.select [ Unix.stdin ] [] [] time_budget in | ||
match input_fds with | ||
| [] -> | ||
now +. time_budget | ||
(* Nothing to do, we return the time at the end of the time budget. *) | ||
| _ :: _ -> ( | ||
(* TODO if anything fancy is done in the UI in the future we need to lock | ||
the state with the provided mutex *) | ||
match Term.event term with | ||
| `Key (`ASCII 'q', _) -> | ||
(* When we encounter q we make sure to quit by signaling termination. *) | ||
Unix.kill (Unix.getpid ()) Sys.sigterm; | ||
(* We return either the intended end time or the current time if we | ||
went over it. *) | ||
Float.max (now +. time_budget) (Unix.gettimeofday ()) | ||
| _ -> | ||
(* We return either the intended end time or the current time if we | ||
went over it. *) | ||
Float.max (now +. time_budget) (Unix.gettimeofday ()) | ||
| exception Unix.Unix_error ((EAGAIN | EWOULDBLOCK), _, _) -> | ||
(* If we encounter an exception, we make sure to rehandle user events | ||
with a corrected time budget. *) | ||
handle_user_events ~now _mutex | ||
~time_budget: | ||
(time_budget -. Float.min time_budget (Unix.gettimeofday () -. now)) | ||
) | ||
|
||
let reset () = () | ||
|
||
let reset_flush_history () = () | ||
|
||
let finish () = Notty_unix.Term.release term | ||
end | ||
|
||
let backend = | ||
let t = lazy (Dune_console.Threaded.make (module Tui ())) in | ||
fun () -> Lazy.force 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,2 @@ | ||
(** A backend that uses Notty to display the status line in the terminal. *) | ||
val backend : unit -> Dune_console.Backend.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,13 @@ | ||
Copyright (c) 2016-2017 David Kaloper Meršinjak | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Oops, something went wrong.