Compare commits
No commits in common. "5911b052dc526898d4963680fe83c6e2dc67e55f" and "300d68088bf67329a73ea967a5e31bd00f277038" have entirely different histories.
5911b052dc
...
300d68088b
19
apps/ls.cpp
19
apps/ls.cpp
@ -1,5 +1,4 @@
|
|||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <luna/Units.h>
|
|
||||||
#include <os/ArgumentParser.h>
|
#include <os/ArgumentParser.h>
|
||||||
#include <os/Directory.h>
|
#include <os/Directory.h>
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
@ -62,8 +61,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
bool show_all { false };
|
bool show_all { false };
|
||||||
bool show_almost_all { false };
|
bool show_almost_all { false };
|
||||||
bool long_list { false };
|
bool long_list { false };
|
||||||
bool human_readable { false };
|
|
||||||
bool si { false };
|
|
||||||
|
|
||||||
os::ArgumentParser parser;
|
os::ArgumentParser parser;
|
||||||
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
||||||
@ -72,9 +69,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
|
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
|
||||||
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
|
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
|
||||||
parser.add_switch_argument(long_list, 'l', ""_sv, "use a long listing format"_sv);
|
parser.add_switch_argument(long_list, 'l', ""_sv, "use a long listing format"_sv);
|
||||||
parser.add_switch_argument(human_readable, 'h', "human-readable"_sv,
|
|
||||||
"with -l, show human-readable sizes e.g. 2KiB, 6GiB"_sv);
|
|
||||||
parser.add_switch_argument(si, ' ', "si"_sv, "same as -h, but show sizes in powers of 10"_sv);
|
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
Vector<String> files;
|
Vector<String> files;
|
||||||
@ -118,17 +112,8 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
|
|
||||||
TRY(find_user_and_group(st, owner, group));
|
TRY(find_user_and_group(st, owner, group));
|
||||||
|
|
||||||
if (!human_readable && !si)
|
os::println("%6o %u %4s %4s %10lu %s", st.st_mode, st.st_nlink, owner.chars(), group.chars(), st.st_size,
|
||||||
{
|
file.chars());
|
||||||
os::println("%6o %u %4s %4s %10lu %s", st.st_mode, st.st_nlink, owner.chars(), group.chars(),
|
|
||||||
st.st_size, file.chars());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto size = TRY(to_dynamic_unit(st.st_size, 10, false, si ? Unit::SI : Unit::Binary, false));
|
|
||||||
os::println("%6o %u %4s %4s %6s %s", st.st_mode, st.st_nlink, owner.chars(), group.chars(),
|
|
||||||
size.chars(), file.chars());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,4 @@
|
|||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
#include <luna/String.h>
|
#include <luna/String.h>
|
||||||
|
|
||||||
enum class Unit : usize
|
Result<String> to_dynamic_unit(usize value);
|
||||||
{
|
|
||||||
SI = 1000,
|
|
||||||
Binary = 1024
|
|
||||||
};
|
|
||||||
|
|
||||||
Result<String> to_dynamic_unit(usize value, usize round_after = 1000, bool separate = true, Unit unit = Unit::Binary,
|
|
||||||
bool display_unit = true);
|
|
||||||
|
@ -1,44 +1,18 @@
|
|||||||
#include <luna/Alloc.h>
|
#include <luna/Alloc.h>
|
||||||
#include <luna/CType.h>
|
|
||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
#include <luna/ScopeGuard.h>
|
#include <luna/ScopeGuard.h>
|
||||||
#include <luna/Units.h>
|
#include <luna/Units.h>
|
||||||
|
|
||||||
Result<String> to_dynamic_unit(usize value, usize round_after, bool separate, Unit unit, bool display_unit)
|
Result<String> to_dynamic_unit(usize value)
|
||||||
{
|
{
|
||||||
const usize multiplier = (usize)unit;
|
if (value < 1024) { return String::format("%zu bytes"_sv, value); }
|
||||||
if (value < multiplier) { return String::format("%zu%s"_sv, value, display_unit ? " bytes" : ""); }
|
|
||||||
|
|
||||||
bool si = unit == Unit::SI;
|
|
||||||
|
|
||||||
const char* unit_prefixes = "KMGTPE";
|
const char* unit_prefixes = "KMGTPE";
|
||||||
if (si) unit_prefixes = "kMGTPE";
|
while (value > (1024 * 1024))
|
||||||
while (value > (multiplier * multiplier))
|
|
||||||
{
|
{
|
||||||
value /= multiplier;
|
value /= 1024;
|
||||||
unit_prefixes++;
|
unit_prefixes++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const usize divider = (si ? 100 : 103);
|
return String::format("%zu.%zu %ciB"_sv, value / 1024, (value % 1024) / 103, *unit_prefixes);
|
||||||
|
|
||||||
usize fixed = value / multiplier;
|
|
||||||
usize rest = (value % multiplier) / divider;
|
|
||||||
bool round = fixed >= round_after;
|
|
||||||
|
|
||||||
if (round)
|
|
||||||
{
|
|
||||||
if (value % multiplier) fixed++;
|
|
||||||
return String::format("%zu%s%c%s"_sv, fixed, separate ? " " : "", *unit_prefixes,
|
|
||||||
display_unit ? (si ? "B" : "iB") : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((value % multiplier) % divider) rest++;
|
|
||||||
if (rest > 9)
|
|
||||||
{
|
|
||||||
rest = 0;
|
|
||||||
fixed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return String::format("%zu.%zu%s%c%s"_sv, fixed, rest, separate ? " " : "", *unit_prefixes,
|
|
||||||
display_unit ? (si ? "B" : "iB") : "");
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user