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
|
'use strict'
function Source (terminal) {
const fs = require('fs')
const { dialog, app } = require('electron').remote
this.path = null
this.start = function () {
this.new()
}
this.new = function () {
console.log('Source', 'Make a new file..')
this.path = null
terminal.orca.reset()
terminal.resize()
terminal.history.reset()
terminal.cursor.reset()
terminal.clock.play()
}
this.open = function () {
console.log('Source', 'Open a file..')
let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'], filters: [{ name: 'Orca Machines', extensions: ['orca'] }] })
if (!paths) { console.log('Nothing to load'); return }
this.read(paths[0])
}
this.save = function (as = false) {
console.log('Source', 'Save a file..')
if (this.path && !as) {
this.write(this.path)
} else {
this.saveAs()
}
}
this.saveAs = function () {
console.log('Source', 'Save a file as..')
dialog.showSaveDialog((path) => {
if (path === undefined) { return }
if (path.indexOf('.orca') < 0) { path += '.orca' }
terminal.source.write(path)
terminal.source.path = path
})
}
this.revert = function () {
if (!this.path) { return }
console.log('Source', 'Revert a file..')
this.read(this.path)
}
// I/O
this.write = function (path, data = this.generate()) {
console.log('Source', 'Writing ' + path)
fs.writeFile(path, data, (err) => {
if (err) { alert('An error ocurred updating the file' + err.message); console.warn(err) }
terminal.source.remember('active', path)
})
}
this.read = function (path) {
if (!path) { return }
console.log('Source', 'Reading ' + path)
fs.readFile(path, 'utf8', (err, data) => {
if (err) { console.warn(err); terminal.source.new(); return }
terminal.source.path = path
terminal.source.remember('active', path)
terminal.load(this.parse(data))
})
}
// LocalStorage
this.resume = function () {
const path = this.recall('active')
if (path) {
this.read(path)
}
}
this.remember = function (key, val) {
if (!key || !val) { return }
console.log('Source', `Remember: ${key}=${val}`)
localStorage.setItem(key, val)
}
this.recall = function (key) {
if (!key) { return }
if (localStorage.hasOwnProperty(key)) {
console.log('Source', `Recall: ${key}`)
return localStorage.getItem(key)
}
}
this.forget = function (key) {
if (!key) { return }
console.log('Source', `Forget: ${key}`)
localStorage.removeItem(key)
}
// Converters
this.generate = function (orca = terminal.orca) {
return `${orca}`
}
this.parse = function (text) {
const lines = text.split('\n')
const w = lines[0].length
const h = lines.length
const s = lines.join('\n').trim()
return terminal.orca.load(w, h, s)
}
// Etc
this.name = function (path = this.path) {
return path ? path.substr(path.lastIndexOf('/') + 1).replace('.orca', '').trim() : null
}
this.folder = function (path = this.path) {
return path ? path.substring(0, path.lastIndexOf('/')).trim() : null
}
this.toString = function () {
return this.path ? this.name() : 'blank'
}
}
module.exports = Source
|