-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram_Run.cpp
More file actions
148 lines (130 loc) · 3.33 KB
/
Program_Run.cpp
File metadata and controls
148 lines (130 loc) · 3.33 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#ifndef TERMINAL_PROGRAM_RUN_CPP
#define TERMINAL_PROGRAM_RUN_CPP
/* Terminal: Program_Run.cpp
#include "Program_Run.cpp"
*/
Program_Run::Program_Run(Terminal * term): Terminal_Program(term)
{
graphicsMode=false; //We need to convert this to ASCII mode
programName="RUN";
fileToRead=0;
currentLine=0;
output="";
vLine=0;
}
std::string Program_Run::init (Vector <std::string>* vArg)
{
output="";
active=false;
if (vArg==0)
{
return "INTERNAL ERROR: NULL ARGS\n";
}
if (vArg->size()<2)
{
return "Error: No filename specified\n";
}
else
{
fileContent="";
std::string _fileName = (*vArg)(1);
if (_fileName.size() > 0 && DataTools::isAlphaNumeric(_fileName))
{
fileName=_fileName;
if (FileManagerStatic::fileExists("storage/"+fileName))
{
fileContent = FileManagerStatic::load("storage/"+fileName);
if (fileContent.size()>0)
{
std::cout<<"EASI: Load\n";
easi.load(fileContent);
active=true;
return output;
}
}
return "ERROR: FILE DOESN'T EXIST\n";
}
else
{
fileContent="";
fileName="";
active=false;
return "ERROR: FILENAME: "+fileName+" IS NOT ALPHANUMERIC 2\n";
}
}
return "NO U\n";
}
void Program_Run::cycle() // for now this is being called directly before render()
{
if ( easi.isWaitingInput == 0 )
{
isBusy=true;
if (input.size() > 0 )
{
// make sure EASI gets the input.
easi.input=input;
input="";
}
for (int i=0;i<PROGRAM_CYCLES_PER_TICK;++i)
{
output+=easi.cycle();
}
}
else
{
isBusy=false;
}
if (easi.terminated)
{
currentLine=0;
active=false;
isBusy=false;
}
return;
}
void Program_Run::keyboardEvent (Keyboard* _keyboard)
{
if (!active) { return; }
//std::string allowedInputs = " !@#$%^&*()\"\'\\=+-/";
if (easi.isWaitingInput > 0 && _keyboard->keyWasPressed)
{
if (_keyboard->lastKey == Keyboard::ENTER)
{
easi.isWaitingInput--;
std::cout<<"INPUT is: "<<input<<"\n";
easi.vInput.push(input);
input="";
output+="\n";
_keyboard->clearAll();
}
else if (_keyboard->lastKey == Keyboard::BACKSPACE)
{
if ( input.size() > 0 ) { input.pop_back(); output+='\b'; }
_keyboard->clearAll();
}
// ANSI lets us pass backspaces, so we can just return whatever keys we recieve.
else if (DataTools::isAlphaNumeric(_keyboard->lastKey))
{
output+=_keyboard->lastKey;
input+=_keyboard->lastKey;
_keyboard->clearAll();
return;
}
}
}
// Program can return update in text mode, or entire screen in graphics mode.
// Output string should be wiped
std::string Program_Run::render()
{
//Protip: We can't necessarily return if inactive, because there may be a final render call
// or batch cycles.
// Intead just check if we have output to return.
if ( output.size()==0 )
{ return ""; }
// return a copy of output and wipe the output string.
std::string retRender = output;
output="";
return retRender;
}
#endif