-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEdit.js
More file actions
134 lines (102 loc) · 2.87 KB
/
TextEdit.js
File metadata and controls
134 lines (102 loc) · 2.87 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
/*
I just got a bright idea... Why not make a simple text editor?!
I mean... When you just have no other way - better than nothing
Add a nice rolling number wheel for faster linenumber input
Basically 0-9 repeated, select saves digit. Oh, also empty
or otherwise denoted spot for finishing teh num input
by gib
*/
const display = require("display");
const dialog = require("dialog");
const storage = require("storage");
const displayWidth = display.width();
const displayHeight = display.height();
var b_run = true;
var lines = [];
var location = "";
function cls(){
display.fill(BRUCE_BGCOLOR);
}
function handleInput(){
if(keyboard.getEscPress()){
b_run = false;
}
}
// Rethink logic driving the location of the line so that it draws background
// below it without any tinkering with numbers or ugly lines when not needed
function debugLine(inLine){
display.drawFillRect(30, 50, 240, 25, 0);
display.setCursor(31, 51);
display.println(inLine);
}
function loadFile(fileLocation){
try{
return storage.read(fileLocation);
}
catch(ferr){
cls();
dialog.error("Error reading: " + ferr);
}
}
function numberLines(arrayOfLines){
cls();
var doLines = dialog.message("Do you want line\nnumbered with that?", {left: "Yes", right: "No"});
if(doLines === "Yes"){
for(var line = 0; line < arrayOfLines.length; line++){
arrayOfLines[line] = (line + 1) + " " + arrayOfLines[line];
}
}
return arrayOfLines
}
function askIfNewFile(){
cls();
var newFile = "";
if(lines.length){
newFile = dialog.message("Load new file?", {left: "Yes", right: "No"});
}
if(newFile === "right"){
return lines;
}
else{
location = dialog.pickFile();
var file = loadFile(location);
file = file.replace(/\r/g, "");
return file.split("\n");
}
}
function theLogic(){
lines = askIfNewFile();
var dirtyLines = numberLines(lines);
var stringified = dirtyLines.join("\n");
cls();
dialog.viewText(stringified, location);
cls();
var lineNumber = dialog.prompt("", -1, "Which line you want edited?");
cls();
try{
lineNumber = Number(lineNumber) - 1;
}
catch(lerr){
dialog.error("Error converting: " + lerr);
}
lines[lineNumber] = dialog.prompt(lines[lineNumber]);
delay(300);
cls();
var doSave = dialog.message("Save file?", {left: "Yes", right: "No"});
cls();
if(doSave === "Yes"){
storage.write(location, lines, "write");
dialog.success("File saved", true);
cls();
}
var doRepeat = dialog.message("One more?", {left: "Yes", right: "No"});
if(doRepeat === "right") b_run = false;
}
function main(){
while(b_run){
handleInput();
theLogic();
delay(16);
}
}
main();