All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6] help: colorize man pages
@ 2021-05-23  5:44 Felipe Contreras
  2021-05-24 13:13 ` Phillip Wood
                   ` (3 more replies)
  0 siblings, 4 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-05-23  5:44 UTC (permalink / raw)
  To: git
  Cc: Phillip Wood, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano, Felipe Contreras

We already colorize tools traditionally not colorized by default, like
diff and grep. Let's do the same for man.

Our man pages don't contain many useful colors (just blue links),
moreover, many people have groff SGR disabled, so they don't see any
colors with man pages.

We can set LESS_TERMCAP variables to render bold and underlined text
with colors in the pager; a common trick[1].

Bold is rendered as red, underlined as blue, and standout (prompt and
highlighted search) as inverse cyan.

Obviously this only works when the less pager is used.

If the user already has LESS_TERMCAP variables set in his/her
environment, those are respected and nothing changes.

A new color configuration is added: `color.man` for the people that want
to turn this feature off, otherwise `color.ui` is respected.
Additionally, if color.pager is not enabled, this is disregarded.

Normally check_auto_color() would check the value of `color.pager`, but
in this particular case it's not git the one executing the pager, but
man. Therefore we need to check pager_use_color ourselves.

Also--unlike other color.* configurations--color.man=always does not
make any sense here; `git help` is always run for a tty (it would be very
strange for a user to do `git help $page > output`, but in fact, that
works anyway, we don't even need to check if stdout is a tty, but just
to be consistent we do). So it's simply a boolean in our case.

So, in order for this change to have any effect:

 1. The user must use less
 2. Not have the same LESS_TERMCAP variables set
 3. Have color.ui enabled
 4. Not have color.pager disabled
 5. Not have color.man disabled
 7. Not have git with stdout directed to a file

Fortunately the vast majority of our users meet all of the above, and
anybody who doesn't would not be affected negatively (plus very likely
comprises a very tiny minority).

[1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147

Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Phillip Wood <phillip.wood123@gmail.com>
Comments-by: Jeff King <peff@peff.net>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---

I returned back to the LESS_TERMCAP variables because -D options only
exists for non-dos systems since 2021.

I tested this with a version of less that is 10 years old, and everything
works as expected.

Also, I noticed less already chooses nice colors for standout with
--use-color, and the prompt color is cyan, which is the fist one
visible, so I changed standout to cyan.

Range-diff against v5:
1:  64c93d501f ! 1:  3a3e2837ad help: colorize man pages
    @@ Commit message
         moreover, many people have groff SGR disabled, so they don't see any
         colors with man pages.
     
    -    We can set the LESS variable to render bold, underlined, and standout
    -    text with colors in the less pager.
    +    We can set LESS_TERMCAP variables to render bold and underlined text
    +    with colors in the pager; a common trick[1].
     
         Bold is rendered as red, underlined as blue, and standout (prompt and
    -    highlighted search) as inverse magenta.
    +    highlighted search) as inverse cyan.
     
         Obviously this only works when the less pager is used.
     
    -    If the user has already set the LESS variable in his/her environment,
    -    that is respected, and nothing changes. The same if any LESS_TERMCAP_*
    -    variables are set.
    +    If the user already has LESS_TERMCAP variables set in his/her
    +    environment, those are respected and nothing changes.
     
         A new color configuration is added: `color.man` for the people that want
         to turn this feature off, otherwise `color.ui` is respected.
    @@ Commit message
         So, in order for this change to have any effect:
     
          1. The user must use less
    -     2. Not have the LESS variable set
    +     2. Not have the same LESS_TERMCAP variables set
          3. Have color.ui enabled
          4. Not have color.pager disabled
          5. Not have color.man disabled
    @@ Commit message
         anybody who doesn't would not be affected negatively (plus very likely
         comprises a very tiny minority).
     
    +    [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
    +
         Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
         Phillip Wood <phillip.wood123@gmail.com>
         Comments-by: Jeff King <peff@peff.net>
    @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *pag
     +	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
     +		return;
     +
    -+	/* User already has configured less colors */
    -+	if (getenv("LESS_TERMCAP_md") ||
    -+		getenv("LESS_TERMCAP_us") ||
    -+		getenv("LESS_TERMCAP_so")) {
    -+		return;
    -+	}
    -+
     +	/* Disable groff colors */
     +	setenv("GROFF_NO_SGR", "1", 0);
     +
    -+	/* Add red to bold, blue to underline, and magenta to standout */
    -+	/* No visual information is lost */
    -+	setenv("LESS", "Dd+r$Du+b$Ds+m", 0);
    ++	/* Bold */
    ++	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
    ++	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
    ++
    ++	/* Underline */
    ++	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
    ++	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
    ++
    ++	/* Standout */
    ++	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
    ++	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
     +}
     +
      static void exec_man_man(const char *path, const char *page)
    @@ builtin/help.c: static int git_help_config(const char *var, const char *value, v
      }
      
      static struct cmdnames main_cmds, other_cmds;
    +
    + ## color.h ##
    +@@ color.h: struct strbuf;
    + #define GIT_COLOR_FAINT		"\033[2m"
    + #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
    + #define GIT_COLOR_REVERSE	"\033[7m"
    ++#define GIT_COLOR_UNDERLINE	"\033[4m"
    + 
    + /* A special value meaning "no color selected" */
    + #define GIT_COLOR_NIL "NIL"

 Documentation/config/color.txt |  5 +++++
 builtin/help.c                 | 32 +++++++++++++++++++++++++++++++-
 color.h                        |  1 +
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
index d5daacb13a..11278b7f72 100644
--- a/Documentation/config/color.txt
+++ b/Documentation/config/color.txt
@@ -126,6 +126,11 @@ color.interactive.<slot>::
 	or `error`, for four distinct types of normal output from
 	interactive commands.
 
+color.man::
+	This flag can be used to disable the automatic colorizaton of man
+	pages when using the less pager. It's activated only when color.ui
+	allows it, and also when color.pager is on. (`true` by default).
+
 color.pager::
 	A boolean to enable/disable colored output when the pager is in
 	use (default is true).
diff --git a/builtin/help.c b/builtin/help.c
index bb339f0fc8..b6331afc2e 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -11,6 +11,7 @@
 #include "config-list.h"
 #include "help.h"
 #include "alias.h"
+#include "color.h"
 
 #ifndef DEFAULT_HELP_FORMAT
 #define DEFAULT_HELP_FORMAT "man"
@@ -43,6 +44,7 @@ static int verbose = 1;
 static unsigned int colopts;
 static enum help_format help_format = HELP_FORMAT_NONE;
 static int exclude_guides;
+static int man_color = 1;
 static struct option builtin_help_options[] = {
 	OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
 	OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
@@ -253,10 +255,33 @@ static void exec_man_konqueror(const char *path, const char *page)
 	}
 }
 
+static void colorize_man(void)
+{
+	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
+		return;
+
+	/* Disable groff colors */
+	setenv("GROFF_NO_SGR", "1", 0);
+
+	/* Bold */
+	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
+	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
+
+	/* Underline */
+	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
+	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
+
+	/* Standout */
+	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
+	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
+}
+
 static void exec_man_man(const char *path, const char *page)
 {
 	if (!path)
 		path = "man";
+
+	colorize_man();
 	execlp(path, "man", page, (char *)NULL);
 	warning_errno(_("failed to exec '%s'"), path);
 }
@@ -264,6 +289,7 @@ static void exec_man_man(const char *path, const char *page)
 static void exec_man_cmd(const char *cmd, const char *page)
 {
 	struct strbuf shell_cmd = STRBUF_INIT;
+	colorize_man();
 	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
 	execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
 	warning(_("failed to exec '%s'"), cmd);
@@ -371,8 +397,12 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	}
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
+	if (!strcmp(var, "color.man")) {
+		man_color = git_config_bool(var, value);
+		return 0;
+	}
 
-	return git_default_config(var, value, cb);
+	return git_color_default_config(var, value, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/color.h b/color.h
index 98894d6a17..d012add4e8 100644
--- a/color.h
+++ b/color.h
@@ -51,6 +51,7 @@ struct strbuf;
 #define GIT_COLOR_FAINT		"\033[2m"
 #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
 #define GIT_COLOR_REVERSE	"\033[7m"
+#define GIT_COLOR_UNDERLINE	"\033[4m"
 
 /* A special value meaning "no color selected" */
 #define GIT_COLOR_NIL "NIL"
-- 
2.32.0.rc0


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] help: colorize man pages
  2021-05-23  5:44 [PATCH v6] help: colorize man pages Felipe Contreras
