Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 16, 2024
1 parent c09f57f commit a21b24e
Show file tree
Hide file tree
Showing 12 changed files with 591 additions and 21 deletions.
34 changes: 31 additions & 3 deletions src/clis/ls/main.cpp
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();
}
1 change: 1 addition & 0 deletions src/clis/ls/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "exe",
"description": "The ls coreutility",
"requires": [
"karm-cli",
"karm-sys"
]
}
2 changes: 1 addition & 1 deletion src/impls/impl-posix/pkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Res<Vec<String>> installedBundles() {
auto dirs = try$(Sys::_Embed::readDir(repoRoot));
Vec<String> ids;
for (auto &dir : dirs) {
if (dir.isDir)
if (dir.type == Sys::Type::DIR)
ids.pushBack(dir.name);
}
return Ok(ids);
Expand Down
6 changes: 3 additions & 3 deletions src/impls/impl-posix/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Res<Strong<Fd>> openFile(Mime::Url const &url) {
if (raw < 0)
return Posix::fromLastErrno();
auto fd = makeStrong<Posix::Fd>(raw);
if (try$(fd->stat()).type == Stat::DIR)
if (try$(fd->stat()).type == Type::DIR)
return Error::isADirectory();
return Ok(fd);
}
Expand All @@ -108,7 +108,7 @@ Res<Strong<Fd>> openOrCreateFile(Mime::Url const &url) {
if (raw < 0)
return Posix::fromLastErrno();
auto fd = makeStrong<Posix::Fd>(raw);
if (try$(fd->stat()).type == Stat::DIR)
if (try$(fd->stat()).type == Type::DIR)
return Error::isADirectory();
return Ok(fd);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ Res<Vec<DirEntry>> readDir(Mime::Url const &url) {

entries.pushBack(DirEntry{
Str::fromNullterminated(entry->d_name),
entry->d_type == DT_DIR,
entry->d_type == DT_DIR ? Sys::Type::DIR : Sys::Type::FILE,
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/impls/impl-posix/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ Sys::SocketAddr fromSockAddr(struct sockaddr_in sockaddr) {

Sys::Stat fromStat(struct stat const &buf) {
Sys::Stat stat{};
Sys::Stat::Type type = Sys::Stat::FILE;
Sys::Type type = Sys::Type::FILE;
if (S_ISDIR(buf.st_mode))
type = Sys::Stat::DIR;
type = Sys::Type::DIR;
stat.type = type;
stat.size = (usize)buf.st_size;
stat.accessTime = TimeStamp::epoch() + TimeSpan::fromSecs(buf.st_atime);
Expand Down
71 changes: 71 additions & 0 deletions src/libs/karm-cli/args.cpp
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
Loading

0 comments on commit a21b24e

Please sign in to comment.