From 4a0dc0f55414b67d71db6a325511aef547cf0dbd Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Fri, 12 Jun 2020 15:27:15 +0200 Subject: [PATCH] Default to no ANSI, ASCII-only output (#444) * Default to no color/no tty until detected * Default to ASCII-only output unless color enabled --- deps/simple_logging | 2 +- src/alire/alire-dependencies-diffs.adb | 6 ++-- src/alire/alire-solutions-diffs.adb | 39 ++++++++++++++++++-------- src/alire/alire-utils-tty.adb | 27 +++++++++--------- src/alire/alire-utils-tty.ads | 6 ++++ src/alire/alire.ads | 5 ++++ src/alire/alire_early_elaboration.adb | 15 ++-------- src/alr/alr-commands.adb | 13 +++++---- 8 files changed, 66 insertions(+), 47 deletions(-) diff --git a/deps/simple_logging b/deps/simple_logging index 2e3f631a..6030caf9 160000 --- a/deps/simple_logging +++ b/deps/simple_logging @@ -1 +1 @@ -Subproject commit 2e3f631a9a18f29fd56fa66e19ab34c903a8eb02 +Subproject commit 6030caf9c8e4a73b6aade2f543a53aefbd5a1b37 diff --git a/src/alire/alire-dependencies-diffs.adb b/src/alire/alire-dependencies-diffs.adb index 12387a86..1a642830 100644 --- a/src/alire/alire-dependencies-diffs.adb +++ b/src/alire/alire-dependencies-diffs.adb @@ -68,8 +68,10 @@ package body Alire.Dependencies.Diffs is begin if This.Contains_Changes then - Summarize (This.Added, "(added)", TTY.OK ("✓")); - Summarize (This.Removed, "(removed)", TTY.Emph ("✗")); + Summarize (This.Added, "(added)", + (if TTY.Color_Enabled then TTY.OK ("✓") else "+")); + Summarize (This.Removed, "(removed)", + (if TTY.Color_Enabled then TTY.Emph ("✗") else "-")); Table.Print (Info); else Trace.Info (" No changes."); diff --git a/src/alire/alire-solutions-diffs.adb b/src/alire/alire-solutions-diffs.adb index 4d10bf3e..817bd503 100644 --- a/src/alire/alire-solutions-diffs.adb +++ b/src/alire/alire-solutions-diffs.adb @@ -198,18 +198,33 @@ package body Alire.Solutions.Diffs is -- Show icon of change - Table.Append - (Prefix - & (case This.Change (Key (I)) is - when Added => TTY.OK ("✓"), - when Removed => TTY.Emph ("✗"), - when External => TTY.Warn ("↪"), - when Upgraded => TTY.OK ("⭧"), - when Downgraded => TTY.Warn ("⭨"), - when Pinned => TTY.OK ("⊙"), - when Unpinned => TTY.Emph ("𐩒"), - when Unchanged => TTY.OK ("="), - when Unsolved => TTY.Error ("⚠"))); + if TTY.Color_Enabled then + Table.Append + (Prefix + & (case This.Change (Key (I)) is + when Added => TTY.OK ("✓"), + when Removed => TTY.Emph ("✗"), + when External => TTY.Warn ("↪"), + when Upgraded => TTY.OK ("⭧"), + when Downgraded => TTY.Warn ("⭨"), + when Pinned => TTY.OK ("⊙"), + when Unpinned => TTY.Emph ("𐩒"), + when Unchanged => TTY.OK ("="), + when Unsolved => TTY.Error ("⚠"))); + else + Table.Append + (Prefix + & (case This.Change (Key (I)) is + when Added => "+", + when Removed => "-", + when External => "~", + when Upgraded => "^", + when Downgraded => "v", + when Pinned => ".", + when Unpinned => "o", + when Unchanged => "=", + when Unsolved => "!")); + end if; -- Always show crate name diff --git a/src/alire/alire-utils-tty.adb b/src/alire/alire-utils-tty.adb index b70eb21c..5dbc52b4 100644 --- a/src/alire/alire-utils-tty.adb +++ b/src/alire/alire-utils-tty.adb @@ -5,9 +5,7 @@ package body Alire.Utils.TTY is use all type ANSI.Colors; use all type ANSI.Styles; - type States is (Disabled, Enabled, Default); - - Status : States := Default; + Use_Color : Boolean := False; -- Err on the safe side function Regular_Decorator (Level : Simple_Logging.Levels; Message : String) return String; @@ -15,15 +13,11 @@ package body Alire.Utils.TTY is function Verbose_Decorator (Level : Simple_Logging.Levels; Message : String) return String; - --------------- - -- Use_Color -- - --------------- + ------------------- + -- Color_Enabled -- + ------------------- - function Use_Color return Boolean is - (case Status is - when Enabled => True, - when Disabled => False, - when Default => Simple_Logging.Is_TTY); + function Color_Enabled return Boolean is (Use_Color); ------------------- -- Disable_Color -- @@ -31,7 +25,7 @@ package body Alire.Utils.TTY is procedure Disable_Color is begin - Status := Disabled; + Use_Color := False; end Disable_Color; ------------------ @@ -43,8 +37,13 @@ package body Alire.Utils.TTY is -- Enable when appropriate - if Force or else Simple_Logging.Is_TTY then - Status := Enabled; + if Force or else Is_TTY then + Use_Color := True; + Trace.Debug ("Color output enabled"); + else + Trace.Debug ("Color output was requested but not enabled:" + & " force=" & Force'Img + & "; Is_TTY=" & Is_TTY'Img); end if; -- Set debug colors. When Detail/Debug are also enabled, we add the diff --git a/src/alire/alire-utils-tty.ads b/src/alire/alire-utils-tty.ads index 971c86af..e548ca54 100644 --- a/src/alire/alire-utils-tty.ads +++ b/src/alire/alire-utils-tty.ads @@ -9,6 +9,12 @@ package Alire.Utils.TTY with Preelaborate is -- Re-expose for clients package ANSI renames Standard.ANSI; + -------------------- + -- Color enabling -- + -------------------- + + function Color_Enabled return Boolean; + procedure Disable_Color; -- Disables color/formatting output even when TTY is capable diff --git a/src/alire/alire.ads b/src/alire/alire.ads index 816bc014..1cb2bf29 100644 --- a/src/alire/alire.ads +++ b/src/alire/alire.ads @@ -197,6 +197,11 @@ package Alire with Preelaborate is package Trace renames Simple_Logging; + Is_TTY : Boolean renames Simple_Logging.Is_TTY; + -- Flag to enable ASCII control sequences for progress indicators. When + -- redirecting the output these do not work and are too noisy. Defaults + -- to False. + Log_Level : Simple_Logging.Levels renames Simple_Logging.Level; -- This one selects the verbosity level of the logging library. The usage -- of log levels in Alire is as follows. By default, no output is produced diff --git a/src/alire/alire_early_elaboration.adb b/src/alire/alire_early_elaboration.adb index 40289098..dedb8457 100644 --- a/src/alire/alire_early_elaboration.adb +++ b/src/alire/alire_early_elaboration.adb @@ -5,7 +5,7 @@ with Alire; with GNAT.Command_Line; with GNAT.OS_Lib; -with GNATCOLL.Terminal; +with Interfaces.C_Streams; with Simple_Logging.Filtering; @@ -141,18 +141,9 @@ package body Alire_Early_Elaboration is ------------------- procedure TTY_Detection is - use GNATCOLL.Terminal; - Info : Terminal_Info; + use Interfaces.C_Streams; begin - Init_For_Stdout (Info); - - -- Internally, GNATCOLL uses _istty to ascertain color availability, so - -- this serves us too to check if output is being redirected, in which - -- case we don't want certain log output to be emitted. - - if not Has_Colors (Info) then - Simple_Logging.Is_TTY := False; - end if; + Simple_Logging.Is_TTY := isatty (fileno (stdout)) /= 0; end TTY_Detection; begin diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index cb188b4f..1682405a 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -623,17 +623,18 @@ package body Alr.Commands is -- Raw_Arguments. if No_TTY then - Simple_Logging.Is_TTY := False; + Alire.Is_TTY := False; end if; - if Platform.Operating_System in Alire.Platforms.Windows or else - No_Color or else - No_TTY + if Platform.Operating_System not in Alire.Platforms.Windows and then + not No_Color and then + not No_TTY then - Alire.Utils.TTY.Disable_Color; - else Alire.Utils.TTY.Enable_Color (Force => False); -- This may still not enable color if TTY is detected to be incapable + + Simple_Logging.ASCII_Only := False; + -- Also use a fancier busy spinner end if; if Raw_Arguments.Is_Empty then -- 2.39.5