mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
Userland: Improved the printing of the ls command. (#468)
The ls command takes into consideration the length of the largest filename when it prints a line. Fixes #432.
This commit is contained in:
parent
3792c91059
commit
91c79440a1
1 changed files with 8 additions and 6 deletions
|
@ -265,7 +265,6 @@ int do_file_system_object_short(const char* path)
|
||||||
|
|
||||||
int printed_on_row = 0;
|
int printed_on_row = 0;
|
||||||
int nprinted;
|
int nprinted;
|
||||||
|
|
||||||
for (int i = 0; i < names.size(); ++i) {
|
for (int i = 0; i < names.size(); ++i) {
|
||||||
auto& name = names[i];
|
auto& name = names[i];
|
||||||
char pathbuf[256];
|
char pathbuf[256];
|
||||||
|
@ -273,19 +272,22 @@ int do_file_system_object_short(const char* path)
|
||||||
|
|
||||||
if (!print_filesystem_object_short(pathbuf, name.characters(), &nprinted))
|
if (!print_filesystem_object_short(pathbuf, name.characters(), &nprinted))
|
||||||
return 2;
|
return 2;
|
||||||
int column_width = 14;
|
int offset = columns % longest_name / (columns / longest_name);
|
||||||
|
/* The offset must be at least 2 because:
|
||||||
|
* - With each file an aditional char is printed e.g. '@','*'.
|
||||||
|
* - Each filename must be separated by a space.
|
||||||
|
*/
|
||||||
|
int column_width = longest_name + (offset > 0 ? offset : 2);
|
||||||
printed_on_row += column_width;
|
printed_on_row += column_width;
|
||||||
|
|
||||||
for (int i = nprinted; i < column_width; ++i)
|
for (int j = nprinted; i != (names.size() - 1) && j < column_width; ++j)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
if ((printed_on_row + column_width) >= columns) {
|
if ((printed_on_row + column_width) >= columns) {
|
||||||
if (i != names.size() - 1)
|
printf("\n");
|
||||||
printf("\n");
|
|
||||||
printed_on_row = 0;
|
printed_on_row = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (printed_on_row)
|
if (printed_on_row)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue