ClickHouse/base/common/LineReader.h

67 lines
2.0 KiB
C++
Raw Normal View History

#pragma once
#include <common/types.h>
2020-01-10 13:26:23 +00:00
#include <atomic>
2020-01-01 19:22:57 +00:00
#include <vector>
#include <optional>
2020-01-01 19:22:57 +00:00
class LineReader
{
public:
2020-02-25 08:30:11 +00:00
struct Suggest
2020-01-01 19:22:57 +00:00
{
using Words = std::vector<std::string>;
using WordsRange = std::pair<Words::const_iterator, Words::const_iterator>;
Words words;
2020-03-21 05:17:12 +00:00
Words words_no_case;
2020-01-01 19:22:57 +00:00
std::atomic<bool> ready{false};
/// Get iterators for the matched range of words if any.
std::optional<WordsRange> getCompletions(const String & prefix, size_t prefix_length) const;
2020-01-01 19:22:57 +00:00
};
using Patterns = std::vector<const char *>;
LineReader(const String & history_file_path, bool multiline, Patterns extenders, Patterns delimiters);
2020-01-23 08:18:19 +00:00
virtual ~LineReader() {}
/// Reads the whole line until delimiter (in multiline mode) or until the last line without extender.
/// If resulting line is empty, it means the user interrupted the input.
/// Non-empty line is appended to history - without duplication.
/// Typical delimiter is ';' (semicolon) and typical extender is '\' (backslash).
String readLine(const String & first_prompt, const String & second_prompt);
2020-02-25 08:30:11 +00:00
/// When bracketed paste mode is set, pasted text is bracketed with control sequences so
/// that the program can differentiate pasted text from typed-in text. This helps
/// clickhouse-client so that without -m flag, one can still paste multiline queries, and
/// possibly get better pasting performance. See https://cirw.in/blog/bracketed-paste for
/// more details.
virtual void enableBracketedPaste() {}
2020-01-23 08:18:19 +00:00
protected:
2019-12-27 13:11:29 +00:00
enum InputStatus
{
ABORT = 0,
RESET_LINE,
INPUT_LINE,
};
const String history_file_path;
static constexpr char word_break_characters[] = " \t\v\f\a\b\r\n`~!@#$%^&*()-=+[{]}\\|;:'\",<.>/?";
2020-01-23 08:18:19 +00:00
String input;
private:
bool multiline;
Patterns extenders;
Patterns delimiters;
2020-01-23 08:18:19 +00:00
String prev_line;
2020-01-23 08:18:19 +00:00
virtual InputStatus readOneLine(const String & prompt);
virtual void addToHistory(const String &) {}
};