-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 19 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 { | ||
LineReader MiniobLineReader::reader_; | ||
bool MiniobLineReader::is_first_call_ = true; | ||
time_t MiniobLineReader::previous_history_save_time_ = 0; | ||
int MiniobLineReader::history_save_interval_ = 5; | ||
|
||
std::string MiniobLineReader::my_readline(const std::string &prompt, const std::string &history_file) | ||
{ | ||
if (is_first_call_) { | ||
Willaaaaaaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
reader_.history_load(history_file); | ||
is_first_call_ = false; | ||
} | ||
|
||
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(history_file); | ||
} | ||
|
||
return line; | ||
} | ||
|
||
bool MiniobLineReader::is_exit_command(const std::string &cmd, const std::string &history_file) | ||
Willaaaaaaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
|
||
if (is_exit) { | ||
reader_.history_save(history_file); | ||
} | ||
|
||
return is_exit; | ||
} | ||
|
||
bool MiniobLineReader::save_history(const std::string &history_file) { return reader_.history_save(history_file); } | ||
|
||
bool MiniobLineReader::check_and_save_history(const std::string &history_file) | ||
{ | ||
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,64 @@ | ||
/* 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" | ||
using LineReader = replxx::Replxx; | ||
|
||
namespace common { | ||
class MiniobLineReader | ||
{ | ||
public: | ||
/** | ||
* @brief Read a line from input | ||
* @param prompt The prompt to display | ||
* @param history_file path/to/file | ||
* @return char* to input string or nullptr | ||
Willaaaaaaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
static std::string my_readline(const std::string &prompt, const std::string &history_file); | ||
|
||
/** | ||
* @brief Check if the command is an exit command | ||
* @param cmd The input command | ||
* @param history_file path/to/file | ||
* @return True if the command is an exit command | ||
*/ | ||
static bool is_exit_command(const std::string &cmd, const std::string &history_file); | ||
|
||
/** | ||
* @brief Save history to file | ||
* @param history_file path/to/file | ||
* @return True if save succeed | ||
*/ | ||
static bool save_history(const std::string &history_file); | ||
|
||
private: | ||
/** | ||
* @brief Check if history should be auto-saved | ||
* @param history_file path/to/file | ||
* @return True if history should be saved due to time interval | ||
*/ | ||
static bool check_and_save_history(const std::string &history_file); | ||
|
||
private: | ||
static LineReader reader_; | ||
Willaaaaaaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
static bool is_first_call_; | ||
static time_t previous_history_save_time_; | ||
static 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.