[PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Diego Pettenò complained that "ls -l" doesn't use the UTF-8 arrow character to show where symlinks point to. This tiny patch fixes that. With this applied the character is used when the CODESET is UTF-8 otherwise we fall back to the traditional "->" arrow. Ah, "ls -l" is so much prettier now with this oh-so-important patch! For verification: http://pastie.org/573270 This will of course break scripts that try to parse the output of "ls -l" and look for "->". But quite frankly those scripts are broken anyway and should be using "LC_MESSAGES=C" or suchlike. One could argue this breakage might even be desirable. --- src/ls.c | 16 1 files changed, 16 insertions(+), 0 deletions(-) diff --git a/src/ls.c b/src/ls.c index 07e9cf1..f7a838f 100644 --- a/src/ls.c +++ b/src/ls.c @@ -3749,7 +3749,23 @@ print_long_format (const struct fileinfo *f) { if (f->linkname) { +#ifdef HAVE_NL_LANGINFO + static const char *arrow = NULL; + + if (!arrow) + { + const char *cs; + cs = nl_langinfo(CODESET); + + if (cs && strcmp(cs, "UTF-8") == 0) + arrow = " \xe2\x86\x92 "; + else + arrow = " -> "; + } + DIRED_FPUTS_LITERAL (arrow, stdout); +#else DIRED_FPUTS_LITERAL (" -> ", stdout); +#endif print_name_with_quoting (f->linkname, f->linkmode, f->linkok - 1, f->stat_ok, f->filetype, NULL, f->stat.st_nlink, (p - buf) + w + 4); -- 1.6.4 Lennart -- Lennart PoetteringRed Hat, Inc. lennart [at] poettering [dot] net ICQ# 11060553 http://0pointer.net/lennart/ GnuPG 0x1A015CC4
[PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
[Second version of the patch, makes this feature optional with --fancy-chars] Diego Pettenò complained that "ls -l" doesn't use the UTF-8 arrow character to show where symlinks point to. This tiny patch fixes that. With this applied the character is used when the CODESET is UTF-8 otherwise we fall back to the traditional "->" arrow. This is only enabled if --fancy-chars is passed as argument. Ah, "ls -l" is so much prettier now! For verification: http://pastie.org/573270 --- src/ls.c | 33 + 1 files changed, 33 insertions(+), 0 deletions(-) diff --git a/src/ls.c b/src/ls.c index 07e9cf1..0d5be7c 100644 --- a/src/ls.c +++ b/src/ls.c @@ -496,6 +496,11 @@ static enum indicator_style const indicator_style_types[] = }; ARGMATCH_VERIFY (indicator_style_args, indicator_style_types); +/* Use fancy Unicode characters in output. At this point this simply + replaces the -> arrow for symlinks by a real Unicode arrow. */ + +static bool fancy_chars; + /* True means use colors to mark types. Also define the different colors as well as the stuff for the LS_COLORS environment variable. The LS_COLORS variable is now in a termcap-like format. */ @@ -758,6 +763,7 @@ enum AUTHOR_OPTION = CHAR_MAX + 1, BLOCK_SIZE_OPTION, COLOR_OPTION, + FANCY_CHARS_OPTION, DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION, FILE_TYPE_INDICATOR_OPTION, FORMAT_OPTION, @@ -813,6 +819,7 @@ static struct option const long_options[] = {"time", required_argument, NULL, TIME_OPTION}, {"time-style", required_argument, NULL, TIME_STYLE_OPTION}, {"color", optional_argument, NULL, COLOR_OPTION}, + {"fancy-chars", no_argument, NULL, FANCY_CHARS_OPTION}, {"block-size", required_argument, NULL, BLOCK_SIZE_OPTION}, {"context", no_argument, 0, 'Z'}, {"author", no_argument, NULL, AUTHOR_OPTION}, @@ -1522,6 +1529,7 @@ decode_switches (int argc, char **argv) ignore_patterns = NULL; hide_patterns = NULL; print_scontext = false; + fancy_chars = false; /* FIXME: put this in a function. */ { @@ -1866,6 +1874,10 @@ decode_switches (int argc, char **argv) break; } + case FANCY_CHARS_OPTION: + fancy_chars = true; + break; + case INDICATOR_STYLE_OPTION: indicator_style = XARGMATCH ("--indicator-style", optarg, indicator_style_args, @@ -3749,7 +3761,26 @@ print_long_format (const struct fileinfo *f) { if (f->linkname) { +#ifdef HAVE_NL_LANGINFO + static const char *arrow = NULL; + + if (!arrow) + { + arrow = " -> "; + + if (fancy_chars) + { + const char *cs; + cs = nl_langinfo(CODESET); + + if (cs && strcmp(cs, "UTF-8") == 0) + arrow = " \xe2\x86\x92 "; + } + } + DIRED_FPUTS_LITERAL (arrow, stdout); +#else DIRED_FPUTS_LITERAL (" -> ", stdout); +#endif print_name_with_quoting (f->linkname, f->linkmode, f->linkok - 1, f->stat_ok, f->filetype, NULL, f->stat.st_nlink, (p - buf) + w + 4); @@ -4533,6 +4564,8 @@ Mandatory arguments to long options are mandatory for short options too.\n\ -C list entries by columns\n\ --color[=WHEN] control whether color is used to distinguish file\n\ types. WHEN may be `never', `always', or `auto'\n\ + --fancy-chars control whether fancy Unicode characters may be\n\ + used in the output.\n\ -d, --directorylist directory entries instead of contents,\n\ and do not dereference symbolic links\n\ -D, --diredgenerate output designed for Emacs' dired mode\n\ -- 1.6.4 Lennart -- Lennart PoetteringRed Hat, Inc. lennart [at] poettering [dot] net ICQ# 11060553 http://0pointer.net/lennart/ GnuPG 0x1A015CC4
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Hello Lennart, On Thu, Aug 06, 2009 at 07:24:42PM +0200, Lennart Poettering wrote: > Diego Pettenò complained that "ls -l" doesn't use the UTF-8 arrow > character to show where symlinks point to. This tiny patch fixes that. > With this applied the character is used when the CODESET is UTF-8 > otherwise we fall back to the traditional "->" arrow. > > Ah, "ls -l" is so much prettier now with this oh-so-important patch! > For verification: > > http://pastie.org/573270 What if the used font does not include this symbol? Could you check this as well? BTW the symbol in the URL above looks different than that shown in xterm (I used iceweasel for the URL, default fonts for xterm). IMHO use of this symbol should not be enabled by default. Br, Erik -- Premature optimization is the root of all evil. -- Donald Knuth
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
On Thu, 6 Aug 2009, Lennart Poettering wrote: Diego Petten? complained that "ls -l" doesn't use the UTF-8 arrow character to show where symlinks point to. This tiny patch fixes that. With this applied the character is used when the CODESET is UTF-8 otherwise we fall back to the traditional "->" arrow. Ah, "ls -l" is so much prettier now with this oh-so-important patch! For verification: http://pastie.org/573270 This will of course break scripts that try to parse the output of "ls -l" and look for "->". But quite frankly those scripts are broken anyway and should be using "LC_MESSAGES=C" or suchlike. One could argue this breakage might even be desirable. Why are scripts which look for "->" broken? It's explicitly stated in the standard: http://www.opengroup.org/onlinepubs/95399/utilities/ls.html Cheers, Phil
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Lennart Poettering wrote: > [Second version of the patch, makes this feature optional with --fancy-chars] --fancy-chars :) I'm not sure how serious this patch is. How about: alias lsf='ls -l --color | sed "s/ -> / $(tput bold)\u25aa\u25b6$(tput sgr0) /"' cheers, Pádraig. p.s. this chunk is far too verbose > +#ifdef HAVE_NL_LANGINFO > + static const char *arrow = NULL; > + > + if (!arrow) > + { > + arrow = " -> "; > + > + if (fancy_chars) > + { > + const char *cs; > + cs = nl_langinfo(CODESET); > + > + if (cs && strcmp(cs, "UTF-8") == 0) > + arrow = " \xe2\x86\x92 "; > + } > + } > + DIRED_FPUTS_LITERAL (arrow, stdout); > +#else > DIRED_FPUTS_LITERAL (" -> ", stdout); > +#endif this is equivalent I think: static const char *arrow = " -> "; #ifdef HAVE_NL_LANGINFO if (fancy_chars && STREQ (nl_langinfo (CODESET), "UTF-8")) arrow = " \xe2\x86\x92 "; #endif DIRED_FPUTS_LITERAL (arrow, stdout);
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
On Tue, 11.08.09 22:27, Pádraig Brady (p...@draigbrady.com) wrote: > this is equivalent I think: > >static const char *arrow = " -> "; > #ifdef HAVE_NL_LANGINFO >if (fancy_chars && STREQ (nl_langinfo (CODESET), "UTF-8")) > arrow = " \xe2\x86\x92 "; > #endif >DIRED_FPUTS_LITERAL (arrow, stdout); You evaluate the whole expression on every iteration. The whole point of making this variable static is to make sure this isn't necessary. Lennart -- Lennart PoetteringRed Hat, Inc. lennart [at] poettering [dot] net http://0pointer.net/lennart/ GnuPG 0x1A015CC4
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Lennart Poettering wrote: > [Second version of the patch, makes this feature optional with --fancy-chars] > > Diego Pettenò complained that "ls -l" doesn't use the UTF-8 arrow > character to show where symlinks point to. This tiny patch fixes that. > With this applied the character is used when the CODESET is UTF-8 > otherwise we fall back to the traditional "->" arrow. > > This is only enabled if --fancy-chars is passed as argument. > > Ah, "ls -l" is so much prettier now! > > For verification: > > http://pastie.org/573270 Thanks for the patch. However, I'm inclined not to add the functionality even via a separate option, because: - The bar for adding a new option to ls is very high; you'd need more justification than "someone complained" or "it's prettier, now." - It's easy to get nearly the same effect with a simple filter, as Pádraig suggested. (of course, a naive filter fails if a file name contains " -> ", but the end result is solely for human consumption, not for mechanical parsing, so that's ok) Just by the way, I compared your arrow and the one Pádraig used in his example: $ printf 'a -> b\n' a -> b $ printf 'a \xe2\x86\x92 b\n' a → b $ printf 'a \u25aa\u25b6 b\n' a ▪▶ b I found the 1-column-wide arrow to be disconcertingly similar to a hyphen, when viewed via a 7-point (admittedly small) font in a gnome-terminal. Compare to underscore and hyphen: $ printf 'a _\xe2\x86\x92- b\n' a _→- b As usual, if you come up with more justification, or many people clamor for this option, we will revisit the decision.
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Lennart Poettering wrote: > On Tue, 11.08.09 22:27, Pádraig Brady (p...@draigbrady.com) wrote: > >> this is equivalent I think: >> >>static const char *arrow = " -> "; >> #ifdef HAVE_NL_LANGINFO >>if (fancy_chars && STREQ (nl_langinfo (CODESET), "UTF-8")) >> arrow = " \xe2\x86\x92 "; >> #endif >>DIRED_FPUTS_LITERAL (arrow, stdout); > > You evaluate the whole expression on every iteration. The whole point > of making this variable static is to make sure this isn't necessary. Oh right, so something like: static const char *arrow; if (!arrow) { if (fancy_chars && STREQ (locale_charset(), "UTF-8")) arrow = " \xe2\x86\x92 "; else arrow = " -> "; } DIRED_FPUTS_LITERAL (arrow, stdout); Note the use of locale_charset() from gnulib for portability. cheers, Pádraig.
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Jim Meyering wrote: > > - It's easy to get nearly the same effect with a simple filter, > as Pádraig suggested. (of course, a naive filter fails if > a file name contains " -> ", but the end result is solely for > human consumption, not for mechanical parsing, so that's ok) > > Just by the way, I compared your arrow and the one Pádraig > used in his example: > > $ printf 'a -> b\n' > a -> b > $ printf 'a \xe2\x86\x92 b\n' > a → b > $ printf 'a \u25aa\u25b6 b\n' > a ▪▶ b Just to address my OCD, the example alias I posted would not work as somewhere along the line in pasting over a vnc session the unicode characters were mangled. Also the previous alias didn't precompute as much as it could, so: alias lsf="ls -l --color | sed 's/ -> / $(tput bold)▪▶$(tput sgr0) /'" cheers, Pádraig.
Re: [PATCH] ls: Use pretty UTF-8 arrow when showing where symlinks point to
Pádraig Brady wrote: > > Oh right, so something like: > > static const char *arrow; > if (!arrow) > { > if (fancy_chars && STREQ (locale_charset(), "UTF-8")) > arrow = " \xe2\x86\x92 "; > else > arrow = " -> "; > } > DIRED_FPUTS_LITERAL (arrow, stdout); > > Note the use of locale_charset() from gnulib for portability. BTW I still don't think this patch should be applied, as the benefit from the new option is not big enough, and much the same affect can be achieved with a little post processing as demonstrated. cheers, Pádraig.