@ 2021-05-24 13:13 ` Phillip Wood
  2021-05-24 16:56   ` Felipe Contreras
  2021-06-08 12:35 ` Ævar Arnfjörð Bjarmason
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 22+ messages in thread
From: Phillip Wood @ 2021-05-24 13:13 UTC (permalink / raw)
  To: Felipe Contreras, git
  Cc: Ævar Arnfjörð Bjarmason, Jeff King, Junio C Hamano

On 23/05/2021 06:44, Felipe Contreras wrote:
> We already colorize tools traditionally not colorized by default, like
> diff and grep. Let's do the same for man.
> 
> Our man pages don't contain many useful colors (just blue links),
> moreover, many people have groff SGR disabled, so they don't see any
> colors with man pages.
> 
> We can set LESS_TERMCAP variables to render bold and underlined text
> with colors in the pager; a common trick[1].
> 
> Bold is rendered as red, underlined as blue, and standout (prompt and
> highlighted search) as inverse cyan.
> 
> Obviously this only works when the less pager is used.
> 
> If the user already has LESS_TERMCAP variables set in his/her
> environment, those are respected and nothing changes.
> 
> A new color configuration is added: `color.man` for the people that want
> to turn this feature off, otherwise `color.ui` is respected.
> Additionally, if color.pager is not enabled, this is disregarded.
> 
> Normally check_auto_color() would check the value of `color.pager`, but
> in this particular case it's not git the one executing the pager, but
> man. Therefore we need to check pager_use_color ourselves.
> 
> Also--unlike other color.* configurations--color.man=always does not
> make any sense here; `git help` is always run for a tty (it would be very
> strange for a user to do `git help $page > output`, but in fact, that
> works anyway, we don't even need to check if stdout is a tty, but just
> to be consistent we do). So it's simply a boolean in our case.
> 
> So, in order for this change to have any effect:
> 
>   1. The user must use less
>   2. Not have the same LESS_TERMCAP variables set
>   3. Have color.ui enabled
>   4. Not have color.pager disabled
>   5. Not have color.man disabled
>   7. Not have git with stdout directed to a file
> 
> Fortunately the vast majority of our users meet all of the above, and
> anybody who doesn't would not be affected negatively (plus very likely
> comprises a very tiny minority).
> 
> [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
> 
> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Phillip Wood <phillip.wood123@gmail.com>

This footer seems to have got broken between v5 and v6 - were you 
intending to delete it (which is fine by me) as my comments were about 
the approach of the last patch?

I'm still not convinced that git should be messing with the appearance 
of man pages but I don't think we're ever going to agree on that.

Best Wishes

Phillip

> Comments-by: Jeff King <peff@peff.net>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
> 
> I returned back to the LESS_TERMCAP variables because -D options only
> exists for non-dos systems since 2021.
> 
> I tested this with a version of less that is 10 years old, and everything
> works as expected.
> 
> Also, I noticed less already chooses nice colors for standout with
> --use-color, and the prompt color is cyan, which is the fist one
> visible, so I changed standout to cyan.
> 
> Range-diff against v5:
> 1:  64c93d501f ! 1:  3a3e2837ad help: colorize man pages
>      @@ Commit message
>           moreover, many people have groff SGR disabled, so they don't see any
>           colors with man pages.
>       
>      -    We can set the LESS variable to render bold, underlined, and standout
>      -    text with colors in the less pager.
>      +    We can set LESS_TERMCAP variables to render bold and underlined text
>      +    with colors in the pager; a common trick[1].
>       
>           Bold is rendered as red, underlined as blue, and standout (prompt and
>      -    highlighted search) as inverse magenta.
>      +    highlighted search) as inverse cyan.
>       
>           Obviously this only works when the less pager is used.
>       
>      -    If the user has already set the LESS variable in his/her environment,
>      -    that is respected, and nothing changes. The same if any LESS_TERMCAP_*
>      -    variables are set.
>      +    If the user already has LESS_TERMCAP variables set in his/her
>      +    environment, those are respected and nothing changes.
>       
>           A new color configuration is added: `color.man` for the people that want
>           to turn this feature off, otherwise `color.ui` is respected.
>      @@ Commit message
>           So, in order for this change to have any effect:
>       
>            1. The user must use less
>      -     2. Not have the LESS variable set
>      +     2. Not have the same LESS_TERMCAP variables set
>            3. Have color.ui enabled
>            4. Not have color.pager disabled
>            5. Not have color.man disabled
>      @@ Commit message
>           anybody who doesn't would not be affected negatively (plus very likely
>           comprises a very tiny minority).
>       
>      +    [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>      +
>           Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>           Phillip Wood <phillip.wood123@gmail.com>
>           Comments-by: Jeff King <peff@peff.net>
>      @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *pag
>       +	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
>       +		return;
>       +
>      -+	/* User already has configured less colors */
>      -+	if (getenv("LESS_TERMCAP_md") ||
>      -+		getenv("LESS_TERMCAP_us") ||
>      -+		getenv("LESS_TERMCAP_so")) {
>      -+		return;
>      -+	}
>      -+
>       +	/* Disable groff colors */
>       +	setenv("GROFF_NO_SGR", "1", 0);
>       +
>      -+	/* Add red to bold, blue to underline, and magenta to standout */
>      -+	/* No visual information is lost */
>      -+	setenv("LESS", "Dd+r$Du+b$Ds+m", 0);
>      ++	/* Bold */
>      ++	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
>      ++	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
>      ++
>      ++	/* Underline */
>      ++	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
>      ++	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
>      ++
>      ++	/* Standout */
>      ++	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
>      ++	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
>       +}
>       +
>        static void exec_man_man(const char *path, const char *page)
>      @@ builtin/help.c: static int git_help_config(const char *var, const char *value, v
>        }
>        
>        static struct cmdnames main_cmds, other_cmds;
>      +
>      + ## color.h ##
>      +@@ color.h: struct strbuf;
>      + #define GIT_COLOR_FAINT		"\033[2m"
>      + #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
>      + #define GIT_COLOR_REVERSE	"\033[7m"
>      ++#define GIT_COLOR_UNDERLINE	"\033[4m"
>      +
>      + /* A special value meaning "no color selected" */
>      + #define GIT_COLOR_NIL "NIL"
> 
>   Documentation/config/color.txt |  5 +++++
>   builtin/help.c                 | 32 +++++++++++++++++++++++++++++++-
>   color.h                        |  1 +
>   3 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
> index d5daacb13a..11278b7f72 100644
> --- a/Documentation/config/color.txt
> +++ b/Documentation/config/color.txt
> @@ -126,6 +126,11 @@ color.interactive.<slot>::
>   	or `error`, for four distinct types of normal output from
>   	interactive commands.
>   
> +color.man::
> +	This flag can be used to disable the automatic colorizaton of man
> +	pages when using the less pager. It's activated only when color.ui
> +	allows it, and also when color.pager is on. (`true` by default).
> +
>   color.pager::
>   	A boolean to enable/disable colored output when the pager is in
>   	use (default is true).
> diff --git a/builtin/help.c b/builtin/help.c
> index bb339f0fc8..b6331afc2e 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -11,6 +11,7 @@
>   #include "config-list.h"
>   #include "help.h"
>   #include "alias.h"
> +#include "color.h"
>   
>   #ifndef DEFAULT_HELP_FORMAT
>   #define DEFAULT_HELP_FORMAT "man"
> @@ -43,6 +44,7 @@ static int verbose = 1;
>   static unsigned int colopts;
>   static enum help_format help_format = HELP_FORMAT_NONE;
>   static int exclude_guides;
> +static int man_color = 1;
>   static struct option builtin_help_options[] = {
>   	OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
>   	OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
> @@ -253,10 +255,33 @@ static void exec_man_konqueror(const char *path, const char *page)
>   	}
>   }
>   
> +static void colorize_man(void)
> +{
> +	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
> +		return;
> +
> +	/* Disable groff colors */
> +	setenv("GROFF_NO_SGR", "1", 0);
> +
> +	/* Bold */
> +	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
> +	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
> +
> +	/* Underline */
> +	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
> +	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
> +
> +	/* Standout */
> +	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
> +	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
> +}
> +
>   static void exec_man_man(const char *path, const char *page)
>   {
>   	if (!path)
>   		path = "man";
> +
> +	colorize_man();
>   	execlp(path, "man", page, (char *)NULL);
>   	warning_errno(_("failed to exec '%s'"), path);
>   }
> @@ -264,6 +289,7 @@ static void exec_man_man(const char *path, const char *page)
>   static void exec_man_cmd(const char *cmd, const char *page)
>   {
>   	struct strbuf shell_cmd = STRBUF_INIT;
> +	colorize_man();
>   	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
>   	execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
>   	warning(_("failed to exec '%s'"), cmd);
> @@ -371,8 +397,12 @@ static int git_help_config(const char *var, const char *value, void *cb)
>   	}
>   	if (starts_with(var, "man."))
>   		return add_man_viewer_info(var, value);
> +	if (!strcmp(var, "color.man")) {
> +		man_color = git_config_bool(var, value);
> +		return 0;
> +	}
>   
> -	return git_default_config(var, value, cb);
> +	return git_color_default_config(var, value, cb);
>   }
>   
>   static struct cmdnames main_cmds, other_cmds;
> diff --git a/color.h b/color.h
> index 98894d6a17..d012add4e8 100644
> --- a/color.h
> +++ b/color.h
> @@ -51,6 +51,7 @@ struct strbuf;
>   #define GIT_COLOR_FAINT		"\033[2m"
>   #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
>   #define GIT_COLOR_REVERSE	"\033[7m"
> +#define GIT_COLOR_UNDERLINE	"\033[4m"
>   
>   /* A special value meaning "no color selected" */
>   #define GIT_COLOR_NIL "NIL"
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] help: colorize man pages
  2021-05-24 13:13 ` Phillip Wood
@ 2021-05-24 16:56   ` Felipe Contreras
  0 siblings, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-05-24 16:56 UTC (permalink / raw)
  To: Phillip Wood, Felipe Contreras, git
  Cc: Ævar Arnfjörð Bjarmason, Jeff King, Junio C Hamano

Phillip Wood wrote:
> On 23/05/2021 06:44, Felipe Contreras wrote:

> > Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> > Phillip Wood <phillip.wood123@gmail.com>
> 
> This footer seems to have got broken between v5 and v6 - were you 
> intending to delete it (which is fine by me) as my comments were about 
> the approach of the last patch?

I was probably trying to put you in the Cc list.

> I'm still not convinced that git should be messing with the appearance 
> of man pages but I don't think we're ever going to agree on that.

That's your opinion, and it's fine, we all have opinions.

But the interesting thing for everyone else is *why*. Why aren't you
convinced?

I still haven't heard a convincing argument regarding what makes
`git help` fundamentally different from `git diff` _for the user_.

Cheers.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] help: colorize man pages
  2021-05-23  5:44 [PATCH v6] help: colorize man pages Felipe Contreras
  2021-05-24 13:13 ` Phillip Wood
@ 2021-06-08 12:35 ` Ævar Arnfjörð Bjarmason
  2021-06-08 13:57   ` Junio C Hamano
  2021-06-24  1:44 ` [PATCH v6] help: colorize man pages Felipe Contreras
  2021-06-26  2:50 ` [PATCH v8] help: add option to colorize man pages under less Felipe Contreras
  3 siblings, 1 reply; 22+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-06-08 12:35 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: git, Phillip Wood, Jeff King, Junio C Hamano


On Sun, May 23 2021, Felipe Contreras wrote:

> We already colorize tools traditionally not colorized by default, like
> diff and grep. Let's do the same for man.
>
> Our man pages don't contain many useful colors (just blue links),
> moreover, many people have groff SGR disabled, so they don't see any
> colors with man pages.
>
> We can set LESS_TERMCAP variables to render bold and underlined text
> with colors in the pager; a common trick[1].
>
> Bold is rendered as red, underlined as blue, and standout (prompt and
> highlighted search) as inverse cyan.
>
> Obviously this only works when the less pager is used.
>
> If the user already has LESS_TERMCAP variables set in his/her
> environment, those are respected and nothing changes.
>
> A new color configuration is added: `color.man` for the people that want
> to turn this feature off, otherwise `color.ui` is respected.
> Additionally, if color.pager is not enabled, this is disregarded.
>
> Normally check_auto_color() would check the value of `color.pager`, but
> in this particular case it's not git the one executing the pager, but
> man. Therefore we need to check pager_use_color ourselves.
>
> Also--unlike other color.* configurations--color.man=always does not
> make any sense here; `git help` is always run for a tty (it would be very
> strange for a user to do `git help $page > output`, but in fact, that
> works anyway, we don't even need to check if stdout is a tty, but just
> to be consistent we do). So it's simply a boolean in our case.
>
> So, in order for this change to have any effect:
>
>  1. The user must use less
>  2. Not have the same LESS_TERMCAP variables set
>  3. Have color.ui enabled
>  4. Not have color.pager disabled
>  5. Not have color.man disabled
>  7. Not have git with stdout directed to a file
>
> Fortunately the vast majority of our users meet all of the above, and
> anybody who doesn't would not be affected negatively (plus very likely
> comprises a very tiny minority).
>
> [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>
> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Phillip Wood <phillip.wood123@gmail.com>
> Comments-by: Jeff King <peff@peff.net>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---

I've been running with this on my personal git build since May 26th. I
haven't had any issues with it, and I like the new coloring.

I for one would like to have this picked up by Junio.

I think this is a good example of a change that we're better off just
merging down and then reverting if the wider audience of git users hates
it, rather than trying to come to some perfect consensus here
on-list.

We have a wider audience running "next" than "seen" (but this didn't
even make "seen"), if this were to make it into a release and users
overwhelmingly dislike it it's no big deal. There's a config option to
turn it off, and/or we could make it opt-in later.

Alternatively this could be opt-in and not fall under the color.ui=auto
umbrella, or only in combination with feature.experimental (or a new
ui.experimental?, which would default to that?).

But in any case if judgement call UI changes are always hidden behind
options we'll never make forward progress on things that are possibly
better defaults (and if they're not, we can always simply revert the
change).

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] help: colorize man pages
  2021-06-08 12:35 ` Ævar Arnfjörð Bjarmason
@ 2021-06-08 13:57   ` Junio C Hamano
  2021-06-08 17:48     ` Felipe Contreras
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 22+ messages in thread
From: Junio C Hamano @ 2021-06-08 13:57 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Felipe Contreras, git, Phillip Wood, Jeff King

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> I've been running with this on my personal git build since May 26th. I
> haven't had any issues with it, and I like the new coloring.
> ...
> I think this is a good example of a change that we're better off just
> merging down and then reverting if the wider audience of git users hates
> it, rather than trying to come to some perfect consensus here
> on-list.

My impression was tht we already had a rough consensus here on-list
that it may be good to educate users who like this "new coloring"
like you do to configure their "less", so that they consistently get
the "new coloring" they like whether they are doing "git help git",
"man git", or even "man ls", and the approach the posted patch takes
will not help (it only affects "git help git" among these).

I'd rather not to take it.

Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] help: colorize man pages
  2021-06-08 13:57   ` Junio C Hamano
