MinitScript  0.9.31 PRE-BETA
Console.cpp
Go to the documentation of this file.
1 #if defined(_MSC_VER)
2  #include <windows.h>
3 #endif
4 
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 #include <string_view>
9 #include <vector>
10 
15 
16 using std::cin;
17 using std::cout;
18 using std::endl;
19 using std::getline;
20 using std::string;
21 using std::string_view;
22 using std::unique_ptr;
23 using std::vector;
24 
28 
29 unique_ptr<Console::Logger> Console::logger;
30 
31 void Console::setLogger(Logger* logger) {
32  Console::logger = unique_ptr<Logger>(logger);
33 }
34 
36  #if defined(_MSC_VER)
37  // set Microsoft Windows console to use UTF-8
38  SetConsoleOutputCP(65001);
39  #endif
40 }
41 
42 void Console::printLine(const string_view& str)
43 {
44  // log to logger
45  if (logger != nullptr) {
46  logger->printLine(str);
47  return;
48  }
49  // nope, cout this
50  cout << str << endl;
51  cout.flush();
52 }
53 
54 void Console::print(const string_view& str)
55 {
56  // log to logger
57  if (logger != nullptr) {
58  logger->print(str);
59  return;
60  }
61  // nope, cout this
62  cout << str;
63  cout.flush();
64 }
65 
67 {
68  // log to logger
69  if (logger != nullptr) {
70  logger->printLine();
71  return;
72  }
73  // nope, cout this
74  cout << endl;
75  cout.flush();
76 }
77 
78 const string Console::readLine()
79 {
80  string line;
81  getline(cin, line);
82  return line;
83 }
84 
85 const string Console::readAll() {
86  string line;
87  string result;
88  while (cin.eof() == false && getline(cin, line)) {
89  if (StringTools::endsWith(line, "\r") == true) line = StringTools::substring(line, 0, line.size() - 1);
90  result+= line + "\n";
91  }
92  return line;
93 }
94 
95 const vector<string> Console::readAllAsArray() {
96  vector<string> result;
97  string line;
98  while (cin.eof() == false && getline(cin, line)) {
99  if (StringTools::endsWith(line, "\r") == true) line = StringTools::substring(line, 0, line.size() - 1);
100  result.push_back(line);
101  }
102  return result;
103 }
static void printLine()
Print newline to console.
Definition: Console.cpp:66
static void initialize()
Initialize.
Definition: Console.cpp:35
static const string readAll()
Read all input from console into string.
Definition: Console.cpp:85
static MINITSCRIPT_STATIC_DLL_IMPEXT unique_ptr< Logger > logger
Definition: Console.h:97
static const vector< string > readAllAsArray()
Read all input from console into string vector.
Definition: Console.cpp:95
static void print(const string_view &str)
Print given string without trainling newline to console.
Definition: Console.cpp:54
static const string readLine()
Read line from console.
Definition: Console.cpp:78
static const string substring(const string &str, int64_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:203
static const bool endsWith(const string &str, const string &suffix)
Checks if string ends with suffix.
Definition: StringTools.h:49
Time utility class.
Definition: Time.h:20