aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Client/LineReader.h
blob: fc19eaa5667d75fd5d8e8d7ad1fec2d85df4759b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include <mutex>
#include <atomic>
#include <vector>
#include <optional>
#include <replxx.hxx>

#include <base/types.h>
#include <base/defines.h>

namespace DB
{

class LineReader
{
public:
    struct Suggest
    {
        using Words = std::vector<std::string>;
        using Callback = std::function<Words(const String & prefix, size_t prefix_length)>;

        /// Get vector for the matched range of words if any.
        replxx::Replxx::completions_t getCompletions(const String & prefix, size_t prefix_length, const char * word_break_characters);
        void addWords(Words && new_words);

        void setCompletionsCallback(Callback && callback) { custom_completions_callback = callback; }

    private:
        Words words TSA_GUARDED_BY(mutex);
        Words words_no_case TSA_GUARDED_BY(mutex);

        Callback custom_completions_callback = nullptr;

        std::mutex mutex;
    };

    using Patterns = std::vector<const char *>;

    LineReader(const String & history_file_path, bool multiline, Patterns extenders, Patterns delimiters);
    virtual ~LineReader() = default;

    /// 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);

    /// 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.
    /// These methods (if implemented) emit the control characters immediately, without waiting
    /// for the next readLine() call.
    virtual void enableBracketedPaste() {}
    virtual void disableBracketedPaste() {}

protected:
    enum InputStatus
    {
        ABORT = 0,
        RESET_LINE,
        INPUT_LINE,
    };

    const String history_file_path;

    String input;

    bool multiline;

    Patterns extenders;
    Patterns delimiters;

    String prev_line;

    virtual InputStatus readOneLine(const String & prompt);
    virtual void addToHistory(const String &) {}
};

}