summaryrefslogtreecommitdiff
path: root/js/matrix.js
blob: 08f5a790b607a57613d3857066d32e898282a3fe (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function Matrix (w,h,f){
	this.x = 0
	this.y = 0
	this.w = w
	this.h = h
	this.f = f
	this.initialize()
}
Matrix.prototype.initialize = function(){
	var w = this.w || 1, h = this.h || 1, f = this.f
	var aa = new Array (h)
	for (var y = 0; y < h; y++) {
		aa[y] = new Array (w)
		for (var x = 0; x < w; x++) {
			aa[y][x] = f(x,y)
		}
	}
	this.aa = aa
}
Matrix.prototype.rebuild = function (){
	this.demolish()
	this.initialize()
	this.append()
	this.bind()
	this.generate && this.generate()
}
Matrix.prototype.bind = function () {}
Matrix.prototype.demolish = function (){
	this.forEach(function(lex){
		lex.demolish()
	})
	while (this.rapper.firstChild) {
    this.rapper.removeChild(this.rapper.firstChild);
	}
	this.aa.forEach(function(row){
		row.length = 0
	})
	this.aa.length = 0
}
Matrix.prototype.forEach = function(f){
	this.aa.forEach(function(row, y){
		row.forEach(function(lex, x){
			f(lex, x, y)
		})
	})
}
Matrix.prototype.focusLex = function(y,x){
	if (x < 0) {
		y -= 1
	}
	if (x > this.aa[0].length) {
		y += 1
	}
	this.aa[mod(y,this.h)][mod(x,this.w)].focus()
}
Matrix.prototype.clear = function(){
	this.forEach(function(lex,x,y){ lex.clear() })
}
Matrix.prototype.fill = function(fg, bg){
	this.fg = fg
	this.bg = bg
	this.forEach(function(lex,x,y){
		lex.fill(fg,bg)
		lex.build()
	})
}
Matrix.prototype.build = function(){
	this.forEach(function(lex,x,y){
		lex.build()
	})
}
Matrix.prototype.append = function(rapper){
	rapper = this.rapper = rapper || this.rapper
  this.aa.forEach(function(row, y){
  	var div = document.createElement("div")
  	row.forEach(function(lex, x) {
			div.appendChild(lex.span)
		})
		rapper.appendChild( div )
	})
}
Matrix.prototype.region = function(w,h,x,y) {
	w = w || 1
	h = h || 1
	x = x || 0
	y = y || 0
	var parent = this
	var mat = new Matrix(w, h, function(x,y){
		return parent.aa[y][x]
	})
	mat.f = this.f
	return mat
}
Matrix.prototype.setCell = function(lex,x,y){
	this.aa[y] && this.aa[y][x] && this.aa[y][x].clone(lex)
}
Matrix.prototype.getCell = function(x,y){
	if (this.aa[y] && this.aa[y][x]) return this.aa[y][x]
	else return null
}
Matrix.prototype.resize = function(w,h){
  var div, row, lex
  var f = this.f, old_h = this.h, old_w = this.w
  var rapper = this.rapper
  w = max(w, 1)
  h = max(h, 1)
  if (h < old_h) {
    for (var y = old_h; y > h; y--) {
      row = this.aa.pop()
      div = row[0].span.parentNode
      row.forEach(function(lex, x){
        lex.demolish()
      })
      div.parentNode.removeChild(div)
    }
  }
  else if (h > old_h) {
    for (var y = old_h; y < h; y++) {
      div = document.createElement("div")
      rapper.appendChild( div )
      this.aa[y] = new Array (w)
      for (var x = 0; x < w; x++) {
        lex = this.aa[y][x] = f(x,y)
        div.appendChild(lex.span)
      }
    }
  }
  
  if (w < old_w) {
    this.aa.forEach(function(row, y){
      while (row.length > w) {
        lex = row.pop()
        lex.demolish()
      }
    })
  }
  else if (w > old_w) {
    this.aa.forEach(function(row, y){
      div = row[0].span.parentNode
      for (var x = row.length; x < w; x++) {
        lex = row[x] = f(x,y)
        div.appendChild(lex.span)
      }
    })
  }
  
  this.w = w
  this.h = h
  this.bind && this.bind()
}

//

Matrix.prototype.ascii = function () {
  var lines = this.aa.map(function(row, y){
    var last, line = ""
    row.forEach(function(lex, x) {
			line += lex.ascii()
		})
    return line // .replace(/\s+$/,"")
	})
  var txt = lines.join("\n")
  return txt
}
Matrix.prototype.mirc = function () {
  var lines = this.aa.map(function(row, y){
    var last, line = ""
    row.forEach(function(lex, x) {
			if (lex.eq(last)) {
				line += lex.sanitize()
			}
			else {
				if (x > 0 && last) line += "\x03"
				line += lex.mirc()
				last = lex
			}
		})
		if (last && ! last.isClear()) { line += "\x03" }
    return line
	}).filter(function(line){ return line.length > 0 })
	return lines.join("\n")
}
Matrix.prototype.irssi = function(){
	var txt = this.mirc()
                .replace(/\%/g, '%%')
                .replace(/\\/g, '\\x5C')
                .replace(/\"/g, '\\\"')
                .replace(/\'/g, '\\\'')
                .replace(/\`/g, '\\\`')
                .replace(/\$/g, '\\$')
                // .replace(/\n\s+/g, '\n')
                // .replace(/\s+$/g, '\n')
                // .replace(/^\n+/, '')
                .replace(/\n/g, '\\n')
                .replace(/\x02/g, '\\x02')
                .replace(/\x03/g, '\\x03')
	// console.log(txt.length)
	var escaped_txt = "", kode
	for (var i = 0; i < txt.length; i++) {
		kode = txt.charCodeAt(i)
		if (kode > 0x7f) {
			kode = kode.toString(16)
			switch (kode.length) {
				case 2:
					kode = "0" + kode
				case 3:
					kode = "0" + kode
			}
			escaped_txt += "\\u" + kode
		}
		else {
			escaped_txt += txt[i]
		}
	}
  return '/exec -out printf "' + escaped_txt + '"\n'
}