File: History.sc

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (564 lines) | stat: -rw-r--r-- 14,450 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
History { 		// adc 2006, Birmingham; rewrite 2007.

	classvar <>forwardFunc, <date, <startTimeStamp, <time0, listenFunc;
	classvar <>verbose = false, <>recordLocally = true, <started=false;

	classvar <>saveFolder = "~/Desktop/", <logFolder, <logFile, <logPath, <>keepsLog = true;
	classvar <>current, <>maxShortLength=65;

	var <lines, <lineShorts, <keys, <player, <hasMovedOn;

	*timeStamp {
			// hope it works on linux?
		if (thisProcess.platform.isKindOf(UnixPlatform)) {
			^Date.getDate.stamp
		} {
			// "// temporary kludge to fix Date's brokenness on windows".postln;
			^Main.elapsedTime.round(0.01)
		};
	}

	*dateString {
			// hope it works on linux?
		if (thisProcess.platform.isKindOf(UnixPlatform)) {
			^Date.getDate.asString
		} {
			// temporary kludge to fix Date's brokenness on windows
			^"__date_time_please__"
		};
	}

	*initClass {
		Class.initClassTree(TaskProxy);
		this.makeLogFolder;
		current = this.new;

		listenFunc = { |str, val, func|
			if ( func.notNil
					and: { str.notEmpty }
					and: { str != "\n" }
					and: { str.keep(7) != "History" })
				{
					if (this.verbose, { [str, val, func].postcs });
					if (this.recordLocally, {
						this.enter(thisProcess.interpreter.cmdLine)
					});
					this.forwardFunc.value(str, val, func);
				}
		};
	}

		// top level interface :

	*start {
		var interp = thisProcess.interpreter;
		if(started.not) {
			interp.codeDump = interp.codeDump.addFunc(listenFunc);
			this.hasMovedOn_(true);
			started = true;
			this.startLog;
		} {
			"History has started already.".postln;
		}
	}

	*end {
		thisProcess.interpreter.codeDump = thisProcess.interpreter.codeDump.removeFunc(listenFunc);
		this.endLog;
		this.hasMovedOn_(true);
		started = false;
	}

	*hasMovedOn_ { |flag=true| current.hasMovedOn_(flag) }
	*hasMovedOn  { ^current.hasMovedOn }

	*play { |start=0, end, verbose=true| ^current.play(start, end, verbose) }
	*stop { ^current.stop }

	*enter { |lineStr, id=\me|
		var dateNow, now;
		lineStr = this.prettyString(lineStr);

		if (lineStr.isEmpty) { ^this };	// nothing worth remembering

		if (current.lines.isEmpty) { 	// get startTime if first entry
			startTimeStamp = this.timeStamp;
			date = this.dateString;
			time0 = Main.elapsedTime;

		};

		this.hasMovedOn_(true);
		now = (Main.elapsedTime - time0);
		if (now < 1e-04) { now = 0 }; // on start

		if (keepsLog) { this.addToLog([now, id, lineStr]) };
		current.addLine(now, id, lineStr);
	}

		// forward to current for backwards compat...
	*lines { ^current.lines }
	*lineShorts { ^current.lineShorts }
		// editing
	*removeAt {|index| current.removeAt(index) }
	*removeLast { current.removeLast }
	*keep {|num| current.keep(num) }
	*drop {|num| current.drop(num) }

		// instance methods:
	*new { |lines|
		^super.new.init(lines);
	}

	hasMovedOn_ { |flag=true| hasMovedOn = flag; }

	lines_ { |inLines|
		lines.array_(inLines);
		lineShorts.array_(lines.collect { |line| this.class.shorten(line) });
		keys.clear;
		keys.addAll(lines.collect(_[1]));
	}

	clear {
		lines = List[];
		lineShorts = List[];
		hasMovedOn = true;
	}
	*clear { current.clear }

	init { |inLines|
		keys = IdentitySet.new;
		this.clear.lines_(inLines);

		player = TaskProxy.new.source_({ |e|
			var linesSize, lineIndices, lastTimePlayed;
			linesSize = lines.size;

			if (linesSize > 0) {	// reverse indexing
				lineIndices = (e.endLine.min(linesSize) .. e.startLine.min(linesSize));

				lineIndices.do { |index|
					var time, id, code, waittime;
					#time, id, code = lines[index];

					waittime = time - (lastTimePlayed ? time);
					lastTimePlayed = time;
					waittime.wait;
					if (e.verbose) { code.postln };
					code.compile.value;	// so it does not change cmdLine.
				};
			};
			0.5.wait;
			"history is over.".postln;
		}).set(\startLine, 0, \endLine, 0);
	}

	makeCurrent { History.current = this; hasMovedOn = true }
	isCurrent { ^this === History.current }

	play { |start=0, end, verbose=true|	// line numbers;
									// starting from past 0 may not work.
		start = start.clip(0, lines.lastIndex);
		end = (end ? lines.lastIndex).clip(0, lines.lastIndex);

		player.set(\startLine, start, \endLine, end, \verbose, verbose);
		player.play;
	}

	stop { player.stop; }

	addLine { |now, authID, lineStr|
		var line = [ now, authID, lineStr ];
		if (lines.isEmpty) {
			lines.add(line);
			lineShorts.add(this.class.shorten(line));
		} {
			lines.addFirst(line);
			lineShorts.addFirst(this.class.shorten(line));
		};
		keys.add(authID);
	}
		// simple editing
	removeAt { |index|
		if (index.isKindOf(Collection)) { index.sort.reverseDo (this.removeAt(_)); ^this };

		if (index < lines.size) { 	// ignore out of range indices, keep lists synced.
			[lines, lineShorts].do(_.removeAt(index));		};
		hasMovedOn = true;
	}
	removeLast { this.removeAt(lines.size - 1) }
	keep { |num|
		lines = lines.keep(num);
		lineShorts = lineShorts.keep(num);
		hasMovedOn = true;
	}
	drop { |num|
		lines = lines.drop(num);
		lineShorts = lineShorts.drop(num);
		hasMovedOn = true;
	}
		// loading from and saving to files
	*saveCS { |path, forward=false| current.saveCS(path, forward) }
	saveCS { |path, forward=false|
		var file, lines2write;

		lines2write = if (forward) { lines.reverse } { lines };
		path = path ?? { saveFolder ++ "history_" ++ this.class.timeStamp ++ ".scd" };
		file = File(path.standardizePath, "w");
		file.write(lines2write.asCompileString);
		inform("History written to:" + path);
		file.close;
	}

	*loadCS { |path, forward=false| current.loadCS(path, forward) }

	loadCS { |path, forward=false|
		var file, ll;
		protect {
			file = File(path.standardizePath, "r");
			ll = file.readAllString;
		} {
			file.close;
		};
		ll !? {
			ll = ll.compile.value;
			if (forward) { ll = ll.reverse };
			this.lines_(ll)
		};
	}

			// network setups support
	*network { }
	*localOn { recordLocally = true }
	*localOff { recordLocally = false }

			// string formatting utils
	storyString {
		var alone = lines.collectAs({ |line| line[1] }, IdentitySet).size == 1;
		var str;

		str = "///////////////////////////////////////////////////\n";
		str = str ++ format("// History, as it was on %.\n", this.class.dateString);
		str = str ++ "///////////////////////////////////////////////////\n\n";

		lines.reverseDo { |x|
			var now, id, cmdLine;
			#now, id, cmdLine = x;
				str = str ++
				format("// - % - % \n",
					this.class.formatTime(now),
					if(alone) { "" } { "(" ++ id ++ ")" }
				);
			if(cmdLine.find("\n").notNil and: { cmdLine[0] != $( }) {
				cmdLine = format("(\n%\n);", cmdLine)
			};
			str = str ++ cmdLine ++ "\n\n";
		};
		^str;
	}

	*saveStory { |path| current.saveStory(path) }

	saveStory { |path|
		var file;
		path = path ?? { saveFolder ++ "History_" ++ this.class.timeStamp ++ ".scd" };

		file = File(path.standardizePath, "w");
		file.write(this.storyString);
		file.close;
	}

	*formatTime { arg val;
			var h, m, s;
			h = val div: (60 * 60);
			val = val - (h * 60 * 60);
			m = val div: 60;
			val = val - (m * 60);
			s = val;
			^"%:%:%".format(h, m, s.round(0.01))
	}
	*unformatTime { arg str;
			var h, m, s;
			#h, m, s = str.split($:).collect(_.interpret);
			^h * 60 + m * 60 + s
	}

	*prettyString { |str|
		// remove returns at beginning or end of the string
		var startIndex = str.detectIndex({ |ch| ch != $\n });
		var endChar = str.last;
		var lastCharIndex = str.lastIndex;
		while { endChar == $\n } {
			lastCharIndex = lastCharIndex - 1;
			endChar = str[lastCharIndex];
		};
		// [startIndex, lastCharIndex].postln;
		^str.copyRange(startIndex, lastCharIndex);
	}

		// convert to shortline for gui
	*shorten { |line, maxLength|
		var  time, id, lineStr, head, length;
		#time, id, lineStr = line;
		head = (this.formatTime(time) + "-" + id + "- ");
		maxLength = maxLength ? maxShortLength;
		^head ++ lineStr.keep(maxLength  - head.size);
	}

		// not used yet
	*getTimeFromString { arg str;
		var ts, i;
		if(str.beginsWith("// - ").not) { ^nil };
		i = str.find(" - ", offset: 4);
		if(i.isNil) { i = 10 }; // assume it's ok.
		ts = str[5..i+2].postln.split($:).collect(_.asFloat);
		^ts[0] * (60 * 60) + (ts[1] * 60) + ts[2]
	}
		// not used yet
	*asLines { arg str;
		var indices;
		indices = str.findAll("// -");
		^str.clumps(indices.differentiate)
	}

	/*
	// problem: interpreter cancels backslashes etc.
	*stream { arg str, func;
		var lastTime=0, time;
		func = func ?? {
			{|str|
				var dt = ~prev / str.size;
				fork {
					0.2.wait; // wait until result from last evaluation is printed
					str.do {|char|
						char.post;
						dt.wait;
					};
					str.compile.value;

				};
			}

		};
		^Routine {
			this.asLines(str).do { |line|
				time = this.getTimeFromString(line) ? lastTime;
				(prev:lastTime, delta: time - lastTime, play: { func.(line); }).yield;
				lastTime = time;
			}
		}
	}
	*play { arg str, clock;
		str = str ? Document.current.string;
		^this.stream(str).asEventStreamPlayer.play(clock);
	}
	*playDocument {

	}
	*/

	*cmdPeriod { this.enter("// thisProcess.cmdPeriod"); }

				// log file support - global only
	*makeLogFolder {
		var supportDir = thisProcess.platform.userAppSupportDir;
		var specialFolder = supportDir ++ "/HistoryLogs";

		if (pathMatch(supportDir).isEmpty) { logFolder = ""; ^this };

		if (pathMatch(specialFolder).isEmpty) {
			specialFolder.mkdir;
			if (pathMatch(specialFolder).isEmpty) {
				logFolder = supportDir; // if not there, put it in flat
			}
		} {
			logFolder = specialFolder;
		};

		// ("// History.logFolder:" +  logFolder).postln;
	}

	*showLogFolder { openOS(logFolder) }

	*checkLogStarted {
		var isOpen;
		if (logFile.isNil) { this.startLog };

		isOpen = logFile.isOpen;
		^if (isOpen.not) { this.startLog; ^logFile.isOpen } { true };
	}

	*startLog {
		var timeStamp = this.timeStamp;
		var timeString = this.dateString;
		// open file with current date
		logPath = logFolder ++ "/log_History_" ++ timeStamp ++ ".scd";
		logFile = File(logPath, "w");
		if (logFile.isOpen) {
			logFile.write(format("// History, as it was on %.\n\n",
				timeString) ++ "[\n" /*]*/ );
			"// History.logFile opened.".inform;
		} {
			"// History: could not open logFile!".warn;
		};
	}

	*addToLog { |line|
		// add a single line
		if (this.checkLogStarted) {
			try {
				logFile.write(line.asCompileString ++ ",\n")
			} {
				"// History: could not write to logFile!".warn;
			}
		} {
			warn("// History: logFile is not open!");
		};
	}

	*endLog {
		// close file
		try { logFile.write( /*[*/ "];") };
		try { logFile.close; "// History.logFile closed.".inform; };
	}

	*showLogFile { Document.open(this.logPath) }

	matchKeys { |key|
		var indices = [];
		if (key == \all) { ^(0..lines.size - 1) };
		if (key.isNil) { ^nil };

			// list of keys:
		if (key.isArray) {
			lines.do { |line, i| if (key.includes(line[1])) { indices = indices.add(i) } }
		} {
			lines.do { |line, i| if (line[1] == key) { indices = indices.add(i) } }
		};
		^indices
	}

	matchString { |str, ignoreCase=true|
		var indices = [];
		if (str.notNil and: (str != "")) {
			lines.do { |line, i| if (line[2].find(str, ignoreCase).notNil) { indices = indices.add(i) } };
			^indices
		} { ^nil }
	}

	indicesFor { |keys, string=""|
		var indicesK, indicesS, indicesFound;
		indicesK = this.matchKeys(keys);
		indicesS = this.matchString(string);
	//	[\indicesK, indicesK, \indicesS, indicesS].postln;

		indicesFound = if (indicesK.notNil) {
			if (indicesS.notNil) { indicesK.sect(indicesS) } { indicesK }
		} {
			if (indicesS.notNil) { indicesS } { (0.. lines.size - 1) }
		};
		^indicesFound
	}

	*makeWin { |where, textHeight=12| ^current.makeWin(where, textHeight) }

	makeWin { |where, textHeight=12| ^HistoryGui(this, where, textHeight) }

	*document { current.document }

	document { arg title="";	// platform dependent ...
		var docTitle;
		Platform.case(
			\windows, {
				this.storyString.newTextWindow("History_documented");
			},
			{
				docTitle = title ++ Date.getDate.format("%Y-%m-%e-%Hh%M-History");
				Document.new(docTitle, this.storyString)
					.path_(docTitle); // don't lose title.
			}
		)
	}

	*readFromDoc { |path|
		var file, line, count = 0, lineStrings = [], comLineIndices = [], splitPoints;
		file = File(path.standardizePath, "r");

		if (file.isOpen.not) {
			("History: file" + path + "not found!").warn;
			^false
		};
			// read all lines, keep indices of commentlines
		while { line = file.getLine; line.notNil } {
			lineStrings = lineStrings.add(line);
			if (line.beginsWith("// - ")) {
				splitPoints = line.findAll(" - ");
				comLineIndices = comLineIndices.add([count] ++ splitPoints);
			};
			count = count + 1;
		};

		^comLineIndices.collect { |list, i|
			var lineIndex, sep1, sep2, nextComIndex;
			var comLine, timeStr, time, key, codeStr;
			#lineIndex, sep1, sep2 = list;

			comLine = lineStrings[lineIndex];
			timeStr = comLine.copyRange(sep1 + 3, sep2 - 1);
			time = History.unformatTime(timeStr);

			key = comLine.copyToEnd(sep2 + 3).select(_.isAlphaNum).asSymbol;
			nextComIndex = (comLineIndices[i + 1] ? [lineStrings.size]).first;

			codeStr = lineStrings.copyRange(lineIndex + 1, nextComIndex - 2).join;

			[time, key, codeStr];
		};
	}
	*checkPath { |path|
		var ext = path.splitext[1];
		if ([\sc, \scd, \txt, \nil, \rtf].includes(ext.asSymbol)) {
			^true
		} {
			warn("History: file format" + ext + "for story files likely not supported!				Please use .txt, .scd, or other text format.");
			^false
		};
	}
		// load file saved with saveStory
	*loadStory { |path| current.loadStory(path) }

	loadStory { |path|
		var lines;
		if (this.class.checkPath(path)) {
			lines = this.class.readFromDoc(path);
			if (lines == false) {
				warn("History: no lines, so could not be loaded.")
			} {
				this.lines_(lines.reverse)
			}
		};
	}

	*rewrite { |path, open = true|
		var lines, time, repath, file2;
		lines = path.load;

		if (lines.isNil) { "no history, no future.".warn; ^this };

		time = path.basename.splitext.first.keep(-13).split($_).collect { |str, i|
			str.clump(2).join("-:"[i]);
		}.join($ );

		repath = path.splitext.collect { |str, i| str ++ ["_rewritten.", ""][i] }.join;

		file2 = File.open(repath, "w");
		file2.write("// History rewritten from" + time);
		lines.do { |line|
			var time, tag, code;
			#time, tag, code = line;
			file2.write("\n\n\n// when: % - who: % \n\n(\n%\n)\n".format(time, tag, code));
		};
		file2.close;
		if (open) { repath.openTextFile };
	}
}