@ 2021-06-08 17:48     ` Felipe Contreras
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-08 17:48 UTC (permalink / raw)
  To: Junio C Hamano, Ævar Arnfjörð Bjarmason
  Cc: Felipe Contreras, git, Phillip Wood, Jeff King

Junio C Hamano wrote:
> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> 
> > I've been running with this on my personal git build since May 26th. I
> > haven't had any issues with it, and I like the new coloring.
> > ...
> > I think this is a good example of a change that we're better off just
> > merging down and then reverting if the wider audience of git users hates
> > it, rather than trying to come to some perfect consensus here
> > on-list.
> 
> My impression was tht we already had a rough consensus here on-list
> that it may be good to educate users who like this "new coloring"
> like you do to configure their "less",

Not true.

Jeff said users would probably have configured man to use colors
themselves, but he never responded back when I asked him *how* [1].

It is a tricky question, because I already know it's not possible to do
it in a way that works in all distributions, for all programs without
polluting the user environment to do things she probably doesn't want.

> so that they consistently get the "new coloring" they like whether
> they are doing "git help git", "man git", or even "man ls", and the
> approach the posted patch takes will not help (it only affects "git
> help git" among these).

Please explain exactly *how* the user will be able to do that.


Moreover. I don't think git should be in the business of educating users
how to use other software. The way they use less in conjunction with
other software is up to them.

And in fact we already help naive users that have not configured their
pager, so that it works better in git.

We do this for them:

  LESS=FRX LV=-c # see PAGER_ENV

Why aren't we "educating them" about LESS=FRX instead?

We have set good defaults for less since pretty much the start:

  f67b45f862 (Introduce trivial new pager.c helper infrastructure, 2006-02-28)


I don't think Jeff is the consensus. He expressed an opinion that
perhaps X is better, but without clearly defining X, that's not really
a viable option.

> I'd rather not to take it.

Can you explain why? All the outstanding comments have been addressed.

Cheers.

[1] https://lore.kernel.org/git/60a96e76a4b20_857e92085c@natae.notmuch/

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-08 13:57   ` Junio C Hamano
  2021-06-08 17:48     ` Felipe Contreras
@ 2021-06-21  8:34     ` Ævar Arnfjörð Bjarmason
  2021-06-21 10:17       ` Phillip Wood
                         ` (3 more replies)
  1 sibling, 4 replies; 22+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-06-21  8:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Felipe Contreras, Jeff King, Phillip Wood,
	Ævar Arnfjörð Bjarmason

From: Felipe Contreras <felipe.contreras@gmail.com>

We already colorize tools traditionally not colorized by default, like
diff and grep. Let's do the same for man, but only if `color.man` is
explicitly set to "true".

Unlike other `color.*` output this colorization is not enabled by
`color.ui` being true, the user needs to explicitly set the
`color.man` variable to `true.

When it was proposed to treat `color.man` like any other `color.*`
variable some thought that git opting in coloring for an external
program such as man(1) was a step too far[1], even if the user invoked
it via the "git help <topic>" wrapper.

So let's make this explicitly opt-in for now. As noted in the
documentation we're leaving ourselves an out to turn this on by
default in the future, or e.g. putting it under the
feature.experimental umbrella. We probably won't, but let's not
promise users that `color.man` will forever be a special-case.

As for what this actually does the effect of having this enabled is
that a documentation blurb like (some parts elided with "[...]"):

	NAME
	----
	git-config - Get and set [...]

	SYNOPSIS
	--------
	[...]
	'git config' [<file-option>] [...]
	[...]
	The `--type=<type>` option instructs 'git config' to ensure [...]

Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
config" and other '-quoted parts in BLUE UNDERLINE instead of
UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
instead of BOLD. The "Standout" setting is then used for the user's
own search bar (invoked with "/") and prompt. See [2] for more
examples

Normally check_auto_color() would check the value of `color.pager`, but
in this particular case it's not git the one executing the pager, but
man. Therefore we need to check pager_use_color ourselves.

We do not need to support `color.man` being set to `always`; The `git
help` command is always run for a tty (it would be very strange for a
user to do `git help $page > output`, but in fact, that works anyway,
we don't even need to check if stdout is a tty, but just to be
consistent we do). So it's simply a boolean in our case.

So, in order for this change to have any effect:

 1. color.man=true must be set in the config
 2. The user must use less
 3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
 4. Have color.ui enabled
 5. Not have color.pager disabled
 6. Not have git with stdout directed to a file

1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147

Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---

On Tue, Jun 08 2021, Junio C Hamano wrote:

> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>
>> I've been running with this on my personal git build since May 26th. I
>> haven't had any issues with it, and I like the new coloring.
>> ...
>> I think this is a good example of a change that we're better off just
>> merging down and then reverting if the wider audience of git users hates
>> it, rather than trying to come to some perfect consensus here
>> on-list.
>
> My impression was tht we already had a rough consensus here on-list
> that it may be good to educate users who like this "new coloring"
> like you do to configure their "less", so that they consistently get
> the "new coloring" they like whether they are doing "git help git",
> "man git", or even "man ls", and the approach the posted patch takes
> will not help (it only affects "git help git" among these).
>
> I'd rather not to take it.

Fair enough, here's a version I think you and others will find
acceptable then. It allows users like me who like this to explicitly
opt-in via color.man=true.

I also took the liberty of making some changes to the commit message
to reword it for this new behavior, and to show an example of the sort
of colors that change with this patch.

The only change to the patch itself is that now color_man is set to 0
by default, not 1. I also moved its declaration so that it's with the
one other config variable, insted of with the other command-line
options to "git help".

Range-diff against v6:
1:  e021ca1da21 ! 1:  a950ef49e28 help: colorize man pages
    @@ Metadata
     Author: Felipe Contreras <felipe.contreras@gmail.com>
     
      ## Commit message ##
    -    help: colorize man pages
    +    help: colorize man pages if man.color=true under less(1)
     
         We already colorize tools traditionally not colorized by default, like
    -    diff and grep. Let's do the same for man.
    -
    -    Our man pages don't contain many useful colors (just blue links),
    -    moreover, many people have groff SGR disabled, so they don't see any
    -    colors with man pages.
    -
    -    We can set LESS_TERMCAP variables to render bold and underlined text
    -    with colors in the pager; a common trick[1].
    -
    -    Bold is rendered as red, underlined as blue, and standout (prompt and
    -    highlighted search) as inverse cyan.
    -
    -    Obviously this only works when the less pager is used.
    -
    -    If the user already has LESS_TERMCAP variables set in his/her
    -    environment, those are respected and nothing changes.
    -
    -    A new color configuration is added: `color.man` for the people that want
    -    to turn this feature off, otherwise `color.ui` is respected.
    -    Additionally, if color.pager is not enabled, this is disregarded.
    +    diff and grep. Let's do the same for man, but only if `color.man` is
    +    explicitly set to "true".
    +
    +    Unlike other `color.*` output this colorization is not enabled by
    +    `color.ui` being true, the user needs to explicitly set the
    +    `color.man` variable to `true.
    +
    +    When it was proposed to treat `color.man` like any other `color.*`
    +    variable some thought that git opting in coloring for an external
    +    program such as man(1) was a step too far[1], even if the user invoked
    +    it via the "git help <topic>" wrapper.
    +
    +    So let's make this explicitly opt-in for now. As noted in the
    +    documentation we're leaving ourselves an out to turn this on by
    +    default in the future, or e.g. putting it under the
    +    feature.experimental umbrella. We probably won't, but let's not
    +    promise users that `color.man` will forever be a special-case.
    +
    +    As for what this actually does the effect of having this enabled is
    +    that a documentation blurb like (some parts elided with "[...]"):
    +
    +            NAME
    +            ----
    +            git-config - Get and set [...]
    +
    +            SYNOPSIS
    +            --------
    +            [...]
    +            'git config' [<file-option>] [...]
    +            [...]
    +            The `--type=<type>` option instructs 'git config' to ensure [...]
    +
    +    Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
    +    config" and other '-quoted parts in BLUE UNDERLINE instead of
    +    UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
    +    instead of BOLD. The "Standout" setting is then used for the user's
    +    own search bar (invoked with "/") and prompt. See [2] for more
    +    examples
     
         Normally check_auto_color() would check the value of `color.pager`, but
         in this particular case it's not git the one executing the pager, but
         man. Therefore we need to check pager_use_color ourselves.
     
    -    Also--unlike other color.* configurations--color.man=always does not
    -    make any sense here; `git help` is always run for a tty (it would be very
    -    strange for a user to do `git help $page > output`, but in fact, that
    -    works anyway, we don't even need to check if stdout is a tty, but just
    -    to be consistent we do). So it's simply a boolean in our case.
    +    We do not need to support `color.man` being set to `always`; The `git
    +    help` command is always run for a tty (it would be very strange for a
    +    user to do `git help $page > output`, but in fact, that works anyway,
    +    we don't even need to check if stdout is a tty, but just to be
    +    consistent we do). So it's simply a boolean in our case.
     
         So, in order for this change to have any effect:
     
    -     1. The user must use less
    -     2. Not have the same LESS_TERMCAP variables set
    -     3. Have color.ui enabled
    -     4. Not have color.pager disabled
    -     5. Not have color.man disabled
    -     7. Not have git with stdout directed to a file
    -
    -    Fortunately the vast majority of our users meet all of the above, and
    -    anybody who doesn't would not be affected negatively (plus very likely
    -    comprises a very tiny minority).
    +     1. color.man=true must be set in the config
    +     2. The user must use less
    +     3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
    +     4. Have color.ui enabled
    +     5. Not have color.pager disabled
    +     6. Not have git with stdout directed to a file
     
    -    [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
    +    1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
    +    2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
     
         Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
    -    Phillip Wood <phillip.wood123@gmail.com>
    -    Comments-by: Jeff King <peff@peff.net>
         Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
    +    Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
      ## Documentation/config/color.txt ##
     @@ Documentation/config/color.txt: color.interactive.<slot>::
    @@ Documentation/config/color.txt: color.interactive.<slot>::
      	interactive commands.
      
     +color.man::
    -+	This flag can be used to disable the automatic colorizaton of man
    -+	pages when using the less pager. It's activated only when color.ui
    -+	allows it, and also when color.pager is on. (`true` by default).
    ++	This flag can be used to enable the automatic colorizaton of man
    ++	pages when using the less pager, `false` by default. When set to
    ++	`true` it's activated only when `color.ui` allows it, and if
    ++	`color.pager` enable (which it is by default).
     +
      color.pager::
    - 	A boolean to enable/disable colored output when the pager is in
    - 	use (default is true).
    + 	A boolean to specify whether `auto` color modes should colorize
    + 	output going to the pager. Defaults to true; set this to false
    +@@ Documentation/config/color.txt: color.ui::
    + 	output not intended for machine consumption to use color, to
    + 	`true` or `auto` (this is the default since Git 1.8.4) if you
    + 	want such output to use color when written to the terminal.
    +++
    ++When set to `true` certain other `color.*` variables may still not be
    ++turned on unless explicitly enabled. Currently this only applies to
    ++`color.man`, see above. Such opt-in variables may be moved under the
    ++default `color.ui` umbrella in the future.
     
      ## builtin/help.c ##
     @@
    @@ builtin/help.c
      
      #ifndef DEFAULT_HELP_FORMAT
      #define DEFAULT_HELP_FORMAT "man"
    -@@ builtin/help.c: static int verbose = 1;
    - static unsigned int colopts;
    - static enum help_format help_format = HELP_FORMAT_NONE;
    - static int exclude_guides;
    -+static int man_color = 1;
    - static struct option builtin_help_options[] = {
    - 	OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
    - 	OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
    +@@ builtin/help.c: enum help_format {
    + 	HELP_FORMAT_WEB
    + };
    + 
    ++static int man_color;
    + static const char *html_path;
    + 
    + static int show_all = 0;
     @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *page)
      	}
      }

 Documentation/config/color.txt | 11 +++++++++++
 builtin/help.c                 | 32 +++++++++++++++++++++++++++++++-
 color.h                        |  1 +
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
index e05d520a867..2f12ae3386d 100644
--- a/Documentation/config/color.txt
+++ b/Documentation/config/color.txt
@@ -126,6 +126,12 @@ color.interactive.<slot>::
 	or `error`, for four distinct types of normal output from
 	interactive commands.
 
