-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c09f57f
commit a21b24e
Showing
12 changed files
with
591 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
#include <karm-cli/args.h> | ||
#include <karm-sys/dir.h> | ||
#include <karm-sys/entry.h> | ||
|
||
Async::Task<> entryPointAsync(Sys::Context &) { | ||
Async::Task<> entryPointAsync(Sys::Context &ctx) { | ||
auto allFlag = Cli::flag('a', "all"s, "Do not ignore entries starting with ."s); | ||
auto listFlag = Cli::flag('l', "list"s, "Use a long listing format."s); | ||
|
||
Cli::Command cmd{ | ||
"ls"s, | ||
NONE, | ||
"List directory contents."s, | ||
{allFlag} | ||
}; | ||
|
||
co_trya$(cmd.execAsync(ctx)); | ||
|
||
if (not cmd) | ||
co_return Ok(); | ||
|
||
auto url = co_try$(Mime::parseUrlOrPath(".")); | ||
auto dir = co_try$(Sys::Dir::open(url)); | ||
for (auto const &entry : dir.entries()) | ||
Sys::println(entry.name); | ||
|
||
for (auto const &entry : dir.entries()) { | ||
if (!allFlag && entry.name[0] == '.') | ||
continue; | ||
|
||
if (listFlag) { | ||
auto fileUrl = url / entry.name; | ||
auto stat = co_try$(Sys::stat(fileUrl)); | ||
Sys::println("{} {} {}", stat.type == Sys::Type::DIR ? "d"s : "-"s, stat.size, stat.modifyTime, entry.name); | ||
} else { | ||
Sys::println("{}", entry.name); | ||
} | ||
} | ||
|
||
co_return Ok(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"type": "exe", | ||
"description": "The ls coreutility", | ||
"requires": [ | ||
"karm-cli", | ||
"karm-sys" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "args.h" | ||
|
||
namespace Karm::Cli { | ||
|
||
// MARK: Tokenizer ------------------------------------------------------------- | ||
|
||
void tokenize(Str arg, Vec<Token> &out) { | ||
if (arg == "--"s) { | ||
out.pushBack(Token::EXTRA); | ||
} else if (arg == "-"s) { | ||
out.pushBack("-"s); | ||
} else if (startWith(arg, "--"s) == Match::PARTIAL) { | ||
out.emplaceBack(Token::OPTION, next(arg, 2)); | ||
} else if (startWith(arg, "-"s) == Match::PARTIAL) { | ||
Str flags = next(arg, 1); | ||
for (auto r : iterRunes(flags)) | ||
out.emplaceBack(r); | ||
} else { | ||
out.pushBack(arg); | ||
} | ||
} | ||
|
||
void tokenize(Slice<Str> args, Vec<Token> &out) { | ||
for (auto &arg : args) | ||
tokenize(arg, out); | ||
} | ||
|
||
void tokenize(int argc, char **argv, Vec<Token> &out) { | ||
for (int i = 0; i < argc; ++i) | ||
tokenize(Str::fromNullterminated(argv[i]), out); | ||
} | ||
|
||
// MARK: Values ---------------------------------------------------------------- | ||
|
||
void ValueParser<bool>::usage(Io::Emit &e) { | ||
e("true|false"); | ||
} | ||
|
||
Res<bool> ValueParser<bool>::parse(Cursor<Token> &) { | ||
return Ok(true); | ||
} | ||
|
||
void ValueParser<i32>::usage(Io::Emit &e) { | ||
e("integer"); | ||
} | ||
|
||
Res<i32> ValueParser<i32>::parse(Cursor<Token> &c) { | ||
if (c.ended() or c->kind != Token::OPERAND) | ||
return Error::other("missing value"); | ||
|
||
auto value = c.next().value; | ||
|
||
auto result = Io::atoi(value); | ||
if (not result) | ||
return Error::other("expected integer"); | ||
|
||
return Ok(result.unwrap()); | ||
} | ||
|
||
void ValueParser<Str>::usage(Io::Emit &e) { | ||
e("string"); | ||
} | ||
|
||
Res<Str> ValueParser<Str>::parse(Cursor<Token> &c) { | ||
if (c.ended() or c->kind != Token::OPERAND) | ||
return Error::other("missing value"); | ||
|
||
return Ok(c.next().value); | ||
} | ||
|
||
} // namespace Karm::Cli |
Oops, something went wrong.