forked from timgabets/atm-screentext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screentext.js
150 lines (133 loc) · 3.88 KB
/
screentext.js
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
149
150
function ScreenTextService(cursor){
this.screen_text = {};
this.cursor = cursor;
/**
* [init description]
* @return {[type]} [description]
*/
this.init = function(){
this.screen_text = {
'@': ' ',
'A': ' ',
'B': ' ',
'C': ' ',
'D': ' ',
'E': ' ',
'F': ' ',
'G': ' ',
'H': ' ',
'I': ' ',
'J': ' ',
'K': ' ',
'L': ' ',
'M': ' ',
'N': ' ',
'O': ' '
};
};
function setCharAt(str,index,chr)
{
if(index > str.length-1) return str;
return str.substring(0,index) + chr + str.substring(index+1);
}
/**
* [copy description]
* @param {[type]} screen_text [description]
* @return {[type]} [description]
*/
this.copy = function(screen_text)
{
for (var key in screen_text)
{
if (screen_text.hasOwnProperty(key))
{
for(i = 0; i < screen_text[key].length; i++)
{
if (screen_text[key].charAt(i) != ' ')
this.screen_text[key] = setCharAt(this.screen_text[key], i, screen_text[key].charAt(i) );
}
}
}
this.cursor.init();
};
/**
* [get description]
* @return {[type]} [description]
*/
this.get = function(){
return this.screen_text;
};
/**
* [getHTML description]
* @return {[type]} [description]
*/
this.getHTML = function(){
if(this.screen_text){
var converted = {};
for (var key in this.screen_text)
if (this.screen_text.hasOwnProperty(key)){
var ikey = key;
if (key === '@') ikey = 'at';
converted[ikey] = this.screen_text[key].split(' ').join(' ');
}
return converted;
}
};
/**
* [replaceCharAt description]
* @param {[type]} string [description]
* @param {[type]} position [description]
* @param {[type]} replacement [description]
* @return {[type]} [description]
*/
this.replaceCharAt = function(string, position, replacement){
if(string)
return string.substr(0, position) + replacement + string.substr(position + 1);
};
/**
* [add description]
* @param {[type]} text [description]
*/
this.add = function(text)
{
if(this.screen_text['@'] === undefined)
this.init();
for(var i = 0; i < text.length; i++)
{
var char = text[i];
var row = this.cursor.getPosition()['y'];
var column = this.cursor.cursor_position['x'];
this.screen_text[row] = this.replaceCharAt(this.screen_text[row], column, char);
this.cursor.move();
}
};
/**
* [put insert the character and don't move the cursor]
* @return {[type]} [description]
*/
this.put = function(text){
var stored_cursor_position = this.cursor.getPosition();
this.add(text);
this.cursor.setPosition(stored_cursor_position['y'] + stored_cursor_position['x']);
};
/**
* [isEmpty description]
* @return {[type]} [description]
*/
this.isEmpty = function(){
for (var key in this.screen_text) {
if (this.screen_text.hasOwnProperty(key))
if(this.screen_text[key] !== ' ')
return false;
}
return true;
};
/**
* [setCursorPosition description]
* @param {[type]} cursor_position [description]
*/
this.setCursorPosition = function(cursor_position){
this.cursor.setPosition(cursor_position['y'] + cursor_position['x']);
};
};
module.exports = ScreenTextService;