+color.man::
+	This flag can be used to enable the automatic colorizaton of man
+	pages when using the less pager, `false` by default. When set to
+	`true` it's activated only when `color.ui` allows it, and if
+	`color.pager` enable (which it is by default).
+
 color.pager::
 	A boolean to specify whether `auto` color modes should colorize
 	output going to the pager. Defaults to true; set this to false
@@ -200,3 +206,8 @@ color.ui::
 	output not intended for machine consumption to use color, to
 	`true` or `auto` (this is the default since Git 1.8.4) if you
 	want such output to use color when written to the terminal.
++
+When set to `true` certain other `color.*` variables may still not be
+turned on unless explicitly enabled. Currently this only applies to
+`color.man`, see above. Such opt-in variables may be moved under the
+default `color.ui` umbrella in the future.
diff --git a/builtin/help.c b/builtin/help.c
index bb339f0fc80..c607da88d78 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -11,6 +11,7 @@
 #include "config-list.h"
 #include "help.h"
 #include "alias.h"
+#include "color.h"
 
 #ifndef DEFAULT_HELP_FORMAT
 #define DEFAULT_HELP_FORMAT "man"
@@ -34,6 +35,7 @@ enum help_format {
 	HELP_FORMAT_WEB
 };
 
+static int man_color;
 static const char *html_path;
 
 static int show_all = 0;
@@ -253,10 +255,33 @@ static void exec_man_konqueror(const char *path, const char *page)
 	}
 }
 
+static void colorize_man(void)
+{
+	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
+		return;
+
+	/* Disable groff colors */
+	setenv("GROFF_NO_SGR", "1", 0);
+
+	/* Bold */
+	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
+	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
+
+	/* Underline */
+	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
+	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
+
+	/* Standout */
+	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
+	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
+}
+
 static void exec_man_man(const char *path, const char *page)
 {
 	if (!path)
 		path = "man";
+
+	colorize_man();
 	execlp(path, "man", page, (char *)NULL);
 	warning_errno(_("failed to exec '%s'"), path);
 }
@@ -264,6 +289,7 @@ static void exec_man_man(const char *path, const char *page)
 static void exec_man_cmd(const char *cmd, const char *page)
 {
 	struct strbuf shell_cmd = STRBUF_INIT;
+	colorize_man();
 	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
 	execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
 	warning(_("failed to exec '%s'"), cmd);
@@ -371,8 +397,12 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	}
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
+	if (!strcmp(var, "color.man")) {
+		man_color = git_config_bool(var, value);
+		return 0;
+	}
 
-	return git_default_config(var, value, cb);
+	return git_color_default_config(var, value, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/color.h b/color.h
index 98894d6a175..d012add4e8a 100644
--- a/color.h
+++ b/color.h
@@ -51,6 +51,7 @@ struct strbuf;
 #define GIT_COLOR_FAINT		"\033[2m"
 #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
 #define GIT_COLOR_REVERSE	"\033[7m"
+#define GIT_COLOR_UNDERLINE	"\033[4m"
 
 /* A special value meaning "no color selected" */
 #define GIT_COLOR_NIL "NIL"
-- 
2.32.0.599.g672d808136f


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
@ 2021-06-21 10:17       ` Phillip Wood
  2021-06-21 10:28       ` Junio C Hamano
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 22+ messages in thread
From: Phillip Wood @ 2021-06-21 10:17 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, git
  Cc: Junio C Hamano, Felipe Contreras, Jeff King

On 21/06/2021 09:34, Ævar Arnfjörð Bjarmason wrote:
> From: Felipe Contreras <felipe.contreras@gmail.com>
> 
> We already colorize tools traditionally not colorized by default, like
> diff and grep. Let's do the same for man, but only if `color.man` is
> explicitly set to "true".
> 
> Unlike other `color.*` output this colorization is not enabled by
> `color.ui` being true, the user needs to explicitly set the
> `color.man` variable to `true.
> 
> When it was proposed to treat `color.man` like any other `color.*`
> variable some thought that git opting in coloring for an external
> program such as man(1) was a step too far[1], even if the user invoked
> it via the "git help <topic>" wrapper.
> 
> So let's make this explicitly opt-in for now. As noted in the
> documentation we're leaving ourselves an out to turn this on by
> default in the future, or e.g. putting it under the
> feature.experimental umbrella. We probably won't, but let's not
> promise users that `color.man` will forever be a special-case.
> 
> As for what this actually does the effect of having this enabled is
> that a documentation blurb like (some parts elided with "[...]"):
> 
> 	NAME
> 	----
> 	git-config - Get and set [...]
> 
> 	SYNOPSIS
> 	--------
> 	[...]
> 	'git config' [<file-option>] [...]
> 	[...]
> 	The `--type=<type>` option instructs 'git config' to ensure [...]
> 
> Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
> config" and other '-quoted parts in BLUE UNDERLINE instead of
> UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
> instead of BOLD. The "Standout" setting is then used for the user's
> own search bar (invoked with "/") and prompt. See [2] for more
> examples
> 
> Normally check_auto_color() would check the value of `color.pager`, but
> in this particular case it's not git the one executing the pager, but
> man. Therefore we need to check pager_use_color ourselves.
> 
> We do not need to support `color.man` being set to `always`; The `git
> help` command is always run for a tty (it would be very strange for a
> user to do `git help $page > output`, but in fact, that works anyway,
> we don't even need to check if stdout is a tty, but just to be
> consistent we do). So it's simply a boolean in our case.
> 
> So, in order for this change to have any effect:
> 
>   1. color.man=true must be set in the config
>   2. The user must use less
>   3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>   4. Have color.ui enabled
>   5. Not have color.pager disabled
>   6. Not have git with stdout directed to a file
> 
> 1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
> 2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147

Thanks for taking the time to write such a clear and comprehensive 
message, I think making this setting opt in for now makes sense. I've 
left a couple of comments below.

> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
> 
> On Tue, Jun 08 2021, Junio C Hamano wrote:
> 
>> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>>
>>> I've been running with this on my personal git build since May 26th. I
>>> haven't had any issues with it, and I like the new coloring.
>>> ...
>>> I think this is a good example of a change that we're better off just
>>> merging down and then reverting if the wider audience of git users hates
>>> it, rather than trying to come to some perfect consensus here
>>> on-list.
>>
>> My impression was tht we already had a rough consensus here on-list
>> that it may be good to educate users who like this "new coloring"
>> like you do to configure their "less", so that they consistently get
>> the "new coloring" they like whether they are doing "git help git",
>> "man git", or even "man ls", and the approach the posted patch takes
>> will not help (it only affects "git help git" among these).
>>
>> I'd rather not to take it.
> 
> Fair enough, here's a version I think you and others will find
> acceptable then. It allows users like me who like this to explicitly
> opt-in via color.man=true.
> 
> I also took the liberty of making some changes to the commit message
> to reword it for this new behavior, and to show an example of the sort
> of colors that change with this patch.
> 
> The only change to the patch itself is that now color_man is set to 0
> by default, not 1. I also moved its declaration so that it's with the
> one other config variable, insted of with the other command-line
> options to "git help".
> 
> Range-diff against v6:
> 1:  e021ca1da21 ! 1:  a950ef49e28 help: colorize man pages
>      @@ Metadata
>       Author: Felipe Contreras <felipe.contreras@gmail.com>
>       
>        ## Commit message ##
>      -    help: colorize man pages
>      +    help: colorize man pages if man.color=true under less(1)
>       
>           We already colorize tools traditionally not colorized by default, like
>      -    diff and grep. Let's do the same for man.
>      -
>      -    Our man pages don't contain many useful colors (just blue links),
>      -    moreover, many people have groff SGR disabled, so they don't see any
>      -    colors with man pages.
>      -
>      -    We can set LESS_TERMCAP variables to render bold and underlined text
>      -    with colors in the pager; a common trick[1].
>      -
>      -    Bold is rendered as red, underlined as blue, and standout (prompt and
>      -    highlighted search) as inverse cyan.
>      -
>      -    Obviously this only works when the less pager is used.
>      -
>      -    If the user already has LESS_TERMCAP variables set in his/her
>      -    environment, those are respected and nothing changes.
>      -
>      -    A new color configuration is added: `color.man` for the people that want
>      -    to turn this feature off, otherwise `color.ui` is respected.
>      -    Additionally, if color.pager is not enabled, this is disregarded.
>      +    diff and grep. Let's do the same for man, but only if `color.man` is
>      +    explicitly set to "true".
>      +
>      +    Unlike other `color.*` output this colorization is not enabled by
>      +    `color.ui` being true, the user needs to explicitly set the
>      +    `color.man` variable to `true.
>      +
>      +    When it was proposed to treat `color.man` like any other `color.*`
>      +    variable some thought that git opting in coloring for an external
>      +    program such as man(1) was a step too far[1], even if the user invoked
>      +    it via the "git help <topic>" wrapper.
>      +
>      +    So let's make this explicitly opt-in for now. As noted in the
>      +    documentation we're leaving ourselves an out to turn this on by
>      +    default in the future, or e.g. putting it under the
>      +    feature.experimental umbrella. We probably won't, but let's not
>      +    promise users that `color.man` will forever be a special-case.
>      +
>      +    As for what this actually does the effect of having this enabled is
>      +    that a documentation blurb like (some parts elided with "[...]"):
>      +
>      +            NAME
>      +            ----
>      +            git-config - Get and set [...]
>      +
>      +            SYNOPSIS
>      +            --------
>      +            [...]
>      +            'git config' [<file-option>] [...]
>      +            [...]
>      +            The `--type=<type>` option instructs 'git config' to ensure [...]
>      +
>      +    Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
>      +    config" and other '-quoted parts in BLUE UNDERLINE instead of
>      +    UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
>      +    instead of BOLD. The "Standout" setting is then used for the user's
>      +    own search bar (invoked with "/") and prompt. See [2] for more
>      +    examples
>       
>           Normally check_auto_color() would check the value of `color.pager`, but
>           in this particular case it's not git the one executing the pager, but
>           man. Therefore we need to check pager_use_color ourselves.
>       
>      -    Also--unlike other color.* configurations--color.man=always does not
>      -    make any sense here; `git help` is always run for a tty (it would be very
>      -    strange for a user to do `git help $page > output`, but in fact, that
>      -    works anyway, we don't even need to check if stdout is a tty, but just
>      -    to be consistent we do). So it's simply a boolean in our case.
>      +    We do not need to support `color.man` being set to `always`; The `git
>      +    help` command is always run for a tty (it would be very strange for a
>      +    user to do `git help $page > output`, but in fact, that works anyway,
>      +    we don't even need to check if stdout is a tty, but just to be
>      +    consistent we do). So it's simply a boolean in our case.
>       
>           So, in order for this change to have any effect:
>       
>      -     1. The user must use less
>      -     2. Not have the same LESS_TERMCAP variables set
>      -     3. Have color.ui enabled
>      -     4. Not have color.pager disabled
>      -     5. Not have color.man disabled
>      -     7. Not have git with stdout directed to a file
>      -
>      -    Fortunately the vast majority of our users meet all of the above, and
>      -    anybody who doesn't would not be affected negatively (plus very likely
>      -    comprises a very tiny minority).
>      +     1. color.man=true must be set in the config
>      +     2. The user must use less
>      +     3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>      +     4. Have color.ui enabled
>      +     5. Not have color.pager disabled
>      +     6. Not have git with stdout directed to a file
>       
>      -    [1] https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>      +    1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
>      +    2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>       
>           Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>      -    Phillip Wood <phillip.wood123@gmail.com>
>      -    Comments-by: Jeff King <peff@peff.net>
>           Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
>      +    Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>       
>        ## Documentation/config/color.txt ##
>       @@ Documentation/config/color.txt: color.interactive.<slot>::
>      @@ Documentation/config/color.txt: color.interactive.<slot>::
>        	interactive commands.
>        
>       +color.man::
>      -+	This flag can be used to disable the automatic colorizaton of man
>      -+	pages when using the less pager. It's activated only when color.ui
>      -+	allows it, and also when color.pager is on. (`true` by default).
>      ++	This flag can be used to enable the automatic colorizaton of man
>      ++	pages when using the less pager, `false` by default. When set to
>      ++	`true` it's activated only when `color.ui` allows it, and if
>      ++	`color.pager` enable (which it is by default).
>       +
>        color.pager::
>      - 	A boolean to enable/disable colored output when the pager is in
>      - 	use (default is true).
>      + 	A boolean to specify whether `auto` color modes should colorize
>      + 	output going to the pager. Defaults to true; set this to false
>      +@@ Documentation/config/color.txt: color.ui::
>      + 	output not intended for machine consumption to use color, to
>      + 	`true` or `auto` (this is the default since Git 1.8.4) if you
>      + 	want such output to use color when written to the terminal.
>      +++
>      ++When set to `true` certain other `color.*` variables may still not be
>      ++turned on unless explicitly enabled. Currently this only applies to
>      ++`color.man`, see above. Such opt-in variables may be moved under the
>      ++default `color.ui` umbrella in the future.
>       
>        ## builtin/help.c ##
>       @@
>      @@ builtin/help.c
>        
>        #ifndef DEFAULT_HELP_FORMAT
>        #define DEFAULT_HELP_FORMAT "man"
>      -@@ builtin/help.c: static int verbose = 1;
>      - static unsigned int colopts;
>      - static enum help_format help_format = HELP_FORMAT_NONE;
>      - static int exclude_guides;
>      -+static int man_color = 1;
>      - static struct option builtin_help_options[] = {
>      - 	OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
>      - 	OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
>      +@@ builtin/help.c: enum help_format {
>      + 	HELP_FORMAT_WEB
>      + };
>      +
>      ++static int man_color;
>      + static const char *html_path;
>      +
>      + static int show_all = 0;
>       @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *page)
>        	}
>        }
> 
>   Documentation/config/color.txt | 11 +++++++++++
>   builtin/help.c                 | 32 +++++++++++++++++++++++++++++++-
>   color.h                        |  1 +
>   3 files changed, 43 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
> index e05d520a867..2f12ae3386d 100644
> --- a/Documentation/config/color.txt
> +++ b/Documentation/config/color.txt
> @@ -126,6 +126,12 @@ color.interactive.<slot>::
>   	or `error`, for four distinct types of normal output from
>   	interactive commands.
>   
> +color.man::
> +	This flag can be used to enable the automatic colorizaton of man
> +	pages when using the less pager, `false` by default. When set to
> +	`true` it's activated only when `color.ui` allows it, and if
> +	`color.pager` enable (which it is by default).

