-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove `le::cli`, add new stuff in `le::console`. Refactor `le::imcpp::ConsoleWindow` for `le::console`.
- Loading branch information
Showing
18 changed files
with
356 additions
and
348 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
#pragma once | ||
#include <le/core/polymorphic.hpp> | ||
#include <string> | ||
|
||
namespace le::console { | ||
class Command : public Polymorphic { | ||
public: | ||
struct Response { | ||
std::string message{}; | ||
std::string error{}; | ||
}; | ||
|
||
explicit Command(std::string name) : m_name(std::move(name)) {} | ||
|
||
[[nodiscard]] auto get_name() const -> std::string_view { return m_name; } | ||
|
||
virtual auto process(std::string_view argument) -> Response = 0; | ||
|
||
private: | ||
std::string m_name{}; | ||
}; | ||
} // namespace le::console |
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,69 @@ | ||
#pragma once | ||
#include <le/console/command.hpp> | ||
#include <deque> | ||
#include <functional> | ||
#include <memory> | ||
#include <unordered_map> | ||
|
||
namespace le::console { | ||
struct Entry { | ||
std::string request{}; | ||
Command::Response response{}; | ||
}; | ||
|
||
struct Autocomplete { | ||
std::string_view common_suffix{}; | ||
std::vector<std::string_view> candidates{}; | ||
}; | ||
|
||
class Console { | ||
public: | ||
static constexpr std::size_t max_entries_v{100}; | ||
static constexpr std::size_t max_log_entries_v{100}; | ||
static constexpr std::size_t max_history_v{100}; | ||
|
||
Console(); | ||
|
||
template <std::derived_from<Command> Type, typename... Args> | ||
requires(std::constructible_from<Type, Args...>) | ||
auto add(Args&&... args) -> Type& { | ||
auto t = std::make_unique<Type>(std::forward<Args>(args)...); | ||
auto& ret = *t; | ||
auto name = static_cast<Command const&>(ret).get_name(); | ||
m_commands.insert_or_assign(name, std::move(t)); | ||
return ret; | ||
} | ||
|
||
void submit(std::string_view text); | ||
[[nodiscard]] auto autocomplete(std::string_view text, std::size_t cursor) const -> Autocomplete; | ||
|
||
void add_entry(Entry entry); | ||
void add_entry(Autocomplete const& ac); | ||
void add_to_history(std::string text); | ||
|
||
[[nodiscard]] auto get_entries() const -> std::deque<Entry> const& { return m_entries; } | ||
[[nodiscard]] auto get_history() const -> std::deque<std::string> const& { return m_history; } | ||
|
||
void clear_log() { m_entries.clear(); } | ||
void clear_history() { m_history.clear(); } | ||
|
||
std::size_t max_entries{max_log_entries_v}; | ||
std::size_t max_log_entries{max_log_entries_v}; | ||
std::size_t max_history{max_history_v}; | ||
|
||
private: | ||
struct Builtin { | ||
std::string_view name{}; | ||
std::function<void(Console&)> command{}; | ||
}; | ||
|
||
[[nodiscard]] auto autocomplete(std::string_view fragment) const -> std::vector<std::string_view>; | ||
auto builtin(std::string_view cmd) -> bool; | ||
[[nodiscard]] auto get_builtin_names(std::string_view fragment) const -> std::vector<std::string_view>; | ||
|
||
std::unordered_map<std::string_view, std::unique_ptr<Command>> m_commands{}; | ||
std::vector<Builtin> m_builtins{}; | ||
std::deque<Entry> m_entries{}; | ||
std::deque<std::string> m_history{}; | ||
}; | ||
} // namespace le::console |
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,61 @@ | ||
#pragma once | ||
#include <le/console/command.hpp> | ||
#include <charconv> | ||
#include <concepts> | ||
#include <format> | ||
|
||
namespace le::console { | ||
template <typename Type> | ||
concept PropertyT = std::same_as<Type, bool> || std::integral<Type> || std::floating_point<Type> || std::same_as<Type, std::string>; | ||
|
||
template <PropertyT Type> | ||
constexpr auto to_str() { | ||
if constexpr (std::same_as<Type, bool>) { | ||
return "boolean"; | ||
} else if constexpr (std::integral<Type>) { | ||
return "integer"; | ||
} else if constexpr (std::floating_point<Type>) { | ||
return "float"; | ||
} else { | ||
return "string"; | ||
} | ||
} | ||
|
||
template <PropertyT Type> | ||
class Property : public Command { | ||
public: | ||
using Base = Property<Type>; | ||
|
||
using Command::Command; | ||
|
||
virtual void on_changed() = 0; | ||
|
||
Type value{}; | ||
|
||
private: | ||
auto process(std::string_view const argument) -> Response final { | ||
if (argument.empty()) { return Response{.message = std::format("{}", value)}; } | ||
|
||
auto invalid_arg = [&] { return Response{.error = std::format("invalid argument, expected {}", to_str<Type>())}; }; | ||
|
||
if constexpr (std::same_as<Type, bool>) { | ||
if (argument == "true" || argument == "1") { | ||
value = true; | ||
} else if (argument == "false" || argument == "0") { | ||
value = false; | ||
} else { | ||
return invalid_arg(); | ||
} | ||
} else if constexpr (std::same_as<Type, std::string>) { | ||
value = argument; | ||
} else { | ||
auto const* end = argument.data() + argument.size(); | ||
auto [ptr, ec] = std::from_chars(argument.data(), end, value); | ||
if (ec != std::errc{} || ptr != end) { return invalid_arg(); } | ||
} | ||
|
||
on_changed(); | ||
return {}; | ||
} | ||
}; | ||
} // namespace le::console |
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,19 @@ | ||
#pragma once | ||
#include <le/console/command.hpp> | ||
|
||
namespace le::console { | ||
class Trigger : public Command { | ||
public: | ||
using Base = Trigger; | ||
|
||
using Command::Command; | ||
|
||
virtual void on_triggered() = 0; | ||
|
||
private: | ||
auto process(std::string_view const /*unused*/) -> Response final { | ||
on_triggered(); | ||
return {}; | ||
} | ||
}; | ||
} // namespace le::console |
Oops, something went wrong.