repl.it
@Vandesm14/

Text Editor

Nodejs

No description

fork
loading
Files
  • index.js
  • chars.txt
  • demo.txt
  • prototypes.js
  • Packager files
  • package-lock.json
  • package.json
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require('fs');
const keypress = require('keypress');
const chalk = require('chalk');
const logUpdate = require('log-update');
const readline = require('readline-sync').question;

keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('keypress', function(ch, e) {
	key(e, ch);
});

var file = fs.readFileSync('demo.txt', 'utf8');
var printable = fs.readFileSync('chars.txt', 'utf8');
var char = 0;
var charEnd = 0;
var line = 0;
var lineEnd = 0;

var height = process.stdout.rows;
var width = process.stdout.columns;
var termCache = '';

var cmdMode = true; // cmd / insert
var cmd = '';
var query = '';

printFile();

function printFile() {
	let lines = file.split('\n');
	let extra = height - lines.length - 2;

	console.clear();
	for (let i in lines) {
		let str = lines[i].split('').map((el, i) => {
			// i === char ? chalk.black.bgWhite(el) : el;
			if (i === char) {
			// if (char < i && i < charEnd) {
				return chalk.black.bgWhite(el)
			} else {
				return el;
			}
		}).join('');
		if (+i === line) {
		// if (char < i && i < charEnd) {
			console.log(str);
		} else {
			console.log(lines[i]);
		}
	}
	console.log('\n'.repeat(extra - 1));
	if (cmdMode) { // cmd / insert
		console.log(':' + cmd);
	} else {
		console.log('--INSERT--');
	}
}

function exec(cmd) {
	if (!cmd) {
		printFile('Unknown Command');
	}
}

function key(key, ch) {
	let lines = file.split('\n');
	cmd = '';
	if (printable.includes(ch) && !cmdMode) {
		lines[line] = splice(lines[line], char, 0, ch);
		file = lines.join('\n');
		char++;
	} else {
		let maxLines = lines.length - 1;
		key = key || ch;
		switch (true) {
			// Navigation (Ctrl)
			case key.name === 'up' && key.ctrl:
				line = 0;
				break;
			case key.name === 'down' && key.ctrl:
				line = maxLines;
				break;
			case key.name === 'left' && key.ctrl:
				char = 0;
				break;
			case key.name === 'right' && key.ctrl:
				char = lines[line].length - 1;
				break;
			// Navigation
			case key.name === 'up':
				line--;
				break;
			case key.name === 'down':
				line++;
				break;
			case key.name === 'left':
				char--;
				break;
			case key.name === 'right':
				char++;
				break;
			// Slection
			case key.name === 'up' && key.shift:
				lineEnd--;
				break;
			case key.name === 'down' && key.shift:
				lineEnd++;
				break;
			case key.name === 'left' && key.shift:
				charEnd--;
				break;
			case key.name === 'right' && key.shift:
				charEnd++;
				break;
			// Switch mode
			case key.name === 'i':
				cmdMode = false;
				break;
			case key.name === 'escape':
				cmdMode = true;
				break;
			// Commands
			case key.name === 'f':
				cmd = 'f';
				break;
			// Editing
			case key.name === 'backspace':
				lines[line] = splice(lines[line], char - 1, 1, '');
				file = lines.join('\n');
				char--;
				break;
		}
		line = line < 0 ? 0 : line;
		line = line > maxLines ? maxLines : line;
		char = char < 0 ? 0 : char;
		char = char > lines[line].length - 1 ? lines[line].length - 1 : char;

		lineEnd = lineEnd < 0 ? 0 : lineEnd;
		lineEnd = lineEnd > maxLines ? maxLines : lineEnd;
		charEnd = charEnd < 0 ? 0 : charEnd;
		charEnd = charEnd > lines[lineEnd].length - 1 ? lines[lineEnd].length - 1 : charEnd;

		if (charEnd < char) {
			let temp = char;
			char = charEnd;
			charEnd = temp;
		}
	}
	printFile();
}

function input(str) {
	process.stdin.pause();
	let ret = readline(str);
	process.stdin.resume();
	return ret;
}

function splice(str, start, delCount, newSubStr) {
	return str.slice(0, start) + newSubStr + str.slice(start + Math.abs(delCount));
};
Fetching token
?