32 lines
969 B
C++
32 lines
969 B
C++
#pragma once
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "Completion.h"
|
|
|
|
// Reads one line with cursor editing, history (up/down) and tab completion.
|
|
// Falls back to plain std::getline when stdin/stdout are not a terminal.
|
|
class LineEditor {
|
|
public:
|
|
using CompletionFn = std::function<Completion(const std::string& textBeforeCursor)>;
|
|
|
|
LineEditor();
|
|
|
|
void setCompleter(const CompletionFn& completer);
|
|
void setHistoryFile(const std::string& path); // loads it and appends new entries
|
|
void addHistory(const std::string& line);
|
|
|
|
// Returns false on end of input (Ctrl-D on an empty line, or closed stdin).
|
|
bool readLine(const std::string& prompt, std::string& line);
|
|
|
|
private:
|
|
bool readPlain(const std::string& prompt, std::string& line);
|
|
bool readInteractive(const std::string& prompt, std::string& line);
|
|
|
|
CompletionFn completer_;
|
|
std::vector<std::string> history_;
|
|
std::string historyFile_;
|
|
};
|