s/and if `color.pager` enable/and `color.pager` is enabled/?

> +
>   color.pager::
>   	A boolean to specify whether `auto` color modes should colorize
>   	output going to the pager. Defaults to true; set this to false
> @@ -200,3 +206,8 @@ color.ui::
>   	output not intended for machine consumption to use color, to
>   	`true` or `auto` (this is the default since Git 1.8.4) if you
>   	want such output to use color when written to the terminal.
> ++
> +When set to `true` certain other `color.*` variables may still not be
> +turned on unless explicitly enabled. Currently this only applies to
> +`color.man`, see above. Such opt-in variables may be moved under the
> +default `color.ui` umbrella in the future.
> diff --git a/builtin/help.c b/builtin/help.c
> index bb339f0fc80..c607da88d78 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -11,6 +11,7 @@
>   #include "config-list.h"
>   #include "help.h"
>   #include "alias.h"
> +#include "color.h"
>   
>   #ifndef DEFAULT_HELP_FORMAT
>   #define DEFAULT_HELP_FORMAT "man"
> @@ -34,6 +35,7 @@ enum help_format {
>   	HELP_FORMAT_WEB
>   };
>   
> +static int man_color;
>   static const char *html_path;
>   
>   static int show_all = 0;
> @@ -253,10 +255,33 @@ static void exec_man_konqueror(const char *path, const char *page)
>   	}
>   }
>   
> +static void colorize_man(void)
> +{
> +	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
> +		return;
> +
> +	/* Disable groff colors */
> +	setenv("GROFF_NO_SGR", "1", 0);
> +
> +	/* Bold */
> +	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
> +	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);

We take care not to override the user's preference if these variables 
are set individually, but there is a possible corner case where the user 
sets a subset of these LESS_TERMCAP_* because they like the defaults for 
some of them but not others, in that case we wouldn't want to set any of 
them. Whether this is likely to be a problem in practice or not I'm not 
sure. Version 5 [1] of this patch took care not to override the users 
settings in this case.

Best Wishes

Phillip

[1] 
https://lore.kernel.org/git/20210522011718.541986-1-felipe.contreras@gmail.com/

> +
> +	/* Underline */
> +	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
> +	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
> +
> +	/* Standout */
> +	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
> +	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
> +}
> +
>   static void exec_man_man(const char *path, const char *page)
>   {
>   	if (!path)
>   		path = "man";
> +
> +	colorize_man();
>   	execlp(path, "man", page, (char *)NULL);
>   	warning_errno(_("failed to exec '%s'"), path);
>   }
> @@ -264,6 +289,7 @@ static void exec_man_man(const char *path, const char *page)
>   static void exec_man_cmd(const char *cmd, const char *page)
>   {
>   	struct strbuf shell_cmd = STRBUF_INIT;
> +	colorize_man();
>   	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
>   	execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
>   	warning(_("failed to exec '%s'"), cmd);
> @@ -371,8 +397,12 @@ static int git_help_config(const char *var, const char *value, void *cb)
>   	}
>   	if (starts_with(var, "man."))
>   		return add_man_viewer_info(var, value);
> +	if (!strcmp(var, "color.man")) {
> +		man_color = git_config_bool(var, value);
> +		return 0;
> +	}
>   
> -	return git_default_config(var, value, cb);
> +	return git_color_default_config(var, value, cb);
>   }
>   
>   static struct cmdnames main_cmds, other_cmds;
> diff --git a/color.h b/color.h
> index 98894d6a175..d012add4e8a 100644
> --- a/color.h
> +++ b/color.h
> @@ -51,6 +51,7 @@ struct strbuf;
>   #define GIT_COLOR_FAINT		"\033[2m"
>   #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
>   #define GIT_COLOR_REVERSE	"\033[7m"
> +#define GIT_COLOR_UNDERLINE	"\033[4m"
>   
>   /* A special value meaning "no color selected" */
>   #define GIT_COLOR_NIL "NIL"
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
  2021-06-21 10:17       ` Phillip Wood
@ 2021-06-21 10:28       ` Junio C Hamano
  2021-06-21 18:41         ` Felipe Contreras
  2021-06-21 19:08         ` Ævar Arnfjörð Bjarmason
  2021-06-21 15:59       ` Felipe Contreras
  2021-06-24  0:08       ` Jeff King
  3 siblings, 2 replies; 22+ messages in thread
From: Junio C Hamano @ 2021-06-21 10:28 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: git, Felipe Contreras, Jeff King, Phillip Wood

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> So, in order for this change to have any effect:
>
>  1. color.man=true must be set in the config
>  2. The user must use less
>  3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>  4. Have color.ui enabled
>  5. Not have color.pager disabled
>  6. Not have git with stdout directed to a file
>
> 1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
> 2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>
> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>
> On Tue, Jun 08 2021, Junio C Hamano wrote:
>
>> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>>
>>> I've been running with this on my personal git build since May 26th. I
>>> haven't had any issues with it, and I like the new coloring.
>>> ...
>>> I think this is a good example of a change that we're better off just
>>> merging down and then reverting if the wider audience of git users hates
>>> it, rather than trying to come to some perfect consensus here
>>> on-list.
>>
>> My impression was tht we already had a rough consensus here on-list
>> that it may be good to educate users who like this "new coloring"
>> like you do to configure their "less", so that they consistently get
>> the "new coloring" they like whether they are doing "git help git",
>> "man git", or even "man ls", and the approach the posted patch takes
>> will not help (it only affects "git help git" among these).
>>
>> I'd rather not to take it.
>
> Fair enough, here's a version I think you and others will find
> acceptable then. It allows users like me who like this to explicitly
> opt-in via color.man=true.

Not really.

Since the implementation of the posted patch, as I understand it,
does not aim to affect both "git help -m foo" and "man git-foo"
identically, I think it would be easier to understand to end-users
if this were exposed as a new "mode", like "git help --web" and "git
help --info" are different modes from the "git help --man",
something like "git help --fancy-man" (or whatever is easy to type
and explain, and also add it to the variants help.format knows about
to make it easy to set the default).

One advantage of doing so is that we do not have to worry about "ah,
user has LESS_BLAH environment variable so we should disable this
new mode here" etc.  As long as the new mode is requested either via
the command line option or help.format configuration, it can
completely take it over.  That simplifies the necessary explanation
given to the users quite a lot, no?

> 	----
> 	git-config - Get and set [...]
>
> 	SYNOPSIS
> 	--------
> 	[...]
> 	'git config' [<file-option>] [...]
> 	[...]
> 	The `--type=<type>` option instructs 'git config' to ensure [...]
>
> Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
> config" and other '-quoted parts in BLUE UNDERLINE instead of
> UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
> instead of BOLD. The "Standout" setting is then used for the user's
> own search bar (invoked with "/") and prompt. See [2] for more
> examples

There are BOLD RED and READ BOLD; are they differently rendered?

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
  2021-06-21 10:17       ` Phillip Wood
  2021-06-21 10:28       ` Junio C Hamano
@ 2021-06-21 15:59       ` Felipe Contreras
  2021-06-24  0:08       ` Jeff King
  3 siblings, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-21 15:59 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, git
  Cc: Junio C Hamano, Felipe Contreras, Jeff King, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Ævar Arnfjörð Bjarmason wrote:
