/
history.js
66 lines (52 loc) · 1.14 KB
/
history.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
// Phosphor - a browser-based microcomputer
// Copyright (c) 2017-2018 Marc Lepage
'use strict';
module.exports = class History {
constructor() {
this.clear();
}
clear() {
this.list = [];
this.list[1] = {line:''};
this.i = 1;
this.i0 = 1;
this.i1 = 1;
}
commit() {
if (this.i != this.i1) {
this.list[this.i1].line = this.list[this.i].line;
if (this.list[this.i].orig != undefined) {
this.list[this.i].line = this.list[this.i].orig;
delete this.list[this.i].orig;
}
}
}
get(i) {
return this.list[i||this.i].line;
}
max() {
return this.i1;
}
min() {
return this.i0;
}
modified(i) {
return this.list[i||this.i].orig == undefined ? false : true;
}
new() {
this.i = ++this.i1;
this.list[this.i] = {line:''};
}
next() {
return this.i == this.i1 ? false : (++this.i, true);
}
prev() {
return this.i == this.i0 ? false : (--this.i, true);
}
set(line) {
if (this.i != this.i1 && this.list[this.i].orig == undefined) {
this.list[this.i].orig = this.list[this.i].line;
}
this.list[this.i].line = line;
}
};