Skip to content

Commit

Permalink
pathfinder: split --database option into --dir & --file
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Aug 18, 2024
1 parent 241d699 commit 4e11a89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
36 changes: 23 additions & 13 deletions src/cpp/project/pathfinder/src/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ paf::open::open(lyra::cli& cli)

paf::print::print(lyra::cli& cli)
{
const auto opt_db_type =
lyra::opt(m_db_type, "database")["-d"]["--database"]("database to search")
.choices("both", "directory", "file");
const auto opt_use_dir = lyra::opt(m_use_dir)["-d"]["--dir"]("use directory database");
const auto opt_use_file = lyra::opt(m_use_file)["-f"]["--file"]("use file database");
const auto arg_keycode =
lyra::arg(m_keycode, "keycode")("keycode to match the file").cardinality(1, 1);
const auto opt_nul = lyra::opt(m_nul)["-0"]["-z"]["--null"]("use NUL as output delimiter");

auto command = lyra::command("print", [this](const lyra::group& group) { execute(group); })
.help("Print the value for the given keycode.")
.add_argument(lyra::help(m_show_help))
.add_argument(std::move(opt_db_type))
.add_argument(std::move(opt_use_dir))
.add_argument(std::move(opt_use_file))
.add_argument(std::move(arg_keycode))
.add_argument(std::move(opt_nul))
.optional();
Expand All @@ -116,15 +116,15 @@ paf::print::print(lyra::cli& cli)

paf::list::list(lyra::cli& cli)
{
const auto opt_db_type =
lyra::opt(m_db_type, "database")["-d"]["--database"]("database to list")
.choices("both", "directory", "file");
const auto opt_use_dir = lyra::opt(m_use_dir)["-d"]["--dir"]("use directory database");
const auto opt_use_file = lyra::opt(m_use_file)["-f"]["--file"]("use file database");
const auto opt_nul = lyra::opt(m_nul)["-0"]["-z"]["--null"]("use NUL as output delimiter");

auto command = lyra::command("list", [this](const lyra::group& group) { execute(group); })
.help("List all marks.")
.add_argument(lyra::help(m_show_help))
.add_argument(std::move(opt_db_type))
.add_argument(std::move(opt_use_dir))
.add_argument(std::move(opt_use_file))
.add_argument(std::move(opt_nul))
.optional();

Expand Down Expand Up @@ -319,19 +319,24 @@ PAF_CMD_NORETURN void paf::print::execute(const lyra::group& group)
std::exit(EXIT_SUCCESS);
}

if (m_db_type == "both" || m_db_type == "directory") {
if (!m_use_dir && !m_use_file) {
m_use_dir = true;
m_use_file = true;
}

if (m_use_dir) {
auto db = db::get_db(db_type::directory);
auto dir = db.try_get_mark(m_keycode);
db.cancel();
if (dir) {
std::cout << *dir;
if (!m_db_type.ends_with('/'))
if (!dir->ends_with('/'))
std::cout << '/';
std::cout << (m_nul ? '\0' : '\n');
}
}

if (m_db_type == "both" || m_db_type == "file") {
if (m_use_file) {
auto db = db::get_db(db_type::file);
auto file = db.try_get_mark(m_keycode);
db.cancel();
Expand All @@ -349,13 +354,18 @@ PAF_CMD_NORETURN void paf::list::execute(const lyra::group& group)
std::exit(EXIT_SUCCESS);
}

if (m_db_type == "both" || m_db_type == "directory") {
if (!m_use_dir && !m_use_file) {
m_use_dir = true;
m_use_file = true;
}

if (m_use_dir) {
auto db = db::get_db(db_type::directory);
db.dump(m_nul ? "\0" : " : ", m_nul ? "\0" : "\n");
db.cancel();
}

if (m_db_type == "both" || m_db_type == "file") {
if (m_use_file) {
auto db = db::get_db(db_type::file);
db.dump(m_nul ? "\0" : " : ", m_nul ? "\0" : "\n");
db.cancel();
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/project/pathfinder/src/cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ namespace paf {
class print {
private:
bool m_show_help = false;
std::string m_db_type = "both";
bool m_use_dir = false;
bool m_use_file = false;
std::string m_keycode = "";
bool m_nul = false;

Expand All @@ -82,7 +83,8 @@ namespace paf {
class list {
private:
bool m_show_help = false;
std::string m_db_type = "both";
bool m_use_dir = false;
bool m_use_file = false;
bool m_nul = false;

public:
Expand Down

0 comments on commit 4e11a89

Please sign in to comment.