> From: Felipe Contreras <felipe.contreras@gmail.com>
> 
> We already colorize tools traditionally not colorized by default, like
> diff and grep. Let's do the same for man, but only if `color.man` is
> explicitly set to "true".
> 
> Unlike other `color.*` output this colorization is not enabled by
> `color.ui` being true, the user needs to explicitly set the
> `color.man` variable to `true.
> 
> When it was proposed to treat `color.man` like any other `color.*`
> variable some thought that git opting in coloring for an external
> program such as man(1) was a step too far[1], even if the user invoked
> it via the "git help <topic>" wrapper.
> 
> So let's make this explicitly opt-in for now. As noted in the
> documentation we're leaving ourselves an out to turn this on by
> default in the future, or e.g. putting it under the
> feature.experimental umbrella. We probably won't, but let's not
> promise users that `color.man` will forever be a special-case.
> 
> As for what this actually does the effect of having this enabled is
> that a documentation blurb like (some parts elided with "[...]"):
> 
> 	NAME
> 	----
> 	git-config - Get and set [...]
> 
> 	SYNOPSIS
> 	--------
> 	[...]
> 	'git config' [<file-option>] [...]
> 	[...]
> 	The `--type=<type>` option instructs 'git config' to ensure [...]
> 
> Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
> config" and other '-quoted parts in BLUE UNDERLINE instead of
> UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
> instead of BOLD. The "Standout" setting is then used for the user's
> own search bar (invoked with "/") and prompt. See [2] for more
> examples
> 
> Normally check_auto_color() would check the value of `color.pager`, but
> in this particular case it's not git the one executing the pager, but
> man. Therefore we need to check pager_use_color ourselves.
> 
> We do not need to support `color.man` being set to `always`; The `git
> help` command is always run for a tty (it would be very strange for a
> user to do `git help $page > output`, but in fact, that works anyway,
> we don't even need to check if stdout is a tty, but just to be
> consistent we do). So it's simply a boolean in our case.
> 
> So, in order for this change to have any effect:
> 
>  1. color.man=true must be set in the config
>  2. The user must use less
>  3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>  4. Have color.ui enabled
>  5. Not have color.pager disabled
>  6. Not have git with stdout directed to a file
> 
> 1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
> 2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
> 
> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>

Looks good to me.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21 10:28       ` Junio C Hamano
@ 2021-06-21 18:41         ` Felipe Contreras
  2021-06-21 19:08         ` Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-21 18:41 UTC (permalink / raw)
  To: Junio C Hamano, Ævar Arnfjörð Bjarmason
  Cc: git, Felipe Contreras, Jeff King, Phillip Wood

Junio C Hamano wrote:
> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> > Fair enough, here's a version I think you and others will find
> > acceptable then. It allows users like me who like this to explicitly
> > opt-in via color.man=true.
> 
> Not really.
> 
> Since the implementation of the posted patch, as I understand it,
> does not aim to affect both "git help -m foo" and "man git-foo"
> identically,

It cannot aim to do what is not possible.

> I think it would be easier to understand to end-users
> if this were exposed as a new "mode", like "git help --web" and "git
> help --info" are different modes from the "git help --man",
> something like "git help --fancy-man" (or whatever is easy to type
> and explain, and also add it to the variants help.format knows about
> to make it easy to set the default).

But it is not a new mode.

I presume you mean format, since man, info, and web are formats,
controlled by help.format.

But no, it's not a format either, because we still want to see the same
format (man).

Perhaps you meant a man viewer (controlled with man.viewer), but there's
no command line option to launch help with for example emacs woman.

But still, it's not a new viewer; it's an improvement of an already
existing viewer.

> One advantage of doing so is that we do not have to worry about "ah,
> user has LESS_BLAH environment variable so we should disable this
> new mode here" etc.

We don't have to worry about that with the current patch.

> As long as the new mode is requested either via
> the command line option or help.format configuration, it can
> completely take it over.

That is already the case.

> That simplifies the necessary explanation given to the users quite a
> lot, no?

No, it would still be:

 1. man.viewer=fancyman must be set in the config
 2. The user must use less
 3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
 4. Have color.ui enabled
 5. Not have color.pager disabled
 6. Not have git with stdout directed to a file

Moreover, this explanation is for developers.

Realistically all the user needs to know is that color.man=true turns
this on (man.viewer=fancyman is not better in any way).

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21 10:28       ` Junio C Hamano
  2021-06-21 18:41         ` Felipe Contreras
@ 2021-06-21 19:08         ` Ævar Arnfjörð Bjarmason
  2021-06-23 23:58           ` Jeff King
  1 sibling, 1 reply; 22+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-06-21 19:08 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Felipe Contreras, Jeff King, Phillip Wood


On Mon, Jun 21 2021, Junio C Hamano wrote:

> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
>
>> So, in order for this change to have any effect:
>>
>>  1. color.man=true must be set in the config
>>  2. The user must use less
>>  3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>>  4. Have color.ui enabled
>>  5. Not have color.pager disabled
>>  6. Not have git with stdout directed to a file
>>
>> 1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
>> 2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
>>
>> Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
>> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>> ---
>>
>> On Tue, Jun 08 2021, Junio C Hamano wrote:
>>
>>> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>>>
>>>> I've been running with this on my personal git build since May 26th. I
>>>> haven't had any issues with it, and I like the new coloring.
>>>> ...
>>>> I think this is a good example of a change that we're better off just
>>>> merging down and then reverting if the wider audience of git users hates
>>>> it, rather than trying to come to some perfect consensus here
>>>> on-list.
>>>
>>> My impression was tht we already had a rough consensus here on-list
>>> that it may be good to educate users who like this "new coloring"
>>> like you do to configure their "less", so that they consistently get
>>> the "new coloring" they like whether they are doing "git help git",
>>> "man git", or even "man ls", and the approach the posted patch takes
>>> will not help (it only affects "git help git" among these).
>>>
>>> I'd rather not to take it.
>>
>> Fair enough, here's a version I think you and others will find
>> acceptable then. It allows users like me who like this to explicitly
>> opt-in via color.man=true.
>
> Not really.
>
> [snip] I think it would be easier to understand to end-users
> if this were exposed as a new "mode", like "git help --web" and "git
> help --info" are different modes from the "git help --man",
> something like "git help --fancy-man" (or whatever is easy to type
> and explain, and also add it to the variants help.format knows about
> to make it easy to set the default).
>
> One advantage of doing so is that we do not have to worry about "ah,
> user has LESS_BLAH environment variable so we should disable this
> new mode here" etc.  As long as the new mode is requested either via
> the command line option or help.format configuration, it can
> completely take it over.  That simplifies the necessary explanation
> given to the users quite a lot, no?

The interaction between "git help" and "man"/"less" doesn't really have
an equivalent in the rest of git as far as color output goes. Usually we
emit colors via our own programs.

But no, I think it makes the most sense to consider this orthagonal to
help.format=man or man.viewer=<cmd>.

We're not invoking a different man viewer or command, we're just
expecting that mode to invoke the pager, and if that pager is less to
have these variables tweak our color preferences.

> [unsnip] Since the implementation of the posted patch, as I understand it,
> does not aim to affect both "git help -m foo" and "man git-foo"
> identically, 

Aside from this patch I don't think it makes sense to view git's UI and
interaction with the pager like that.

To probably >95% of our users the "we invoke the pager" is just a
technical implementation details they're not aware of. Git just has
pretty colors by default, so I don't think it's jarring that "git help
xyz" and "man git-xyz" look different.

We also don't try to maintain the UI that:

    git cmd >file &&
    pager file

Gives you the same UX as:

    # Invokes pager(1) for you
    git cmd

Since we set e.g. PAGER_ENV already, I believe this was brought up in
past discussions. So we're already past the point of git adding its own
magic custom options to feed to the pager.

So I don't see the problem with having "a bit more like PAGER_ENV"
hidden behind a color.man=true config option in this case.

>> 	----
>> 	git-config - Get and set [...]
>>
>> 	SYNOPSIS
>> 	--------
>> 	[...]
>> 	'git config' [<file-option>] [...]
>> 	[...]
>> 	The `--type=<type>` option instructs 'git config' to ensure [...]
>>
>> Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
>> config" and other '-quoted parts in BLUE UNDERLINE instead of
>> UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
>> instead of BOLD. The "Standout" setting is then used for the user's
>> own search bar (invoked with "/") and prompt. See [2] for more
>> examples
>
> There are BOLD RED and READ BOLD; are they differently rendered?

Yes, that's just an omission/mistake. It should be the "RED BOLD",
i.e. "<COLOR> <ATTR>".

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21 19:08         ` Ævar Arnfjörð Bjarmason
@ 2021-06-23 23:58           ` Jeff King
  2021-06-24  1:03             ` Felipe Contreras
  2021-06-28 17:37             ` Junio C Hamano
  0 siblings, 2 replies; 22+ messages in thread
From: Jeff King @ 2021-06-23 23:58 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Junio C Hamano, git, Felipe Contreras, Phillip Wood

On Mon, Jun 21, 2021 at 09:08:20PM +0200, Ævar Arnfjörð Bjarmason wrote:

> > [snip] I think it would be easier to understand to end-users
> > if this were exposed as a new "mode", like "git help --web" and "git
> > help --info" are different modes from the "git help --man",
> > something like "git help --fancy-man" (or whatever is easy to type
> > and explain, and also add it to the variants help.format knows about
> > to make it easy to set the default).
> >
> > One advantage of doing so is that we do not have to worry about "ah,
> > user has LESS_BLAH environment variable so we should disable this
> > new mode here" etc.  As long as the new mode is requested either via
> > the command line option or help.format configuration, it can
> > completely take it over.  That simplifies the necessary explanation
> > given to the users quite a lot, no?
> 
> The interaction between "git help" and "man"/"less" doesn't really have
> an equivalent in the rest of git as far as color output goes. Usually we
> emit colors via our own programs.
> 
> But no, I think it makes the most sense to consider this orthagonal to
> help.format=man or man.viewer=<cmd>.
> 
> We're not invoking a different man viewer or command, we're just
> expecting that mode to invoke the pager, and if that pager is less to
> have these variables tweak our color preferences.

FWIW, if we are going to do this, then just having it as "color.man"
makes the most sense to me. It is easily explained as "when we invoke
man, set up some environment variables that may enable colors in the
output".

I'm still entirely unconvinced that this should be in Git at all;
pointing GIT_MAN_VIEWER or man.*.cmd at a color-man wrapper seems like
it would be sufficient. But it feels like that conversation was not
going anywhere productive; I mention it here only to indicate that my
response above is not an endorsement of the concept.

-Peff

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
                         ` (2 preceding siblings ...)
  2021-06-21 15:59       ` Felipe Contreras
@ 2021-06-24  0:08       ` Jeff King
  2021-06-29  1:42         ` Junio C Hamano
  3 siblings, 1 reply; 22+ messages in thread
From: Jeff King @ 2021-06-24  0:08 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: git, Junio C Hamano, Felipe Contreras, Phillip Wood

On Mon, Jun 21, 2021 at 10:34:00AM +0200, Ævar Arnfjörð Bjarmason wrote:

> diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
> index e05d520a867..2f12ae3386d 100644
> --- a/Documentation/config/color.txt
> +++ b/Documentation/config/color.txt
> @@ -126,6 +126,12 @@ color.interactive.<slot>::
>  	or `error`, for four distinct types of normal output from
>  	interactive commands.
>  
> +color.man::
> +	This flag can be used to enable the automatic colorizaton of man
> +	pages when using the less pager, `false` by default. When set to
> +	`true` it's activated only when `color.ui` allows it, and if
> +	`color.pager` enable (which it is by default).

A few typos here:

diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
index 2f12ae3386..fcc12df508 100644
--- a/Documentation/config/color.txt
+++ b/Documentation/config/color.txt
@@ -127,10 +127,10 @@ color.interactive.<slot>::
 	interactive commands.
 
 color.man::
-	This flag can be used to enable the automatic colorizaton of man
+	This flag can be used to enable the automatic colorization of man
 	pages when using the less pager, `false` by default. When set to
 	`true` it's activated only when `color.ui` allows it, and if
