Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/history only dump successful commands #66

Merged
merged 9 commits into from
Feb 28, 2024
18 changes: 8 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
project(
qsyn
LANGUAGES CXX
VERSION 0.6.2)
VERSION 0.6.3)

find_package(OpenMP REQUIRED)

Expand Down Expand Up @@ -52,6 +52,7 @@ FetchContent_Declare(
SYSTEM
GIT_REPOSITORY https://github.com/seleznevae/libfort.git
GIT_TAG v0.4.2)
set(FORT_ENABLE_TESTING OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(libfort)

FetchContent_Declare(
Expand All @@ -66,7 +67,13 @@ FetchContent_Declare(
SYSTEM
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.12.0)
set(SPDLOG_FMT_EXTERNAL ON CACHE INTERNAL "")
FetchContent_MakeAvailable(spdlog)
target_compile_definitions(
spdlog
PRIVATE
SPDLOG_SHORT_LEVEL_NAMES={\"[trace]\ \ \ \ \",\"[debug]\ \ \ \ \",\"[info]\ \ \ \ \ \",\"[warn]\ \ \ \ \ \",\"[error]\ \ \ \ \",\"[critical]\ \",\"\"})
target_link_libraries(spdlog PRIVATE fmt::fmt)

FetchContent_Declare(
GSL
Expand All @@ -82,15 +89,6 @@ file(
"src/**/*.cpp" "src/**/*.hpp" "src/qsyn/qsynrc.default")
add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})

target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL=1)
target_compile_definitions(
spdlog
PUBLIC
SPDLOG_SHORT_LEVEL_NAMES={\"[trace]\ \ \ \ \",\"[debug]\ \ \ \ \",\"[info]\ \ \ \ \ \",\"[warn]\ \ \ \ \ \",\"[error]\ \ \ \ \",\"[critical]\ \",\"\"}
)

target_link_libraries(spdlog PUBLIC fmt::fmt)

target_compile_definitions(${CMAKE_PROJECT_NAME}
PRIVATE QSYN_VERSION="v${CMAKE_PROJECT_VERSION}")
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -Wall -Wextra -Werror)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Visualization functionalities of `qsyn` depend at runtime on the following depen

```sh
❯ ./qsyn
qsyn v0.6.2 - Copyright © 2022-2023, DVLab NTUEE.
qsyn v0.6.3 - Copyright © 2022-2023, DVLab NTUEE.
Licensed under Apache 2.0 License.
qsyn>
```
Expand All @@ -130,7 +130,7 @@ Visualization functionalities of `qsyn` depend at runtime on the following depen

```sh
❯ ./qsyn -f examples/synth.dof
qsyn v0.6.2 - DVLab NTUEE.
qsyn v0.6.3 - DVLab NTUEE.
Licensed under Apache 2.0 License.
qsyn> qcir read benchmark/zx/tof3.zx
```
Expand Down
114 changes: 68 additions & 46 deletions src/cli/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
****************************************************************************/
#pragma once

#include <sys/types.h>

#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
Expand Down Expand Up @@ -37,19 +40,13 @@ struct heterogeneous_string_hash {

class CommandLineInterface;

//----------------------------------------------------------------------
// External declaration
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// command execution status
//----------------------------------------------------------------------
enum class CmdExecResult {
done,
error,
quit,
no_op,
interrupted,
// intentially not using enum class because it is used as the return value of main
enum CmdExecResult : int {
done = EXIT_SUCCESS,
error = EXIT_FAILURE,
cmd_not_found = 127,
interrupted = 130,
quit = 131
};
namespace detail {

Expand Down Expand Up @@ -137,8 +134,16 @@ class CommandLineInterface {
void list_all_commands() const;
void list_all_aliases() const;
void list_all_variables() const;
void print_history(size_t n_print = SIZE_MAX) const;
void write_history(std::filesystem::path const& filepath, size_t n_print = SIZE_MAX, bool append_quit = true) const;

struct HistoryFilter {
bool success : 1;
bool error : 1;
bool unknown : 1;
bool interrupted : 1;
};

void print_history(size_t n_print = SIZE_MAX, HistoryFilter filter = {.success = true, .error = true, .unknown = true, .interrupted = true}) const;
void write_history(std::filesystem::path const& filepath, size_t n_print = SIZE_MAX, bool append_quit = true, HistoryFilter filter = {.success = true, .error = false, .unknown = false, .interrupted = false}) const;
inline void clear_history() {
_history.clear();
_history_idx = 0;
Expand All @@ -163,7 +168,50 @@ class CommandLineInterface {
std::string get_first_token(std::string_view str) const;
std::string get_last_token(std::string_view str) const;

CmdExecResult get_last_return_code() const { return _history.empty() ? CmdExecResult::done : _history.back().status; }

private:
enum class TabActionResult {
autocomplete,
list_options,
no_op
};

struct HistoryEntry {
HistoryEntry(std::string input, CmdExecResult status) : input{std::move(input)}, status{status} {}
std::string input;
CmdExecResult status;
};

// Data members
std::string _command_prompt;
std::string _read_buffer;
size_t _cursor_position = 0;

std::vector<HistoryEntry> _history;
size_t _history_idx = 0;
size_t _tab_press_count = 0;
bool _listening_for_inputs = false;
bool _temp_command_stored = false; // When up/pgUp is pressed, current line
// will be stored in _history and
// _tempCmdStored will be true.
// Reset to false when new command added
CmdMap _commands;

// retiring the use of _cli_level in favor of environment
size_t _cli_level = 0;
// CLI environment variables
// the following are the variables that may be overriden by in scripts
dvlab::utils::Trie _identifiers;
struct Environment {
std::vector<HistoryEntry> _history;
};
bool _echo = true;
std::unordered_map<std::string, std::string, detail::heterogeneous_string_hash, std::equal_to<>> _aliases;
std::unordered_map<std::string, std::string, detail::heterogeneous_string_hash, std::equal_to<>> _variables; // stores the variables key-value pairs, e.g., $1, $INPUT_FILE, etc...

std::stack<jthread::jthread> _command_threads;

// Private member functions
void _clear_read_buffer_and_print_prompt();

Expand All @@ -173,12 +221,6 @@ class CommandLineInterface {
CmdExecResult _dispatch_command(dvlab::Command* cmd, std::vector<argparse::Token> options);
bool _is_escaped(std::string_view str, size_t pos) const;
bool _should_be_escaped(char ch, dvlab::CommandLineInterface::parse_state state) const;

enum class TabActionResult {
autocomplete,
list_options,
no_op
};
// tab-related features features
void _on_tab_pressed();
// onTabPressed subroutines
Expand All @@ -192,18 +234,21 @@ class CommandLineInterface {
bool _autocomplete(std::string prefix_copy, std::vector<std::string> const& strs, parse_state state);
void _print_as_table(std::vector<std::string> words) const;

// Helper functions
size_t _get_first_token_pos(std::string_view str, char token = ' ') const;
size_t _get_last_token_pos(std::string_view str, char token = ' ') const;

bool _move_cursor_to(size_t pos);
bool _to_prev_word();
bool _to_next_word();
bool _delete_char();
void _insert_char(char ch);
void _delete_line();
void _replace_at_cursor(std::string_view old_str, std::string_view new_str);
void _reprint_command();
void _retrieve_history(size_t index);
bool _add_to_history(std::string_view input);
size_t _prev_matching_history(size_t count = 1);
size_t _next_matching_history(size_t count = 1);
void _add_to_history(HistoryEntry const& entry);
void _replace_read_buffer_with_history();

template <typename... Args>
Expand All @@ -226,29 +271,6 @@ class CommandLineInterface {
std::string _replace_variable_keys_with_values(std::string const& str) const;

inline bool _is_special_char(char ch) const { return special_chars.find_first_of(ch) != std::string::npos; }

// Data members
std::string _command_prompt;
std::string _read_buffer;
size_t _cursor_position = 0;

std::vector<std::string> _history;
size_t _history_idx = 0;
size_t _tab_press_count = 0;
bool _listening_for_inputs = false;
bool _temp_command_stored = false; // When up/pgUp is pressed, current line
// will be stored in _history and
// _tempCmdStored will be true.
// Reset to false when new command added
bool _echo = true;
dvlab::utils::Trie _identifiers;
CmdMap _commands;
std::unordered_map<std::string, std::string, detail::heterogeneous_string_hash, std::equal_to<>> _aliases;
std::unordered_map<std::string, std::string, detail::heterogeneous_string_hash, std::equal_to<>> _variables; // stores the variables key-value pairs, e.g., $1, $INPUT_FILE, etc...

size_t _cli_level = 0;

std::stack<jthread::jthread> _command_threads;
};

bool add_cli_common_cmds(dvlab::CommandLineInterface& cli);
Expand Down
38 changes: 22 additions & 16 deletions src/cli/cli_char_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,29 @@ constexpr key_code_type arrow_left_key = 68 + arrow_key_flag;
constexpr key_code_type arrow_key_begin{arrow_up_key};
constexpr key_code_type arrow_key_end{arrow_left_key};

//
// -- MOD keys: 27 -> 91 -> {49-54} -> 126
// MOD_KEY = { INSERT, DELETE, HOME, END, PgUp, PgDown }
//
constexpr key_code_type mod_key_flag{1 << 9};
constexpr key_code_type mod_key_int{91};
constexpr key_code_type home_key{49 + mod_key_flag};
constexpr key_code_type insert_key{50 + mod_key_flag};
constexpr key_code_type delete_key{51 + mod_key_flag};
constexpr key_code_type end_key{52 + mod_key_flag};
constexpr key_code_type pg_up_key{53 + mod_key_flag};
constexpr key_code_type pg_down_key{54 + mod_key_flag};
constexpr key_code_type mod_key_begin{home_key};
constexpr key_code_type mod_key_end{pg_down_key};
constexpr key_code_type mod_key_dummy{126};
// -- CTRL keys: 27 -> 91 -> {49-54} -> 126
// CTRL_KEY = { INSERT, DELETE, HOME, END, PgUp, PgDown }

constexpr key_code_type ctrl_key_flag{1 << 9};
constexpr key_code_type ctrl_key_int = arrow_key_int;
constexpr key_code_type home_key{49 + ctrl_key_flag};
constexpr key_code_type insert_key{50 + ctrl_key_flag};
constexpr key_code_type delete_key{51 + ctrl_key_flag};
constexpr key_code_type end_key{52 + ctrl_key_flag};
constexpr key_code_type pg_up_key{53 + ctrl_key_flag};
constexpr key_code_type pg_down_key{54 + ctrl_key_flag};
constexpr key_code_type ctrl_key_begin{home_key};
constexpr key_code_type ctrl_key_end{pg_down_key};
constexpr key_code_type ctrl_key_dummy{126};

constexpr key_code_type alt_key_flag{1 << 10};
constexpr key_code_type alt_key_int = arrow_key_int;
constexpr key_code_type prev_word_key{98 + alt_key_flag};
constexpr key_code_type next_word_key{102 + alt_key_flag};
constexpr key_code_type alt_key_begin{prev_word_key};
constexpr key_code_type alt_key_end{next_word_key};
constexpr key_code_type alt_key_dummy{126};

//
// [For undefined keys]
constexpr key_code_type undefined_key{INT_MAX};

Expand Down
Loading