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:
Marios Prokopakis 2019-08-19 17:37:21 +03:00 committed by Andreas Kling
parent 3792c91059
commit 91c79440a1
Notes: sideshowbarker 2024-07-19 12:35:46 +09:00

View File

@ -265,7 +265,6 @@ int do_file_system_object_short(const char* path)
int printed_on_row = 0;
int nprinted;
for (int i = 0; i < names.size(); ++i) {
auto& name = names[i];
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))
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;
for (int i = nprinted; i < column_width; ++i)
for (int j = nprinted; i != (names.size() - 1) && j < column_width; ++j)
printf(" ");
if ((printed_on_row + column_width) >= columns) {
if (i != names.size() - 1)
printf("\n");
printf("\n");
printed_on_row = 0;
}
}
if (printed_on_row)
printf("\n");
return 0;
}