-	`color.pager` enable (which it is by default).
+	`color.pager` is enabled (which it is by default).
 
 color.pager::
 	A boolean to specify whether `auto` color modes should colorize

The interaction with color.ui seems unusual. Normally it is not a
gate-keeper for specific colorizations, but rather a fallback when
more-specific color config is unspecified. E.g.:

  [color]
  ui = false
  branch = true

would colorize branch output, but nothing else. But from your
description (and I think the code matches this), doing:

  [color]
  ui = false
  man = true

would still disable the man-colors. So there's no way to enable this
feature without enabling colors everywhere else. I think it should
simply be independent of color.ui (with the exception that it may
eventually use it as a fallback like all the other color.* booleans,
_if_ we want to move it to default-to-on).

-Peff

^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-23 23:58           ` Jeff King
@ 2021-06-24  1:03             ` Felipe Contreras
  2021-06-28 17:37             ` Junio C Hamano
  1 sibling, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-24  1:03 UTC (permalink / raw)
  To: Jeff King, Ævar Arnfjörð Bjarmason
  Cc: Junio C Hamano, git, Felipe Contreras, Phillip Wood

Jeff King wrote:
> On Mon, Jun 21, 2021 at 09:08:20PM +0200, Ævar Arnfjörð Bjarmason wrote:
> 
> > > [snip] I think it would be easier to understand to end-users
> > > if this were exposed as a new "mode", like "git help --web" and "git
> > > help --info" are different modes from the "git help --man",
> > > something like "git help --fancy-man" (or whatever is easy to type
> > > and explain, and also add it to the variants help.format knows about
> > > to make it easy to set the default).
> > >
> > > One advantage of doing so is that we do not have to worry about "ah,
> > > user has LESS_BLAH environment variable so we should disable this
> > > new mode here" etc.  As long as the new mode is requested either via
> > > the command line option or help.format configuration, it can
> > > completely take it over.  That simplifies the necessary explanation
> > > given to the users quite a lot, no?
> > 
> > The interaction between "git help" and "man"/"less" doesn't really have
> > an equivalent in the rest of git as far as color output goes. Usually we
> > emit colors via our own programs.
> > 
> > But no, I think it makes the most sense to consider this orthagonal to
> > help.format=man or man.viewer=<cmd>.
> > 
> > We're not invoking a different man viewer or command, we're just
> > expecting that mode to invoke the pager, and if that pager is less to
> > have these variables tweak our color preferences.
> 
> FWIW, if we are going to do this, then just having it as "color.man"
> makes the most sense to me. It is easily explained as "when we invoke
> man, set up some environment variables that may enable colors in the
> output".
> 
> I'm still entirely unconvinced that this should be in Git at all;

That's OK, you don't need to be convinced for this change to be a
positive one.

> pointing GIT_MAN_VIEWER or man.*.cmd at a color-man wrapper seems like
> it would be sufficient.

What color-man wrapper?

> But it feels like that conversation was not going anywhere productive;

Feelings are not facts.

Bailing from a discussion doesn't resolve the discussion, and the
question "how is a user supposed to configure this properly?" remains
unanswered by you, or anyone [1].

This patch is the closest to a convenient solution anybody has come up
with.

If anybody has any other proposal it would be good to hear them.

[1] https://lore.kernel.org/git/60bfadc0aca09_1abb8f208fd@natae.notmuch/

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: [PATCH v6] help: colorize man pages
  2021-05-23  5:44 [PATCH v6] help: colorize man pages Felipe Contreras
  2021-05-24 13:13 ` Phillip Wood
  2021-06-08 12:35 ` Ævar Arnfjörð Bjarmason
@ 2021-06-24  1:44 ` Felipe Contreras
  2021-06-26  2:50 ` [PATCH v8] help: add option to colorize man pages under less Felipe Contreras
  3 siblings, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-24  1:44 UTC (permalink / raw)
  To: Felipe Contreras, git
  Cc: Phillip Wood, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano, Felipe Contreras

> --- a/builtin/help.c
> +++ b/builtin/help.c

> @@ -264,6 +289,7 @@ static void exec_man_man(const char *path, const char *page)
>  static void exec_man_cmd(const char *cmd, const char *page)
>  {
>  	struct strbuf shell_cmd = STRBUF_INIT;
> +	colorize_man();

On further reflection I don't think this colorize belongs here.
exec_man_cmd() is meant to execute any custom command, not necessarily
man.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v8] help: add option to colorize man pages under less
  2021-05-23  5:44 [PATCH v6] help: colorize man pages Felipe Contreras
                   ` (2 preceding siblings ...)
  2021-06-24  1:44 ` [PATCH v6] help: colorize man pages Felipe Contreras
@ 2021-06-26  2:50 ` Felipe Contreras
  2021-06-26 14:49   ` Ævar Arnfjörð Bjarmason
  3 siblings, 1 reply; 22+ messages in thread
From: Felipe Contreras @ 2021-06-26  2:50 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano, Felipe Contreras

We already colorize tools traditionally not colorized by default--like
diff and grep. Let's do the same for man, but only if `color.man` is
explicitly set to "true".

Unlike other `color.*` output, this colorization is not enabled when
`color.ui` is true; the user needs to explicitly set the
`color.man` variable to `true.

When it was proposed to treat `color.man` like any other `color.*`
variable, some thought that git opting to add color for an external
program such as man(1) was a step too far [1]--even if the user invoked
it via the "git help <topic>" wrapper. So let's make this explicitly
opt-in for now.

As noted in the documentation we're leaving ourselves an out to turn
this on by default in the future, for example putting it under the
feature.experimental umbrella. We probably won't, but let's not promise
users that `color.man` will forever be a special-case.

As for what this actually does, the effect of having this enabled is
that a documentation blurb like (some parts elided with "[...]"):

	NAME
	----
	git-config - Get and set [...]

	SYNOPSIS
	--------
	[...]
	'git config' [<file-option>] [...]
	[...]
	The `--type=<type>` option instructs 'git config' to ensure [...]

Will have "NAME" and "SYNOPSIS" shown as RED BOLD instead of BOLD,
"git config" and other '-quoted parts in BLUE UNDERLINE instead of
UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
instead of BOLD. The "standout" setting is then used for the user's
own search bar (invoked with "/") and prompt. See [2] for more
examples

Normally check_auto_color() would check the value of `color.pager`, but
in this particular case it's not git the one executing the pager, but
man. Therefore we need to check pager_use_color ourselves.

We do not need to support `color.man` being set to `always`; the `git
help` command is always run for a tty (it would be very strange for a
user to do `git help $page > output`, but in fact, that works anyway,
we don't even need to check if stdout is a tty, but just to be
consistent we do). So it's simply a boolean in our case.

So, in order for this change to have any effect:

 1. color.man=true must be set in the config
 2. The user must use less
 3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
 4. Not have color.pager disabled
 5. Not have git with stdout directed to a file

1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147

Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---

This a reroll of Ævar's v7 with comments from Jeff King so that
color.ui=never doesn't disable color.man, and the documentation was
updated accordingly.

Additinally I removed one call to colorize_man() in exec_man_cmd() which
is not meant for the man command (although it could be used for that).

Plus a bunch of style changes to the commit message.

