[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 3/3] commands/ls: Proper line breaks between arguments
From: |
Glenn Washburn |
Subject: |
[PATCH v2 3/3] commands/ls: Proper line breaks between arguments |
Date: |
Sat, 8 Jun 2024 16:58:33 -0500 |
There should be a blank line before each directory argument and before
each file arugment that come after a directory argument. This brings the
ls command more inline with GNU's ls. Although one key difference still is
that GNU's ls reorders the output of arguments so that all file arguments
are grouped at the beginning of output, while GRUB's ls does no reordering
and shows output for each arument in the order that it was specified on
the command line. Also fixed is an issue where there was a blank line
between the output of consecutive file path arguments, which needlessly
wastes valuable screen space.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/commands/ls.c | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)
diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
index 10939eefeab5..0a3070372821 100644
--- a/grub-core/commands/ls.c
+++ b/grub-core/commands/ls.c
@@ -92,6 +92,8 @@ struct grub_ls_list_files_ctx
int human;
int longlist;
int print_dirhdr;
+ int first_arg;
+ int prev_file;
};
/* Helper for grub_ls_list_files. */
@@ -105,6 +107,19 @@ print_files (const char *filename, const struct
grub_dirhook_info *info,
if ((! ctx->all) && (filename[0] == '.'))
return 0;
+ /* Never print a leading newline if this is the first arg */
+ if (! ctx->first_arg)
+ /*
+ * Print leading newline(s) if (this is a file argument and the previous
+ * was not) or this is the first file from a directory argument.
+ */
+ if (((ctx->filename != NULL) && (! ctx->prev_file)) || ctx->print_dirhdr)
+ {
+ grub_xputs ("\n");
+ if (! ctx->longlist)
+ grub_xputs ("\n");
+ }
+
if (ctx->print_dirhdr)
{
grub_printf ("%s:\n", ctx->dirname);
@@ -190,7 +205,8 @@ print_file (const char *filename, const struct
grub_dirhook_info *info,
}
static grub_err_t
-grub_ls_list_files (char *dirname, int longlist, int all, int human, int
dirhdr)
+grub_ls_list_files (char *dirname, int longlist, int all, int human,
+ int dirhdr, int first_arg, int *prev_file)
{
char *device_name;
grub_fs_t fs;
@@ -239,7 +255,9 @@ grub_ls_list_files (char *dirname, int longlist, int all,
int human, int dirhdr)
.all = all,
.human = human,
.longlist = longlist,
- .print_dirhdr = dirhdr
+ .print_dirhdr = dirhdr,
+ .first_arg = first_arg,
+ .prev_file = *prev_file
};
(fs->fs_dir) (dev, path, print_files, &ctx);
@@ -262,11 +280,11 @@ grub_ls_list_files (char *dirname, int longlist, int all,
int human, int dirhdr)
(fs->fs_dir) (dev, ctx.dirname, print_file, &ctx);
+ *prev_file = 1;
grub_free (ctx.dirname);
}
-
- if (grub_errno == GRUB_ERR_NONE)
- grub_xputs ("\n");
+ else
+ *prev_file = 0;
grub_refresh ();
}
@@ -284,14 +302,14 @@ static grub_err_t
grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = ctxt->state;
- int i;
+ int i, prev_file = 0;
if (argc == 0)
grub_ls_list_devices (state[0].set);
else
for (i = 0; i < argc; i++)
grub_ls_list_files (args[i], state[0].set, state[2].set, state[1].set,
- argc > 1);
+ argc > 1, i == 0, &prev_file);
return 0;
}
--
2.34.1