-
Notifications
You must be signed in to change notification settings - Fork 1.5k
use replxx instead of readline #559
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
437d751
feat: add replxx to .gitmodules and build.sh
Willaaaaaaa ebdddb3
feat(cli): enable cli history via replxx
Willaaaaaaa 01ffe4f
fix: add double-check on `save`, rm unused header and comments
Willaaaaaaa b3a63b9
fix(obclient): readline to replxx
Willaaaaaaa f53504a
fix(oblsm): readline to replxx
Willaaaaaaa 280fbbf
chore: clang-format
Willaaaaaaa 0085f6d
feat: add linenoise and its wrapper class
Willaaaaaaa 67e5ff5
build(cmake): add support for linenoise, reserve replxx(dafault to no…
Willaaaaaaa fa20515
fix: modify source code
Willaaaaaaa 6285ca4
fix: memory leak
Willaaaaaaa ea0f7cf
fix: memory leak, improved code readability
Willaaaaaaa d7b1531
[WIP] try to support both replxx and linenoise
Willaaaaaaa a479729
update
Willaaaaaaa f573918
cmake: use replxx directly.
Willaaaaaaa 498a006
remove linenoise
Willaaaaaaa c12d3a8
feat: use replxx as line_reader
Willaaaaaaa 2795aa7
remove gnu readline configs
Willaaaaaaa c3ef2db
update
Willaaaaaaa 5d37ce8
fix: ctrl+c behavior in obclient
Willaaaaaaa 79c2dab
update: follow @nautaa's advice
Willaaaaaaa 0c833af
rm console print in obclient and oblsm_cli
Willaaaaaaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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,87 @@ | ||
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. | ||
miniob is licensed under Mulan PSL v2. | ||
You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
You may obtain a copy of Mulan PSL v2 at: | ||
http://license.coscl.org.cn/MulanPSL2 | ||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
See the Mulan PSL v2 for more details. */ | ||
|
||
// | ||
// Created by Willaaaaaaa in 2025 | ||
// | ||
|
||
#include "common/linereader/line_reader.h" | ||
#include "common/lang/string.h" | ||
|
||
namespace common { | ||
MiniobLineReader::MiniobLineReader() : history_file_(""), previous_history_save_time_(0), history_save_interval_(5) {} | ||
|
||
MiniobLineReader::~MiniobLineReader() { reader_.history_save(history_file_); } | ||
|
||
MiniobLineReader &MiniobLineReader::instance() | ||
{ | ||
static MiniobLineReader instance; | ||
return instance; | ||
} | ||
|
||
void MiniobLineReader::init(const std::string &history_file) | ||
{ | ||
history_file_ = history_file; | ||
reader_.history_load(history_file_); | ||
} | ||
|
||
std::string MiniobLineReader::my_readline(const std::string &prompt) | ||
{ | ||
const char *cinput = nullptr; | ||
cinput = reader_.input(prompt); | ||
if (cinput == nullptr) { | ||
return ""; | ||
} | ||
|
||
std::string line = cinput; | ||
cinput = nullptr; | ||
|
||
if (line.empty()) { | ||
return ""; | ||
} | ||
|
||
bool is_valid_input = false; | ||
for (auto c : line) { | ||
if (!isspace(c)) { | ||
is_valid_input = true; | ||
break; | ||
} | ||
} | ||
|
||
if (is_valid_input) { | ||
reader_.history_add(line); | ||
check_and_save_history(); | ||
} | ||
|
||
return line; | ||
} | ||
|
||
bool MiniobLineReader::is_exit_command(const std::string &cmd) | ||
{ | ||
std::string lower_cmd = cmd; | ||
common::str_to_lower(lower_cmd); | ||
|
||
bool is_exit = lower_cmd.compare(0, 4, "exit") == 0 || lower_cmd.compare(0, 3, "bye") == 0 || | ||
lower_cmd.compare(0, 2, "\\q") == 0 || lower_cmd.compare(0, 11, "interrupted") == 0; | ||
|
||
return is_exit; | ||
} | ||
|
||
bool MiniobLineReader::check_and_save_history() | ||
{ | ||
time_t current_time = time(nullptr); | ||
if (current_time - previous_history_save_time_ > history_save_interval_) { | ||
reader_.history_save(history_file_); | ||
previous_history_save_time_ = current_time; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} // namespace common |
This file contains hidden or 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,75 @@ | ||
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. | ||
miniob is licensed under Mulan PSL v2. | ||
You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
You may obtain a copy of Mulan PSL v2 at: | ||
http://license.coscl.org.cn/MulanPSL2 | ||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
See the Mulan PSL v2 for more details. */ | ||
|
||
// | ||
// Created by Willaaaaaaa in 2025 | ||
// | ||
|
||
#ifndef COMMON_LINE_READER_H | ||
#define COMMON_LINE_READER_H | ||
|
||
#include "replxx.hxx" | ||
|
||
namespace common { | ||
/** | ||
* @brief A singleton class for line-reading, providing an interface to replxx。 | ||
*/ | ||
class MiniobLineReader | ||
{ | ||
// The following private methods guarantee it's Singleton pattern | ||
private: | ||
MiniobLineReader(); | ||
~MiniobLineReader(); | ||
MiniobLineReader(const MiniobLineReader &) = delete; | ||
MiniobLineReader &operator=(const MiniobLineReader &) = delete; | ||
|
||
public: | ||
/** | ||
* @brief Get the singleton instance | ||
* @return Reference to the singleton instance | ||
*/ | ||
static MiniobLineReader &instance(); | ||
|
||
/** | ||
* @brief Initialize the lineReader with history file | ||
* @param history_file path/to/file | ||
*/ | ||
void init(const std::string &history_file); | ||
|
||
/** | ||
* @brief Read a line from input | ||
* @param prompt The prompt to display | ||
* @return input string | ||
*/ | ||
std::string my_readline(const std::string &prompt); | ||
|
||
/** | ||
* @brief Check if the command is an exit command | ||
* @param cmd The input command | ||
* @return True if the command is an exit command | ||
*/ | ||
bool is_exit_command(const std::string &cmd); | ||
|
||
private: | ||
/** | ||
* @brief Check if history should be auto-saved | ||
* @return True if history should be saved due to time interval | ||
*/ | ||
bool check_and_save_history(); | ||
|
||
private: | ||
replxx::Replxx reader_; | ||
std::string history_file_; | ||
time_t previous_history_save_time_; | ||
int history_save_interval_; | ||
}; | ||
} // namespace common | ||
|
||
#endif // COMMON_LINE_READER_H |
This file contains hidden or 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 contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.