Range-diff against v7:
1:  167f5c8b39 ! 1:  1b3f7ee1aa help: colorize man pages if man.color=true under less(1)
    @@ Metadata
     Author: Felipe Contreras <felipe.contreras@gmail.com>
     
      ## Commit message ##
    -    help: colorize man pages if man.color=true under less(1)
    +    help: add option to colorize man pages under less
     
    -    We already colorize tools traditionally not colorized by default, like
    +    We already colorize tools traditionally not colorized by default--like
         diff and grep. Let's do the same for man, but only if `color.man` is
         explicitly set to "true".
     
    -    Unlike other `color.*` output this colorization is not enabled by
    -    `color.ui` being true, the user needs to explicitly set the
    +    Unlike other `color.*` output, this colorization is not enabled when
    +    `color.ui` is true; the user needs to explicitly set the
         `color.man` variable to `true.
     
         When it was proposed to treat `color.man` like any other `color.*`
    -    variable some thought that git opting in coloring for an external
    -    program such as man(1) was a step too far[1], even if the user invoked
    -    it via the "git help <topic>" wrapper.
    +    variable, some thought that git opting to add color for an external
    +    program such as man(1) was a step too far [1]--even if the user invoked
    +    it via the "git help <topic>" wrapper. So let's make this explicitly
    +    opt-in for now.
     
    -    So let's make this explicitly opt-in for now. As noted in the
    -    documentation we're leaving ourselves an out to turn this on by
    -    default in the future, or e.g. putting it under the
    -    feature.experimental umbrella. We probably won't, but let's not
    -    promise users that `color.man` will forever be a special-case.
    +    As noted in the documentation we're leaving ourselves an out to turn
    +    this on by default in the future, for example putting it under the
    +    feature.experimental umbrella. We probably won't, but let's not promise
    +    users that `color.man` will forever be a special-case.
     
    -    As for what this actually does the effect of having this enabled is
    +    As for what this actually does, the effect of having this enabled is
         that a documentation blurb like (some parts elided with "[...]"):
     
                 NAME
    @@ Commit message
                 [...]
                 The `--type=<type>` option instructs 'git config' to ensure [...]
     
    -    Will have "NAME" and "SECTION" shown as BOLD RED instead of BOLD, "git
    -    config" and other '-quoted parts in BLUE UNDERLINE instead of
    +    Will have "NAME" and "SYNOPSIS" shown as RED BOLD instead of BOLD,
    +    "git config" and other '-quoted parts in BLUE UNDERLINE instead of
         UNDERLINE, and `--type=<type>` and other `-quoted parts in RED BOLD
    -    instead of BOLD. The "Standout" setting is then used for the user's
    +    instead of BOLD. The "standout" setting is then used for the user's
         own search bar (invoked with "/") and prompt. See [2] for more
         examples
     
    @@ Commit message
         in this particular case it's not git the one executing the pager, but
         man. Therefore we need to check pager_use_color ourselves.
     
    -    We do not need to support `color.man` being set to `always`; The `git
    +    We do not need to support `color.man` being set to `always`; the `git
         help` command is always run for a tty (it would be very strange for a
         user to do `git help $page > output`, but in fact, that works anyway,
         we don't even need to check if stdout is a tty, but just to be
    @@ Commit message
          1. color.man=true must be set in the config
          2. The user must use less
          3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
    -     4. Have color.ui enabled
    -     5. Not have color.pager disabled
    -     6. Not have git with stdout directed to a file
    +     4. Not have color.pager disabled
    +     5. Not have git with stdout directed to a file
     
         1. https://lore.kernel.org/git/87tun1qp91.fsf@evledraar.gmail.com/
         2. https://unix.stackexchange.com/questions/119/colors-in-man-pages/147
     
         Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
    -    Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
    +    Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
     
      ## Documentation/config/color.txt ##
     @@ Documentation/config/color.txt: color.interactive.<slot>::
    @@ Documentation/config/color.txt: color.interactive.<slot>::
      	interactive commands.
      
     +color.man::
    -+	This flag can be used to enable the automatic colorizaton of man
    ++	This flag can be used to enable the automatic colorization of man
     +	pages when using the less pager, `false` by default. When set to
    -+	`true` it's activated only when `color.ui` allows it, and if
    -+	`color.pager` enable (which it is by default).
    ++	`true` it's activated only if `color.pager` is enabled (which it
    ++	is by default).
     +
      color.pager::
      	A boolean to specify whether `auto` color modes should colorize
    @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *pag
      
     +static void colorize_man(void)
     +{
    -+	if (!man_color || !want_color(GIT_COLOR_UNKNOWN) || !pager_use_color)
    ++	if (!man_color || !pager_use_color)
     +		return;
     +
     +	/* Disable groff colors */
    @@ builtin/help.c: static void exec_man_konqueror(const char *path, const char *pag
      	execlp(path, "man", page, (char *)NULL);
      	warning_errno(_("failed to exec '%s'"), path);
      }
    -@@ builtin/help.c: static void exec_man_man(const char *path, const char *page)
    - static void exec_man_cmd(const char *cmd, const char *page)
    - {
    - 	struct strbuf shell_cmd = STRBUF_INIT;
    -+	colorize_man();
    - 	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
    - 	execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
    - 	warning(_("failed to exec '%s'"), cmd);
     @@ builtin/help.c: static int git_help_config(const char *var, const char *value, void *cb)
      	}
      	if (starts_with(var, "man."))

 Documentation/config/color.txt | 11 +++++++++++
 builtin/help.c                 | 31 ++++++++++++++++++++++++++++++-
 color.h                        |  1 +
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
index e05d520a86..8511af1f1d 100644
--- a/Documentation/config/color.txt
+++ b/Documentation/config/color.txt
@@ -126,6 +126,12 @@ color.interactive.<slot>::
 	or `error`, for four distinct types of normal output from
 	interactive commands.
 
+color.man::
+	This flag can be used to enable the automatic colorization of man
+	pages when using the less pager, `false` by default. When set to
+	`true` it's activated only if `color.pager` is enabled (which it
+	is by default).
+
 color.pager::
 	A boolean to specify whether `auto` color modes should colorize
 	output going to the pager. Defaults to true; set this to false
@@ -200,3 +206,8 @@ color.ui::
 	output not intended for machine consumption to use color, to
 	`true` or `auto` (this is the default since Git 1.8.4) if you
 	want such output to use color when written to the terminal.
++
+When set to `true` certain other `color.*` variables may still not be
+turned on unless explicitly enabled. Currently this only applies to
+`color.man`, see above. Such opt-in variables may be moved under the
+default `color.ui` umbrella in the future.
diff --git a/builtin/help.c b/builtin/help.c
index bb339f0fc8..150dd05fdb 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -11,6 +11,7 @@
 #include "config-list.h"
 #include "help.h"
 #include "alias.h"
+#include "color.h"
 
 #ifndef DEFAULT_HELP_FORMAT
 #define DEFAULT_HELP_FORMAT "man"
@@ -34,6 +35,7 @@ enum help_format {
 	HELP_FORMAT_WEB
 };
 
+static int man_color;
 static const char *html_path;
 
 static int show_all = 0;
@@ -253,10 +255,33 @@ static void exec_man_konqueror(const char *path, const char *page)
 	}
 }
 
+static void colorize_man(void)
+{
+	if (!man_color || !pager_use_color)
+		return;
+
+	/* Disable groff colors */
+	setenv("GROFF_NO_SGR", "1", 0);
+
+	/* Bold */
+	setenv("LESS_TERMCAP_md", GIT_COLOR_BOLD_RED, 0);
+	setenv("LESS_TERMCAP_me", GIT_COLOR_RESET, 0);
+
+	/* Underline */
+	setenv("LESS_TERMCAP_us", GIT_COLOR_BLUE GIT_COLOR_UNDERLINE, 0);
+	setenv("LESS_TERMCAP_ue", GIT_COLOR_RESET, 0);
+
+	/* Standout */
+	setenv("LESS_TERMCAP_so", GIT_COLOR_CYAN GIT_COLOR_REVERSE, 0);
+	setenv("LESS_TERMCAP_se", GIT_COLOR_RESET, 0);
+}
+
 static void exec_man_man(const char *path, const char *page)
 {
 	if (!path)
 		path = "man";
+
+	colorize_man();
 	execlp(path, "man", page, (char *)NULL);
 	warning_errno(_("failed to exec '%s'"), path);
 }
@@ -371,8 +396,12 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	}
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
+	if (!strcmp(var, "color.man")) {
+		man_color = git_config_bool(var, value);
+		return 0;
+	}
 
-	return git_default_config(var, value, cb);
+	return git_color_default_config(var, value, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/color.h b/color.h
index 98894d6a17..d012add4e8 100644
--- a/color.h
+++ b/color.h
@@ -51,6 +51,7 @@ struct strbuf;
 #define GIT_COLOR_FAINT		"\033[2m"
 #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
 #define GIT_COLOR_REVERSE	"\033[7m"
+#define GIT_COLOR_UNDERLINE	"\033[4m"
 
 /* A special value meaning "no color selected" */
 #define GIT_COLOR_NIL "NIL"
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v8] help: add option to colorize man pages under less
  2021-06-26  2:50 ` [PATCH v8] help: add option to colorize man pages under less Felipe Contreras
@ 2021-06-26 14:49   ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 22+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-06-26 14:49 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: git, Jeff King, Junio C Hamano


On Fri, Jun 25 2021, Felipe Contreras wrote:

> This a reroll of Ævar's v7 with comments from Jeff King so that
> color.ui=never doesn't disable color.man, and the documentation was
> updated accordingly.
>
> Additinally I removed one call to colorize_man() in exec_man_cmd() which
> is not meant for the man command (although it could be used for that).
>
> Plus a bunch of style changes to the commit message.

This version looks good to me, thanks for the style & grammar fixes on
my commit message additions.

>           1. color.man=true must be set in the config
>           2. The user must use less
>           3. Not have the same LESS_TERMCAP variables set (we call setenv(3) with overwrite=0)
>     -     4. Have color.ui enabled
>     -     5. Not have color.pager disabled
>     -     6. Not have git with stdout directed to a file
>     +     4. Not have color.pager disabled
>     +     5. Not have git with stdout directed to a file

Even better.

>  static struct cmdnames main_cmds, other_cmds;
> diff --git a/color.h b/color.h
> index 98894d6a17..d012add4e8 100644
> --- a/color.h
> +++ b/color.h
> @@ -51,6 +51,7 @@ struct strbuf;
>  #define GIT_COLOR_FAINT		"\033[2m"
>  #define GIT_COLOR_FAINT_ITALIC	"\033[2;3m"
>  #define GIT_COLOR_REVERSE	"\033[7m"
> +#define GIT_COLOR_UNDERLINE	"\033[4m"
>  
>  /* A special value meaning "no color selected" */
>  #define GIT_COLOR_NIL "NIL"

Not really needing, but I note that this adds something that we don't
have in test_decode_color(), not that we're testing this output
directly, so it doesn't matter for now.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-23 23:58           ` Jeff King
  2021-06-24  1:03             ` Felipe Contreras
@ 2021-06-28 17:37             ` Junio C Hamano
  2021-06-28 18:05               ` Felipe Contreras
  1 sibling, 1 reply; 22+ messages in thread
From: Junio C Hamano @ 2021-06-28 17:37 UTC (permalink / raw)
  To: Jeff King
  Cc: Ævar Arnfjörð Bjarmason, git, Felipe Contreras,
	Phillip Wood

Jeff King <peff@peff.net> writes:

> I'm still entirely unconvinced that this should be in Git at all;
> pointing GIT_MAN_VIEWER or man.*.cmd at a color-man wrapper seems like
> it would be sufficient. But it feels like that conversation was not
> going anywhere productive; I mention it here only to indicate that my
> response above is not an endorsement of the concept.

I have the same reaction to the patch.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-28 17:37             ` Junio C Hamano
@ 2021-06-28 18:05               ` Felipe Contreras
  0 siblings, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-28 18:05 UTC (permalink / raw)
  To: Junio C Hamano, Jeff King
  Cc: Ævar Arnfjörð Bjarmason, git, Felipe Contreras,
	Phillip Wood

Junio C Hamano wrote:
> Jeff King <peff@peff.net> writes:
> 
> > I'm still entirely unconvinced that this should be in Git at all;
> > pointing GIT_MAN_VIEWER or man.*.cmd at a color-man wrapper seems like
> > it would be sufficient. But it feels like that conversation was not
> > going anywhere productive; I mention it here only to indicate that my
> > response above is not an endorsement of the concept.
> 
> I have the same reaction to the patch.

Do you care to explain why you think so?

What is the alternative in your view? What configuration should users
use instead?

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-24  0:08       ` Jeff King
@ 2021-06-29  1:42         ` Junio C Hamano
  2021-06-29  1:48           ` Felipe Contreras
  0 siblings, 1 reply; 22+ messages in thread
From: Junio C Hamano @ 2021-06-29  1:42 UTC (permalink / raw)
  To: Jeff King
  Cc: Ævar Arnfjörð Bjarmason, git, Felipe Contreras,
	Phillip Wood

Jeff King <peff@peff.net> writes:

> .... But from your
> description (and I think the code matches this), doing:
>
>   [color]
>   ui = false
>   man = true
>
> would still disable the man-colors. So there's no way to enable this
> feature without enabling colors everywhere else. I think it should
> simply be independent of color.ui (with the exception that it may
> eventually use it as a fallback like all the other color.* booleans,
> _if_ we want to move it to default-to-on).

That matches my perception.  Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v7] help: colorize man pages if man.color=true under less(1)
  2021-06-29  1:42         ` Junio C Hamano
@ 2021-06-29  1:48           ` Felipe Contreras
  0 siblings, 0 replies; 22+ messages in thread
From: Felipe Contreras @ 2021-06-29  1:48 UTC (permalink / raw)
  To: Junio C Hamano, Jeff King
  Cc: Ævar Arnfjörð Bjarmason, git, Felipe Contreras,
	Phillip Wood

Junio C Hamano wrote:
> Jeff King <peff@peff.net> writes:
> 
> > .... But from your
> > description (and I think the code matches this), doing:
> >
> >   [color]
> >   ui = false
> >   man = true
> >
> > would still disable the man-colors. So there's no way to enable this
> > feature without enabling colors everywhere else. I think it should
> > simply be independent of color.ui (with the exception that it may
> > eventually use it as a fallback like all the other color.* booleans,
> > _if_ we want to move it to default-to-on).
> 
> That matches my perception.  Thanks.

This is already integrated into v8.

https://lore.kernel.org/git/20210626025040.104428-1-felipe.contreras@gmail.com/

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2021-06-29  1:48 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-23  5:44 [PATCH v6] help: colorize man pages Felipe Contreras
2021-05-24 13:13 ` Phillip Wood
2021-05-24 16:56   ` Felipe Contreras
2021-06-08 12:35 ` Ævar Arnfjörð Bjarmason
2021-06-08 13:57   ` Junio C Hamano
2021-06-08 17:48     ` Felipe Contreras
2021-06-21  8:34     ` [PATCH v7] help: colorize man pages if man.color=true under less(1) Ævar Arnfjörð Bjarmason
2021-06-21 10:17       ` Phillip Wood
2021-06-21 10:28       ` Junio C Hamano
2021-06-21 18:41         ` Felipe Contreras
2021-06-21 19:08         ` Ævar Arnfjörð Bjarmason
2021-06-23 23:58           ` Jeff King
2021-06-24  1:03             ` Felipe Contreras
2021-06-28 17:37             ` Junio C Hamano
2021-06-28 18:05               ` Felipe Contreras
2021-06-21 15:59       ` Felipe Contreras
2021-06-24  0:08       ` Jeff King
2021-06-29  1:42         ` Junio C Hamano
2021-06-29  1:48           ` Felipe Contreras
2021-06-24  1:44 ` [PATCH v6] help: colorize man pages Felipe Contreras
2021-06-26  2:50 ` [PATCH v8] help: add option to colorize man pages under less Felipe Contreras
2021-06-26 14:49   ` Ævar Arnfjörð Bjarmason

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.