aboutsummaryrefslogtreecommitdiff
path: root/src/libcmd/repl-interacter.hh
blob: c31b1a1e669ffb053def3637fe13373be5f82de7 (plain)
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
#pragma once
/// @file

#include "finally.hh"
#include "types.hh"
#include <functional>
#include <string>

namespace nix {

namespace detail {
/** Provides the completion hooks for the repl, without exposing its complete
 * internals. */
struct ReplCompleterMixin {
    virtual StringSet completePrefix(const std::string & prefix) = 0;
};
};

enum class ReplPromptType {
    ReplPrompt,
    ContinuationPrompt,
};

class ReplInteracter
{
public:
    using Guard = Finally<std::function<void()>>;

    virtual Guard init(detail::ReplCompleterMixin * repl) = 0;
    /** Returns a boolean of whether the interacter got EOF */
    virtual bool getLine(std::string & input, ReplPromptType promptType) = 0;
    virtual ~ReplInteracter(){};
};

class ReadlineLikeInteracter : public virtual ReplInteracter
{
    std::string historyFile;
public:
    ReadlineLikeInteracter(std::string historyFile)
        : historyFile(historyFile)
    {
    }
    virtual Guard init(detail::ReplCompleterMixin * repl) override;
    virtual bool getLine(std::string & input, ReplPromptType promptType) override;
    virtual ~ReadlineLikeInteracter() override;
};

class AutomationInteracter : public virtual ReplInteracter
{
public:
    AutomationInteracter() = default;
    virtual Guard init(detail::ReplCompleterMixin * repl) override;
    virtual bool getLine(std::string & input, ReplPromptType promptType) override;
    virtual ~AutomationInteracter() override = default;
};

};