summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/directives/FreshlineDirective.java
blob: dedbbacf30487fd49973171efa5e44933c62418c (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
package bjc.utils.ioutils.format.directives;

import java.io.*;

import bjc.utils.ioutils.format.*;

/**
 * Implement the & directive.
 *
 * This directive will print out a specified number of newlines, and will print
 * one less if the last thing printed was a newline.
 *
 * @author Ben Culkin
 */
public class FreshlineDirective implements Directive {
	@Override
	public Edict compile(CompileContext compCTX) {
		CLParameters params = compCTX.decr.parameters;

		CLValue times = CLValue.nil();
		if (params.length() >= 1) {
			params.mapIndices("count");
			times = params.resolveKey("count");
		}

		return new FreshlineEdict(times);
	}
}

class FreshlineEdict implements Edict {
	private CLValue times;

	public FreshlineEdict(CLValue times) {
		this.times = times;
	}

	@Override
	public void format(FormatContext formCTX) throws IOException {
		int nTimes = times.asInt(formCTX.items, "occurance count", "&", 1);

		if (formCTX.writer.isLastCharNL()) nTimes -= 1;

		for (int i = 0; i < nTimes; i++) formCTX.writer.write("\n");
	}
}