teditor  1.8.0@@fee5e94
Terminal based editor written in C++
option.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdlib.h>
4 #include <string>
5 #include "utils.h"
6 #include "file_utils.h"
7 #include <vector>
8 
9 namespace teditor {
10 
14 class Option {
15  public:
17  enum Type {
23  }; // enum Type
24 
32  Option(const std::string& n, const std::string& v, const std::string& hm,
33  Type t) : name(n), value(v), helpMsg(hm), type(t) {}
34 
39  bool getBool() const {
40  ASSERT(type == Boolean, "Option '%s' is not Boolean!", name.c_str());
41  return value == "YES";
42  }
43 
44  int getInt() const {
45  ASSERT(type == Integer, "Option '%s' is not Integer!", name.c_str());
46  return str2num(value);
47  }
48 
49  float getReal() const {
50  ASSERT(type == Real, "Option '%s' is not Real!", name.c_str());
51  return str2real(value);
52  }
53 
54  std::string getStr() const;
55 
56  char getChar() const {
57  ASSERT(type == Char, "Option '%s' is not Char!", name.c_str());
58  return value[0];
59  }
69  static void add(const std::string& n, const std::string& v,
70  const std::string& hm, Type t);
71 
73  static void set(const std::string& n, const std::string& val);
74 
76  static Option& get(const std::string& n);
77 
79  static void dumpAll(const std::string& outfile);
80 
82  static void printOpts();
83 
84  private:
86  std::string name;
88  std::string value;
90  std::string helpMsg;
92  Type type;
93 
94  void setVal(const std::string& val) { value = val; }
95 
96  static std::string type2str(Type t);
97  static std::string expandOptions(const std::string& str);
98 }; // class Option
99 
100 
102 bool parseArgs(int argc, char** argv, std::vector<FileInfo>& files);
103 
104 } // namespace teditor
teditor::watch::ops::getWatchBuff
Buffer & getWatchBuff(Editor &ed)
Definition: watch/ops.cpp:45
teditor::Option::Char
@ Char
Definition: option.h:22
teditor::ledger::Parser::topAccounts
Accounts topAccounts(bool sort=true) const
Computes balances of only the top-level accounts. Top-level account is the account name that preceeds...
Definition: ledger/parser.cpp:134
MESSAGE
#define MESSAGE(ed, fmt,...)
Definition: logger.h:50
teditor::calc::addCmd
void addCmd(const std::string &c)
Definition: extensions/calc/mode.cpp:22
teditor::calc::ops::isOnSeparator
bool isOnSeparator(Buffer &buf)
Definition: calc/ops.cpp:100
teditor::options
Options & options()
Definition: option.cpp:35
teditor::readFileInfo
FileInfo readFileInfo(const std::string &arg)
Definition: file_utils.cpp:274
teditor::StringChoices
Definition: cmd_msg_bar.h:38
teditor::Option::Option
Option(const std::string &n, const std::string &v, const std::string &hm, Type t)
Define a new option.
Definition: option.h:32
teditor::parseRcFile
void parseRcFile(const std::string &rc)
Definition: option.cpp:165
teditor::calc::ops::isOnPrompt
bool isOnPrompt(Buffer &buf, bool includeTrailingSpace=false)
Definition: calc/ops.cpp:92
logger.h
teditor::timeToDateStr
std::string timeToDateStr(const TimePoint &pt)
Definition: time_utils.cpp:39
teditor::todo::ops::getTodoShowBuff
Buffer & getTodoShowBuff(Editor &ed)
Definition: todo/ops.cpp:34
teditor::str2num
int str2num(const std::string &str)
Definition: utils.cpp:73
teditor::Option::add
static void add(const std::string &n, const std::string &v, const std::string &hm, Type t)
Define a new option.
Definition: option.cpp:51
teditor::isCurrentOrParentDir
bool isCurrentOrParentDir(const std::string &dir)
Definition: file_utils.cpp:302
teditor::Option::dumpAll
static void dumpAll(const std::string &outfile)
Definition: option.cpp:58
teditor::watch::WatchMode
Definition: extensions/watch/mode.h:13
teditor::Option::getBool
bool getBool() const
Definition: option.h:39
teditor::Option::printOpts
static void printOpts()
Definition: option.cpp:73
CMBAR_MSG
#define CMBAR_MSG(ed, fmt,...)
Definition: logger.h:52
teditor::Mode::inferMode
static std::string inferMode(const std::string &file, bool isDir)
Helper to infer mode name from the file.
Definition: core/mode.cpp:40
teditor::Option::getReal
float getReal() const
Definition: option.h:49
teditor::check_output
CmdStatus check_output(const std::string &cmd)
Definition: utils.cpp:110
teditor::Buffer::getPoint
const Point & getPoint() const
Definition: buffer.h:165
teditor::calc::ops::isPromptLine
bool isPromptLine(Buffer &buf)
Definition: calc/ops.cpp:85
teditor::ledger::Parser::minmaxDates
void minmaxDates(TimePoint &min, TimePoint &max) const
Definition: ledger/parser.cpp:177
mode.h
teditor::ledger::ops::getLedgerShowBuff
Buffer & getLedgerShowBuff(Editor &ed)
Definition: ledger/ops.cpp:31
teditor::Option::Boolean
@ Boolean
Definition: option.h:18
teditor::dir::ops::DEF_CMD
DEF_CMD(OpenFile, "dirmode-open-file", "dir_ops", DEF_OP() { auto &buf=ed.getBuff();const auto &cu=buf.getPoint();auto file=buf.dirModeGetFileAtLine(cu.y);auto &dir=buf.getFileName();if(file=="."||file.empty()) return;if(file=="..") { ed.load(dirname(dir), 0);return;} ed.load(dir+'/'+file, 0);})
teditor::watch::ops::DEF_CMD
DEF_CMD(Watch, "watch", "ledger_ops", DEF_OP() { auto cmd=ed.prompt("Command to watch: ");if(cmd.empty()) return;CMBAR_MSG(ed, "Starting watch on '%s'...\n", cmd.c_str());auto &buf=getWatchBuff(ed);ed.switchToBuff("*watch");auto *mode=buf.getMode< watch::WatchMode >("watch");mode->start(&buf, cmd);})
teditor::Buffer::getMode
ModeT * getMode(const std::string &name)
Definition: buffer.h:216
teditor::Line::get
const std::string & get() const
Definition: line.h:51
teditor::calc::ops::getCalcBuff
Buffer & getCalcBuff(Editor &ed)
Definition: calc/ops.cpp:67
objects.h
teditor::Option::set
static void set(const std::string &n, const std::string &val)
Definition: option.cpp:47
teditor::Buffer::at
Line & at(int idx)
Definition: buffer.h:98
teditor::calc::ops::prompt
const std::string & prompt()
Definition: calc/ops.cpp:75
isearch.h
parser.h
teditor::Option::getChar
char getChar() const
Definition: option.h:56
command.h
teditor::ledger::ops::showTopAccounts
void showTopAccounts(Buffer &buf, const std::string &file)
Definition: ledger/ops.cpp:50
DEF_OP
#define DEF_OP()
Definition: command.h:55
teditor::copyFile
void copyFile(const std::string &in, const std::string &out)
Definition: file_utils.cpp:257
teditor::grep::ops::DEF_CMD
DEF_CMD(Grep, "grep", "grep_ops", DEF_OP() { auto cmd=ed.prompt("Run grep (like this): ", nullptr, nullptr, Option::get("grepCmd").getStr());if(cmd.empty()) { CMBAR_MSG(ed, "grep: nothing to run!\n");return;} CMBAR_MSG(ed, "grep cmd = %s\n", cmd.c_str());auto pwd=ed.getBuff().pwd();if(cmd.back()=='.') { cmd.pop_back();cmd+=pwd;} auto res=check_output(cmd);if(res.status !=0) { CMBAR_MSG(ed, "grep failed. Exit status = %d\n", res.status);MESSAGE(ed, "cmd = %s\nerr = %s\n", cmd.c_str(), res.error.c_str());return;} auto &buf=getGrepBuff(ed);buf.clear();buf.insert("Grep\nCommand: "+cmd+"\npwd: "+pwd+"\n\n");buf.insert(res.output);buf.begin();ed.switchToBuff("*grep");})
utils.h
teditor::Buffer::end
void end()
Definition: buffer.cpp:718
teditor::ledger::ops::DEF_CMD
DEF_CMD(LedgerTop, "ledger::top", "ledger_ops", DEF_OP() { const auto &file=ed.getBuff().getFileName();auto &buf=getLedgerShowBuff(ed);showTopAccounts(buf, file);ed.switchToBuff("*ledger");})
teditor::todo::String
@ String
Definition: todo/parser.cpp:19
CASE
#define CASE(t)
Definition: option.cpp:90
teditor::calc::ops::printHeader
void printHeader(Buffer &buf)
Definition: calc/ops.cpp:106
teditor::ledger::ops::showAllAccounts
void showAllAccounts(Buffer &buf, const std::string &file)
Definition: ledger/ops.cpp:75
teditor::Options
std::unordered_map< std::string, std::shared_ptr< Option > > Options
Definition: option.cpp:33
teditor::Option::String
@ String
Definition: option.h:21
teditor::Option::getInt
int getInt() const
Definition: option.h:44
teditor::calc::ops::insertChar
void insertChar(Buffer &buf, char c, Editor &ed)
Definition: calc/ops.cpp:113
teditor::expandEnvVars
std::string expandEnvVars(const std::string &str)
Definition: utils.cpp:219
teditor::Editor::getBuff
Buffer & getBuff()
Definition: editor.h:28
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::watch::WatchMode::start
void start(Buffer *buf, const std::string &cmd, int sleepLenMs=0)
Definition: extensions/watch/mode.cpp:35
teditor::str2real
float str2real(const std::string &str)
Definition: utils.cpp:80
file_utils.h
teditor::TimePoint
std::chrono::system_clock::time_point TimePoint
Definition: time_utils.h:7
teditor::ledger::ops::printHeader
void printHeader(Buffer &buf, const TimePoint &min, const TimePoint &max)
Definition: ledger/ops.cpp:39
teditor::Editor
Definition: editor.h:23
teditor::weekFor
void weekFor(TimePoint &start, TimePoint &end, const TimePoint &pt)
Definition: time_utils.cpp:154
teditor::todo::ops::DEF_CMD
DEF_CMD(TodoOpen, "todo-open", "ledger_ops", DEF_OP() { auto todoFile=Option::get("todo:file").getStr();ed.load(todoFile, 0);})
teditor::calc::ops::lineSeparator
const std::string & lineSeparator()
Definition: calc/ops.cpp:80
time_utils.h
teditor::ledger::Parser
Definition: ledger/parser.h:10
teditor::todo::Parser
Definition: todo/parser.h:9
teditor::Mode::createMode
static ModePtr createMode(const std::string &mode)
Helper to create mode object of the named mode.
Definition: core/mode.cpp:48
mode.h
teditor::parseArgs
bool parseArgs(int argc, char **argv, std::vector< FileInfo > &files)
Definition: option.cpp:184
teditor::Option::getStr
std::string getStr() const
Definition: option.cpp:26
teditor::ledger::Parser::allAccounts
Accounts allAccounts(bool sort=true) const
Computes balances of the top-level accounts as well as all accounts.
Definition: ledger/parser.cpp:149
parser.h
teditor::rel2abs
std::string rel2abs(const std::string &pwd, const std::string &rel)
Definition: file_utils.cpp:118
teditor::Buffer::setMode
void setMode(ModePtr m)
Definition: buffer.h:213
teditor::todo::ops::showTodosFor
void showTodosFor(Buffer &buf, const TimePoint &start, const TimePoint &end)
Definition: todo/ops.cpp:47
teditor::registerAllOptions
void registerAllOptions()
Definition: option.cpp:104
teditor::Option
Class to define an option used in the editor.
Definition: option.h:14
parser.h
teditor::calc::vars
VarMap & vars()
Definition: extensions/calc/mode.cpp:11
teditor::calc::ops::DEF_CMD
DEF_CMD(Calc, "calc", "calc_ops", DEF_OP() { auto &buf=getCalcBuff(ed);printHeader(buf);ed.switchToBuff("*calc");})
teditor::todo::findMatchesIn
CalendarMatches findMatchesIn(const CalendarItems &items, const TimePoint &start, const TimePoint &end)
Definition: objects.cpp:54
teditor::Buffer::insert
virtual void insert(char c)
Definition: buffer.cpp:25
teditor::Option::get
static Option & get(const std::string &n)
Definition: option.cpp:40
option.h
editor.h
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::dirname
std::string dirname(const std::string &file)
Definition: file_utils.cpp:139
teditor::grep::ops::getGrepBuff
Buffer & getGrepBuff(Editor &ed)
Definition: grep/ops.cpp:33
teditor::Option::Type
Type
Definition: option.h:17
teditor::Option::Real
@ Real
Definition: option.h:20
teditor::todo::Parser::items
const CalendarItems & items() const
Definition: todo/parser.h:15
teditor::sortedKeys
std::vector< K > sortedKeys(const std::unordered_map< K, V > &map)
Definition: utils.h:149
teditor::calc::cmds
History & cmds()
Definition: extensions/calc/mode.cpp:16
teditor::isFile
bool isFile(const std::string &f)
Definition: file_utils.cpp:43
teditor::Buffer
Definition: buffer.h:25
teditor::Option::Integer
@ Integer
Definition: option.h:19
teditor::Buffer::clear
virtual void clear()
Definition: buffer.cpp:173
teditor
Definition: any.hpp:10