|
| 1 | +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. |
| 2 | +miniob is licensed under Mulan PSL v2. |
| 3 | +You can use this software according to the terms and conditions of the Mulan PSL v2. |
| 4 | +You may obtain a copy of Mulan PSL v2 at: |
| 5 | + http://license.coscl.org.cn/MulanPSL2 |
| 6 | +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, |
| 7 | +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, |
| 8 | +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 9 | +See the Mulan PSL v2 for more details. */ |
| 10 | + |
| 11 | +// |
| 12 | +// Created by Willaaaaaaa in 2025 |
| 13 | +// |
| 14 | + |
| 15 | +#include "common/linereader/line_reader.h" |
| 16 | +#include "common/lang/string.h" |
| 17 | + |
| 18 | +namespace common { |
| 19 | +MiniobLineReader::MiniobLineReader() : history_file_(""), previous_history_save_time_(0), history_save_interval_(5) {} |
| 20 | + |
| 21 | +MiniobLineReader::~MiniobLineReader() { reader_.history_save(history_file_); } |
| 22 | + |
| 23 | +MiniobLineReader &MiniobLineReader::instance() |
| 24 | +{ |
| 25 | + static MiniobLineReader instance; |
| 26 | + return instance; |
| 27 | +} |
| 28 | + |
| 29 | +void MiniobLineReader::init(const std::string &history_file) |
| 30 | +{ |
| 31 | + history_file_ = history_file; |
| 32 | + reader_.history_load(history_file_); |
| 33 | +} |
| 34 | + |
| 35 | +std::string MiniobLineReader::my_readline(const std::string &prompt) |
| 36 | +{ |
| 37 | + const char *cinput = nullptr; |
| 38 | + cinput = reader_.input(prompt); |
| 39 | + if (cinput == nullptr) { |
| 40 | + return ""; |
| 41 | + } |
| 42 | + |
| 43 | + std::string line = cinput; |
| 44 | + cinput = nullptr; |
| 45 | + |
| 46 | + if (line.empty()) { |
| 47 | + return ""; |
| 48 | + } |
| 49 | + |
| 50 | + bool is_valid_input = false; |
| 51 | + for (auto c : line) { |
| 52 | + if (!isspace(c)) { |
| 53 | + is_valid_input = true; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (is_valid_input) { |
| 59 | + reader_.history_add(line); |
| 60 | + check_and_save_history(); |
| 61 | + } |
| 62 | + |
| 63 | + return line; |
| 64 | +} |
| 65 | + |
| 66 | +bool MiniobLineReader::is_exit_command(const std::string &cmd) |
| 67 | +{ |
| 68 | + std::string lower_cmd = cmd; |
| 69 | + common::str_to_lower(lower_cmd); |
| 70 | + |
| 71 | + bool is_exit = lower_cmd.compare(0, 4, "exit") == 0 || lower_cmd.compare(0, 3, "bye") == 0 || |
| 72 | + lower_cmd.compare(0, 2, "\\q") == 0 || lower_cmd.compare(0, 11, "interrupted") == 0; |
| 73 | + |
| 74 | + return is_exit; |
| 75 | +} |
| 76 | + |
| 77 | +bool MiniobLineReader::check_and_save_history() |
| 78 | +{ |
| 79 | + time_t current_time = time(nullptr); |
| 80 | + if (current_time - previous_history_save_time_ > history_save_interval_) { |
| 81 | + reader_.history_save(history_file_); |
| 82 | + previous_history_save_time_ = current_time; |
| 83 | + return true; |
| 84 | + } |
| 85 | + return false; |
| 86 | +} |
| 87 | +} // namespace common |
0 commit comments