aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-29 00:52:39 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-29 00:52:39 +0100
commite5180acf5f2dfac788e8c12886095ed1ac66fae5 (patch)
treecf2c8cc0eeb89bf5d9700a0c00ccdfe98e301615
parentd24315268c11d435bb9accbce87b7f46dda6ed3e (diff)
downloadnihil-e5180acf5f2dfac788e8c12886095ed1ac66fae5.tar.gz
nihil-e5180acf5f2dfac788e8c12886095ed1ac66fae5.tar.bz2
cli: improve path handling
-rw-r--r--nihil.cli/command_tree.cc25
-rw-r--r--nihil.cli/command_tree.ccm2
2 files changed, 9 insertions, 18 deletions
diff --git a/nihil.cli/command_tree.cc b/nihil.cli/command_tree.cc
index 4142a40..7cea8da 100644
--- a/nihil.cli/command_tree.cc
+++ b/nihil.cli/command_tree.cc
@@ -59,7 +59,7 @@ auto command_tree_node::get_child(this command_tree_node &self,
}
auto command_tree_node::get_or_create_child(this command_tree_node &self,
- std::string_view child)
+ std::string_view child)
-> command_tree_node *
{
// Return the existing child, if there is one.
@@ -72,8 +72,11 @@ auto command_tree_node::get_or_create_child(this command_tree_node &self,
command_tree_node(&self, child));
// Give the child a dummy command.
- it->second.m_command = std::make_shared<command_node>(
- self.path() + ' ' + child);
+ auto path = self.m_parent != nullptr
+ ? std::format("{} {}", self.m_parent->path(), child)
+ : std::string(child);
+
+ it->second.m_command = std::make_shared<command_node>(path);
return &it->second;
}
@@ -108,21 +111,9 @@ auto command_tree_node::print_commands(this command_tree_node const &self)
}
auto command_tree_node::path(this command_tree_node const &self)
- -> std::string
+ -> std::string_view
{
- auto path = std::string();
-
- auto const *node = &self;
- while (node->m_parent != nullptr) {
- path = node->m_this_word + ' ' + path;
- node = node->m_parent;
- }
-
- // Trim the trailing space.
- if (!path.empty())
- path.pop_back();
-
- return path;
+ return self.m_command->path();
}
auto command_tree::insert(this command_tree &self,
diff --git a/nihil.cli/command_tree.ccm b/nihil.cli/command_tree.ccm
index f52b768..7297af7 100644
--- a/nihil.cli/command_tree.ccm
+++ b/nihil.cli/command_tree.ccm
@@ -62,7 +62,7 @@ struct command_tree_node final {
* Get the path of this command_node.
*/
[[nodiscard]] auto path(this command_tree_node const &self)
- -> std::string;
+ -> std::string_view;
/*
* Print this node's children in a form useful to humans.