teditor  1.8.0@@fee5e94
Terminal based editor written in C++
cmd_msg_bar.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "buffer.h"
4 #include "utils.h"
5 
6 
7 namespace teditor {
8 
10 typedef bool(*ChoicesFilter)(const std::string&, const std::string&);
11 
12 
13 class CmdMsgBar;
14 
15 class Choices {
16 public:
17  Choices(ChoicesFilter cf): filter(cf), choiceIdx(-1) {}
18  virtual ~Choices() {}
19  virtual const std::string& at(int idx) const = 0;
20  virtual int size() const = 0;
21  virtual bool updateChoices(const std::string& str) { return false; }
22  virtual std::string getFinalStr(int idx, const std::string& str) const = 0;
23  virtual void updateMainBuffer(CmdMsgBar& cmBar) {}
24  virtual void resetLocations(CmdMsgBar& cmBar);
25  bool match(const std::string& line, const std::string& str) const;
26  bool match(int idx, const std::string& str) const;
27  void setChoiceIdx(int idx) { choiceIdx = idx; }
28  int getChoiceIdx() const { return choiceIdx; }
29 
30 private:
31  ChoicesFilter filter;
32  // only used to communicate the final choice of the user to the extensions
33  // that create these choice objects!
34  int choiceIdx;
35 };
36 
37 
38 class StringChoices: public Choices {
39 public:
41  const std::string& at(int idx) const { return options[idx]; }
42  std::string getFinalStr(int idx, const std::string& str) const;
43  int size() const { return (int)options.size(); }
44 
45 protected:
47 };
48 
49 
50 class CmdMsgBar: public Buffer {
51 public:
52  CmdMsgBar();
53  void insert(char c) override;
54  void insert(const std::string& str) override;
55  void draw(Editor& ed, const Window& win) override;
56  int drawLine(int y, const std::string& line, Editor& ed, int lineNum,
57  const Window& win) override;
58  void load(const std::string& file, int line=0) override {}
59  bool save(const std::string& fName="") override { return false; }
60  void clear() override;
61  void setMinLoc(int loc) { minLoc = loc; }
62  int getMinStartLoc() const override { return minLoc; }
63  int totalLinesNeeded(const Point& dim) const override;
64  void setChoices(Choices* ch) { choices = ch; }
65  void clearChoices();
66  bool usingChoices() const { return choices != nullptr; }
67  std::string getStr() const { return lines[0].get().substr(minLoc); }
68  std::string getFinalChoice() const;
69  void down();
70  void up();
71  void updateChoices();
72  int getOptLoc() const { return optLoc; }
73  void setOptLoc(int loc) { optLoc = loc; }
74  void lineUp(const Point& dim) override;
75  void lineDown() override;
76  void setStartLine(int line) { startLine = line; }
77 
78 private:
80  int minLoc;
82  Choices* choices;
84  int optLoc;
85 
86  int linesNeeded(const std::string& str, int wid) const;
87 };
88 
89 } // end namespace teditor
teditor::Choices::setChoiceIdx
void setChoiceIdx(int idx)
Definition: cmd_msg_bar.h:27
teditor::CmdMsgBar::getOptLoc
int getOptLoc() const
Definition: cmd_msg_bar.h:72
teditor::options
Options & options()
Definition: option.cpp:35
teditor::CmdMsgBar::load
void load(const std::string &file, int line=0) override
Definition: cmd_msg_bar.h:58
teditor::StringChoices
Definition: cmd_msg_bar.h:38
teditor::CmdMsgBar::down
void down()
Definition: cmd_msg_bar.cpp:146
teditor::CmdMsgBar::drawLine
int drawLine(int y, const std::string &line, Editor &ed, int lineNum, const Window &win) override
Definition: cmd_msg_bar.cpp:53
teditor::cmbar::CmBarMode::CmBarMode
CmBarMode()
Definition: cmbar.cpp:13
logger.h
teditor::CmdMsgBar::CmdMsgBar
CmdMsgBar()
Definition: cmd_msg_bar.cpp:33
teditor::cmbar::CmBarMode::getKeyCmdMap
KeyCmdMap & getKeyCmdMap()
Definition: cmbar.cpp:24
teditor::cmbar::CmBarMode::getColorMap
ColorMap & getColorMap()
Definition: cmbar.cpp:25
key_cmd_map.h
teditor::strFind
bool strFind(const std::string &line, const std::string &str)
Definition: utils.cpp:237
teditor::CmdMsgBar::setOptLoc
void setOptLoc(int loc)
Definition: cmd_msg_bar.h:73
teditor::ColorMap
Definition: colors.h:87
teditor::StringChoices::size
int size() const
Definition: cmd_msg_bar.h:43
teditor::CmdMsgBar::setStartLine
void setStartLine(int line)
Definition: cmd_msg_bar.h:76
teditor::StringChoices::StringChoices
StringChoices(const Strings &arr, ChoicesFilter cf=strFind)
Definition: cmd_msg_bar.cpp:24
teditor::Window::start
const Pos2di & start() const
Definition: window.h:58
teditor::KeyCmdMap
Definition: key_cmd_map.h:19
teditor::ChoicesFilter
bool(* ChoicesFilter)(const std::string &, const std::string &)
Definition: cmd_msg_bar.h:10
teditor::CmdMsgBar
Definition: cmd_msg_bar.h:50
teditor::CmdMsgBar::clearChoices
void clearChoices()
Definition: cmd_msg_bar.cpp:141
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::CmdMsgBar::lineDown
void lineDown() override
Definition: cmd_msg_bar.cpp:176
teditor::KeyCmdMap::resetTraversal
void resetTraversal()
Definition: key_cmd_map.h:26
teditor::Choices
Definition: cmd_msg_bar.h:15
teditor::AttrColor
Definition: colors.h:56
teditor::Buffer::at
Line & at(int idx)
Definition: buffer.h:98
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::Line::numLinesNeeded
int numLinesNeeded(int wid) const
Definition: line.cpp:49
teditor::cmbar::CmBarMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: cmbar.cpp:34
teditor::Editor::sendChar
int sendChar(int x, int y, const AttrColor &fg, const AttrColor &bg, char c)
Definition: editor.cpp:295
command.h
teditor::StringChoices::options
Strings options
Definition: cmd_msg_bar.h:46
DEF_OP
#define DEF_OP()
Definition: command.h:55
utils.h
teditor::CmdMsgBar::up
void up()
Definition: cmd_msg_bar.cpp:159
teditor::Choices::updateMainBuffer
virtual void updateMainBuffer(CmdMsgBar &cmBar)
Definition: cmd_msg_bar.h:23
teditor::Buffer::lines
std::vector< Line > lines
Definition: buffer.h:271
teditor::Choices::size
virtual int size() const =0
teditor::CmdMsgBar::clear
void clear() override
Definition: cmd_msg_bar.cpp:134
teditor::CmdMsgBar::setMinLoc
void setMinLoc(int loc)
Definition: cmd_msg_bar.h:61
teditor::Window
Definition: window.h:15
teditor::CmdMsgBar::insert
void insert(char c) override
Definition: cmd_msg_bar.cpp:108
cmd_msg_bar.h
teditor::CmdMsgBar::usingChoices
bool usingChoices() const
Definition: cmd_msg_bar.h:66
teditor::Buffer::startLine
int startLine
Definition: buffer.h:272
teditor::CmdMsgBar::totalLinesNeeded
int totalLinesNeeded(const Point &dim) const override
Definition: cmd_msg_bar.cpp:93
teditor::CmdMsgBar::getMinStartLoc
int getMinStartLoc() const override
Definition: cmd_msg_bar.h:62
teditor::CmdMsgBar::updateChoices
void updateChoices()
Definition: cmd_msg_bar.cpp:126
teditor::Buffer::cu
Point cu
Definition: buffer.h:280
buffer.h
teditor::Editor
Definition: editor.h:23
teditor::Choices::Choices
Choices(ChoicesFilter cf)
Definition: cmd_msg_bar.h:17
teditor::Pos2d< int >
teditor::cmbar::ops::DEF_CMD
DEF_CMD(Quit, "prompt-quit", "cmbar_ops", DEF_OP() { ed.requestQuitPromptLoop();})
teditor::Choices::resetLocations
virtual void resetLocations(CmdMsgBar &cmBar)
Definition: cmd_msg_bar.cpp:18
teditor::Mode
Definition: core/mode.h:27
teditor::cmbar::CmBarMode::indent
int indent(Buffer &buf, int line)
Definition: cmbar.cpp:22
teditor::Choices::updateChoices
virtual bool updateChoices(const std::string &str)
Definition: cmd_msg_bar.h:21
colors.h
teditor::StringChoices::getFinalStr
std::string getFinalStr(int idx, const std::string &str) const
Definition: cmd_msg_bar.cpp:28
teditor::CmdMsgBar::save
bool save(const std::string &fName="") override
Definition: cmd_msg_bar.h:59
teditor::CmdMsgBar::getFinalChoice
std::string getFinalChoice() const
Definition: cmd_msg_bar.cpp:82
teditor::CmdMsgBar::lineUp
void lineUp(const Point &dim) override
Definition: cmd_msg_bar.cpp:172
teditor::CmdMsgBar::draw
void draw(Editor &ed, const Window &win) override
Definition: cmd_msg_bar.cpp:37
teditor::Mode::createMode
static ModePtr createMode(const std::string &mode)
Helper to create mode object of the named mode.
Definition: core/mode.cpp:48
teditor::StringChoices::at
const std::string & at(int idx) const
Definition: cmd_msg_bar.h:41
teditor::Buffer::mode
ModePtr mode
Definition: buffer.h:278
teditor::CmdMsgBar::setChoices
void setChoices(Choices *ch)
Definition: cmd_msg_bar.h:64
window.h
teditor::Choices::getFinalStr
virtual std::string getFinalStr(int idx, const std::string &str) const =0
teditor::cmbar::CmBarMode
Definition: cmbar.cpp:11
teditor::Buffer::lineReset
void lineReset()
Definition: buffer.h:197
teditor::cmbar::CmBarMode::getColorFor
void getColorFor(AttrColor &fg, AttrColor &bg, int lineNum, int pos, const Buffer &b, bool isHighlighted)
Definition: cmbar.cpp:26
mode.h
ULTRA_DEBUG
#define ULTRA_DEBUG(fmt,...)
Definition: logger.h:74
teditor::cmbar::REGISTER_MODE
REGISTER_MODE(CmBarMode, "cmbar")
teditor::ColorMap::get
const AttrColor & get(const std::string &name) const
Definition: colors.cpp:67
teditor::Choices::match
bool match(const std::string &line, const std::string &str) const
Definition: cmd_msg_bar.cpp:10
teditor::Choices::getChoiceIdx
int getChoiceIdx() const
Definition: cmd_msg_bar.h:28
editor.h
teditor::cmbar::CmBarMode::create
static Mode * create()
Definition: cmbar.cpp:32
teditor::Pos2d::x
T x
Definition: pos2d.h:16
teditor::Choices::at
virtual const std::string & at(int idx) const =0
teditor::CmdMsgBar::getStr
std::string getStr() const
Definition: cmd_msg_bar.h:67
teditor::Window::dim
const Pos2di & dim() const
Definition: window.h:59
teditor::Buffer
Definition: buffer.h:25
teditor::Choices::~Choices
virtual ~Choices()
Definition: cmd_msg_bar.h:18
teditor
Definition: any.hpp:10