console-toolkit 1.2.8 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -25
- package/package.json +26 -6
- package/src/alphanumeric/arrows.d.ts +48 -0
- package/src/alphanumeric/arrows.js +23 -0
- package/src/alphanumeric/fractions.d.ts +65 -0
- package/src/alphanumeric/fractions.js +49 -0
- package/src/alphanumeric/number-formatters.d.ts +91 -0
- package/src/alphanumeric/number-formatters.js +45 -1
- package/src/alphanumeric/roman.d.ts +15 -0
- package/src/alphanumeric/roman.js +12 -0
- package/src/alphanumeric/unicode-cultural-numbers.d.ts +65 -0
- package/src/alphanumeric/unicode-cultural-numbers.js +1 -0
- package/src/alphanumeric/unicode-letters.d.ts +32 -0
- package/src/alphanumeric/unicode-letters.js +8 -0
- package/src/alphanumeric/unicode-numbers.d.ts +44 -0
- package/src/alphanumeric/unicode-numbers.js +21 -0
- package/src/alphanumeric/utils.d.ts +45 -0
- package/src/alphanumeric/utils.js +26 -0
- package/src/ansi/csi.d.ts +141 -0
- package/src/ansi/csi.js +51 -2
- package/src/ansi/index.d.ts +26 -0
- package/src/ansi/sgr-constants.d.ts +173 -0
- package/src/ansi/sgr-state.d.ts +91 -0
- package/src/ansi/sgr-state.js +45 -0
- package/src/ansi/sgr.d.ts +587 -0
- package/src/ansi/sgr.js +426 -6
- package/src/box.d.ts +160 -0
- package/src/box.js +113 -12
- package/src/charts/bars/block-frac-grouped.d.ts +12 -0
- package/src/charts/bars/block-frac-grouped.js +6 -0
- package/src/charts/bars/block-frac.d.ts +34 -0
- package/src/charts/bars/block-frac.js +13 -0
- package/src/charts/bars/block-grouped.d.ts +12 -0
- package/src/charts/bars/block-grouped.js +6 -0
- package/src/charts/bars/block.d.ts +43 -0
- package/src/charts/bars/block.js +13 -0
- package/src/charts/bars/draw-grouped.d.ts +41 -0
- package/src/charts/bars/draw-grouped.js +4 -0
- package/src/charts/bars/draw-stacked.d.ts +47 -0
- package/src/charts/bars/draw-stacked.js +4 -0
- package/src/charts/bars/frac-grouped.d.ts +32 -0
- package/src/charts/bars/frac-grouped.js +13 -0
- package/src/charts/bars/plain-grouped.d.ts +12 -0
- package/src/charts/bars/plain-grouped.js +6 -0
- package/src/charts/bars/plain.d.ts +75 -0
- package/src/charts/bars/plain.js +27 -0
- package/src/charts/columns/block-frac-grouped.d.ts +12 -0
- package/src/charts/columns/block-frac-grouped.js +6 -0
- package/src/charts/columns/block-frac.d.ts +39 -0
- package/src/charts/columns/block-frac.js +13 -0
- package/src/charts/columns/block-grouped.d.ts +12 -0
- package/src/charts/columns/block-grouped.js +6 -0
- package/src/charts/columns/block.d.ts +43 -0
- package/src/charts/columns/block.js +13 -0
- package/src/charts/columns/draw-grouped.d.ts +41 -0
- package/src/charts/columns/draw-grouped.js +4 -0
- package/src/charts/columns/draw-stacked.d.ts +39 -0
- package/src/charts/columns/draw-stacked.js +4 -0
- package/src/charts/columns/frac-grouped.d.ts +37 -0
- package/src/charts/columns/frac-grouped.js +13 -0
- package/src/charts/columns/plain-grouped.d.ts +12 -0
- package/src/charts/columns/plain-grouped.js +6 -0
- package/src/charts/columns/plain.d.ts +32 -0
- package/src/charts/columns/plain.js +13 -0
- package/src/charts/themes/default.d.ts +6 -0
- package/src/charts/themes/default.js +1 -0
- package/src/charts/themes/rainbow-reversed.d.ts +6 -0
- package/src/charts/themes/rainbow-reversed.js +2 -1
- package/src/charts/themes/rainbow.d.ts +6 -0
- package/src/charts/themes/rainbow.js +1 -0
- package/src/charts/utils.d.ts +79 -0
- package/src/charts/utils.js +32 -4
- package/src/draw-block-frac.d.ts +16 -0
- package/src/draw-block-frac.js +14 -0
- package/src/draw-block.d.ts +53 -0
- package/src/draw-block.js +25 -1
- package/src/meta.d.ts +84 -0
- package/src/meta.js +64 -0
- package/src/output/show.d.ts +55 -0
- package/src/output/show.js +28 -0
- package/src/output/updater.d.ts +114 -0
- package/src/output/updater.js +58 -4
- package/src/output/writer.d.ts +87 -0
- package/src/output/writer.js +57 -5
- package/src/panel.d.ts +402 -0
- package/src/panel.js +219 -5
- package/src/plot/bitmap.d.ts +80 -0
- package/src/plot/bitmap.js +33 -4
- package/src/plot/draw-line.d.ts +13 -0
- package/src/plot/draw-line.js +8 -0
- package/src/plot/draw-rect.d.ts +13 -0
- package/src/plot/draw-rect.js +38 -30
- package/src/plot/index.d.ts +39 -0
- package/src/plot/index.js +22 -0
- package/src/plot/to-quads.d.ts +10 -0
- package/src/plot/to-quads.js +5 -0
- package/src/spinner/index.d.ts +4 -0
- package/src/spinner/index.js +0 -2
- package/src/spinner/spin.d.ts +13 -0
- package/src/spinner/spin.js +13 -2
- package/src/spinner/spinner.d.ts +69 -0
- package/src/spinner/spinner.js +30 -2
- package/src/spinner/spinners.d.ts +34 -0
- package/src/spinner/spinners.js +23 -9
- package/src/strings/clip.d.ts +21 -0
- package/src/strings/clip.js +10 -0
- package/src/strings/parse.d.ts +23 -0
- package/src/strings/parse.js +7 -0
- package/src/strings/split.d.ts +38 -0
- package/src/strings/split.js +15 -0
- package/src/strings.d.ts +44 -0
- package/src/strings.js +34 -4
- package/src/style.d.ts +462 -0
- package/src/style.js +58 -4
- package/src/symbols.d.ts +167 -0
- package/src/symbols.js +91 -7
- package/src/table/draw-borders.d.ts +38 -0
- package/src/table/draw-borders.js +10 -2
- package/src/table/index.d.ts +8 -0
- package/src/table/index.js +1 -0
- package/src/table/table.d.ts +234 -0
- package/src/table/table.js +59 -1
- package/src/themes/blocks/unicode-half.d.ts +6 -0
- package/src/themes/blocks/unicode-half.js +1 -0
- package/src/themes/blocks/unicode-thin.d.ts +6 -0
- package/src/themes/blocks/unicode-thin.js +1 -0
- package/src/themes/lines/ascii-compact.d.ts +6 -0
- package/src/themes/lines/ascii-compact.js +1 -0
- package/src/themes/lines/ascii-dots.d.ts +6 -0
- package/src/themes/lines/ascii-dots.js +1 -0
- package/src/themes/lines/ascii-girder.d.ts +6 -0
- package/src/themes/lines/ascii-girder.js +1 -0
- package/src/themes/lines/ascii-github.d.ts +6 -0
- package/src/themes/lines/ascii-github.js +1 -0
- package/src/themes/lines/ascii-reddit.d.ts +6 -0
- package/src/themes/lines/ascii-reddit.js +1 -0
- package/src/themes/lines/ascii-rounded.d.ts +6 -0
- package/src/themes/lines/ascii-rounded.js +1 -0
- package/src/themes/lines/ascii.d.ts +6 -0
- package/src/themes/lines/ascii.js +1 -0
- package/src/themes/lines/unicode-bold.d.ts +6 -0
- package/src/themes/lines/unicode-bold.js +1 -0
- package/src/themes/lines/unicode-rounded.d.ts +6 -0
- package/src/themes/lines/unicode-rounded.js +1 -0
- package/src/themes/lines/unicode.d.ts +6 -0
- package/src/themes/lines/unicode.js +1 -0
- package/src/themes/utils.d.ts +33 -0
- package/src/themes/utils.js +7 -0
- package/src/turtle/draw-line-art.d.ts +19 -0
- package/src/turtle/draw-line-art.js +7 -0
- package/src/turtle/draw-unicode.d.ts +19 -0
- package/src/turtle/draw-unicode.js +8 -0
- package/src/turtle/index.d.ts +21 -0
- package/src/turtle/index.js +8 -0
- package/src/turtle/turtle.d.ts +269 -0
- package/src/turtle/turtle.js +124 -4
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Turtle from './turtle.js';
|
|
2
|
+
import {draw} from './draw-unicode.js';
|
|
3
|
+
import Box from '../box.js';
|
|
4
|
+
|
|
5
|
+
declare module './turtle.js' {
|
|
6
|
+
interface Turtle {
|
|
7
|
+
/** Draws the turtle's path as a Box using Unicode box-drawing characters.
|
|
8
|
+
* @param options - Draw options.
|
|
9
|
+
* @returns A Box containing the drawn path.
|
|
10
|
+
*/
|
|
11
|
+
toBox(options?: {ignore?: string; useArcs?: boolean}): Box;
|
|
12
|
+
/** Draws the turtle's path as a string array using Unicode box-drawing characters.
|
|
13
|
+
* @param options - Draw options.
|
|
14
|
+
* @returns Array of strings.
|
|
15
|
+
*/
|
|
16
|
+
toStrings(options?: {ignore?: string; useArcs?: boolean}): string[];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export {Turtle, draw};
|
|
21
|
+
export default Turtle;
|
package/src/turtle/index.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import Turtle from './turtle.js';
|
|
2
2
|
import draw from './draw-unicode.js';
|
|
3
3
|
|
|
4
|
+
/** Draws the turtle's path as a Box using Unicode box-drawing characters.
|
|
5
|
+
* @param {object} [options] - Draw options.
|
|
6
|
+
* @returns {import('../box.js').default}
|
|
7
|
+
*/
|
|
4
8
|
Turtle.prototype.toBox = function (options) {
|
|
5
9
|
return draw(this, options);
|
|
6
10
|
};
|
|
11
|
+
/** Draws the turtle's path as a string array using Unicode box-drawing characters.
|
|
12
|
+
* @param {object} [options] - Draw options.
|
|
13
|
+
* @returns {string[]}
|
|
14
|
+
*/
|
|
7
15
|
Turtle.prototype.toStrings = function (options) {
|
|
8
16
|
return this.toBox(options).toStrings();
|
|
9
17
|
};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/** Turtle graphics on a grid with directional movement and line drawing.
|
|
2
|
+
* @see {@link https://github.com/uhop/console-toolkit/wiki/Module:-turtle}
|
|
3
|
+
*/
|
|
4
|
+
export class Turtle {
|
|
5
|
+
/** Direction constant: right (0). */
|
|
6
|
+
static RIGHT: 0;
|
|
7
|
+
/** Direction constant: down (1). */
|
|
8
|
+
static DOWN: 1;
|
|
9
|
+
/** Direction constant: left (2). */
|
|
10
|
+
static LEFT: 2;
|
|
11
|
+
/** Direction constant: up (3). */
|
|
12
|
+
static UP: 3;
|
|
13
|
+
|
|
14
|
+
/** Direction constant: east (0). */
|
|
15
|
+
static EAST: 0;
|
|
16
|
+
/** Direction constant: south (1). */
|
|
17
|
+
static SOUTH: 1;
|
|
18
|
+
/** Direction constant: west (2). */
|
|
19
|
+
static WEST: 2;
|
|
20
|
+
/** Direction constant: north (3). */
|
|
21
|
+
static NORTH: 3;
|
|
22
|
+
|
|
23
|
+
/** The 2D grid of cells with directional line weights. */
|
|
24
|
+
cells: ({u?: number; d?: number; l?: number; r?: number} | null)[][];
|
|
25
|
+
/** Grid width. */
|
|
26
|
+
width: number;
|
|
27
|
+
/** Grid height. */
|
|
28
|
+
height: number;
|
|
29
|
+
/** Current direction (0=right, 1=down, 2=left, 3=up). */
|
|
30
|
+
direction: number;
|
|
31
|
+
/** Current position. */
|
|
32
|
+
position: {x: number; y: number};
|
|
33
|
+
/** Current line theme/weight. */
|
|
34
|
+
theme: number;
|
|
35
|
+
/** Saved state stack. */
|
|
36
|
+
stack: [number, number, number, number][];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param width - Grid width.
|
|
40
|
+
* @param height - Grid height.
|
|
41
|
+
* @param theme - Initial line theme (default: 1).
|
|
42
|
+
*/
|
|
43
|
+
constructor(width: number, height: number, theme?: number);
|
|
44
|
+
|
|
45
|
+
/** Resets the turtle position to (0, 0).
|
|
46
|
+
* @returns This Turtle.
|
|
47
|
+
*/
|
|
48
|
+
reset(): this;
|
|
49
|
+
/** Alias for `reset`. */
|
|
50
|
+
home: Turtle['reset'];
|
|
51
|
+
|
|
52
|
+
/** Sets the turtle position.
|
|
53
|
+
* @param x - X coordinate.
|
|
54
|
+
* @param y - Y coordinate.
|
|
55
|
+
* @returns This Turtle.
|
|
56
|
+
*/
|
|
57
|
+
set(x: number, y: number): this;
|
|
58
|
+
/** Alias for `set`. */
|
|
59
|
+
goto: Turtle['set'];
|
|
60
|
+
/** Alias for `set`. */
|
|
61
|
+
setPos: Turtle['set'];
|
|
62
|
+
/** Alias for `set`. */
|
|
63
|
+
setPosition: Turtle['set'];
|
|
64
|
+
|
|
65
|
+
/** Sets the X coordinate.
|
|
66
|
+
* @param x - X coordinate.
|
|
67
|
+
* @returns This Turtle.
|
|
68
|
+
*/
|
|
69
|
+
setX(x: number): this;
|
|
70
|
+
/** Sets the Y coordinate.
|
|
71
|
+
* @param y - Y coordinate.
|
|
72
|
+
* @returns This Turtle.
|
|
73
|
+
*/
|
|
74
|
+
setY(y: number): this;
|
|
75
|
+
|
|
76
|
+
/** Moves the turtle by a relative offset without drawing.
|
|
77
|
+
* @param dx - X offset.
|
|
78
|
+
* @param dy - Y offset.
|
|
79
|
+
* @returns This Turtle.
|
|
80
|
+
*/
|
|
81
|
+
add(dx: number, dy: number): this;
|
|
82
|
+
/** Alias for `add`. */
|
|
83
|
+
advance: Turtle['add'];
|
|
84
|
+
|
|
85
|
+
/** Adds to the X coordinate.
|
|
86
|
+
* @param dx - X offset.
|
|
87
|
+
* @returns This Turtle.
|
|
88
|
+
*/
|
|
89
|
+
addX(dx: number): this;
|
|
90
|
+
/** Adds to the Y coordinate.
|
|
91
|
+
* @param dy - Y offset.
|
|
92
|
+
* @returns This Turtle.
|
|
93
|
+
*/
|
|
94
|
+
addY(dy: number): this;
|
|
95
|
+
|
|
96
|
+
/** Sets the line theme/weight.
|
|
97
|
+
* @param theme - Theme number.
|
|
98
|
+
* @returns This Turtle.
|
|
99
|
+
*/
|
|
100
|
+
setTheme(theme: number): this;
|
|
101
|
+
/** Alias for `setTheme`. */
|
|
102
|
+
setPen: Turtle['setTheme'];
|
|
103
|
+
/** Alias for `setTheme`. */
|
|
104
|
+
setWidth: Turtle['setTheme'];
|
|
105
|
+
|
|
106
|
+
/** Sets the direction.
|
|
107
|
+
* @param direction - Direction (0-3).
|
|
108
|
+
* @returns This Turtle.
|
|
109
|
+
*/
|
|
110
|
+
setDirection(direction: number): this;
|
|
111
|
+
/** Alias for `setDirection`. */
|
|
112
|
+
setDir: Turtle['setDirection'];
|
|
113
|
+
|
|
114
|
+
/** Sets direction to up.
|
|
115
|
+
* @returns This Turtle.
|
|
116
|
+
*/
|
|
117
|
+
setUp(): this;
|
|
118
|
+
/** Alias for `setUp`. */
|
|
119
|
+
setNorth: Turtle['setUp'];
|
|
120
|
+
|
|
121
|
+
/** Sets direction to down.
|
|
122
|
+
* @returns This Turtle.
|
|
123
|
+
*/
|
|
124
|
+
setDown(): this;
|
|
125
|
+
/** Alias for `setDown`. */
|
|
126
|
+
setSouth: Turtle['setDown'];
|
|
127
|
+
|
|
128
|
+
/** Sets direction to left.
|
|
129
|
+
* @returns This Turtle.
|
|
130
|
+
*/
|
|
131
|
+
setLeft(): this;
|
|
132
|
+
/** Alias for `setLeft`. */
|
|
133
|
+
setWest: Turtle['setLeft'];
|
|
134
|
+
|
|
135
|
+
/** Sets direction to right.
|
|
136
|
+
* @returns This Turtle.
|
|
137
|
+
*/
|
|
138
|
+
setRight(): this;
|
|
139
|
+
/** Alias for `setRight`. */
|
|
140
|
+
setEast: Turtle['setRight'];
|
|
141
|
+
|
|
142
|
+
/** Turns the turtle 90° left.
|
|
143
|
+
* @returns This Turtle.
|
|
144
|
+
*/
|
|
145
|
+
left(): this;
|
|
146
|
+
/** Alias for `left`. */
|
|
147
|
+
lt: Turtle['left'];
|
|
148
|
+
|
|
149
|
+
/** Turns the turtle 90° right.
|
|
150
|
+
* @returns This Turtle.
|
|
151
|
+
*/
|
|
152
|
+
right(): this;
|
|
153
|
+
/** Alias for `right`. */
|
|
154
|
+
rt: Turtle['right'];
|
|
155
|
+
|
|
156
|
+
/** Saves the current state onto the stack.
|
|
157
|
+
* @returns This Turtle.
|
|
158
|
+
*/
|
|
159
|
+
save(): this;
|
|
160
|
+
/** Restores the last saved state from the stack.
|
|
161
|
+
* @returns This Turtle.
|
|
162
|
+
*/
|
|
163
|
+
restore(): this;
|
|
164
|
+
|
|
165
|
+
/** Moves up by distance, drawing a line.
|
|
166
|
+
* @param distance - Steps.
|
|
167
|
+
* @returns This Turtle.
|
|
168
|
+
*/
|
|
169
|
+
moveUp(distance: number): this;
|
|
170
|
+
/** Alias for `moveUp`. */
|
|
171
|
+
moveNorth: Turtle['moveUp'];
|
|
172
|
+
|
|
173
|
+
/** Moves down by distance, drawing a line.
|
|
174
|
+
* @param distance - Steps.
|
|
175
|
+
* @returns This Turtle.
|
|
176
|
+
*/
|
|
177
|
+
moveDown(distance: number): this;
|
|
178
|
+
/** Alias for `moveDown`. */
|
|
179
|
+
moveSouth: Turtle['moveDown'];
|
|
180
|
+
|
|
181
|
+
/** Moves left by distance, drawing a line.
|
|
182
|
+
* @param distance - Steps.
|
|
183
|
+
* @returns This Turtle.
|
|
184
|
+
*/
|
|
185
|
+
moveLeft(distance: number): this;
|
|
186
|
+
/** Alias for `moveLeft`. */
|
|
187
|
+
moveWest: Turtle['moveLeft'];
|
|
188
|
+
|
|
189
|
+
/** Moves right by distance, drawing a line.
|
|
190
|
+
* @param distance - Steps.
|
|
191
|
+
* @returns This Turtle.
|
|
192
|
+
*/
|
|
193
|
+
moveRight(distance: number): this;
|
|
194
|
+
/** Alias for `moveRight`. */
|
|
195
|
+
moveEast: Turtle['moveRight'];
|
|
196
|
+
|
|
197
|
+
/** Moves in a direction, drawing a line.
|
|
198
|
+
* @param direction - Direction (0-3).
|
|
199
|
+
* @param distance - Steps.
|
|
200
|
+
* @returns This Turtle.
|
|
201
|
+
*/
|
|
202
|
+
move(direction: number, distance: number): this;
|
|
203
|
+
|
|
204
|
+
/** Moves the turtle forward, drawing a line.
|
|
205
|
+
* @param distance - Steps.
|
|
206
|
+
* @returns This Turtle.
|
|
207
|
+
*/
|
|
208
|
+
forward(distance: number): this;
|
|
209
|
+
/** Alias for `forward`. */
|
|
210
|
+
fd: Turtle['forward'];
|
|
211
|
+
/** Alias for `forward`. */
|
|
212
|
+
fwd: Turtle['forward'];
|
|
213
|
+
|
|
214
|
+
/** Moves the turtle backward, drawing a line.
|
|
215
|
+
* @param distance - Steps.
|
|
216
|
+
* @returns This Turtle.
|
|
217
|
+
*/
|
|
218
|
+
backward(distance: number): this;
|
|
219
|
+
/** Alias for `backward`. */
|
|
220
|
+
bk: Turtle['backward'];
|
|
221
|
+
/** Alias for `backward`. */
|
|
222
|
+
back: Turtle['backward'];
|
|
223
|
+
/** Alias for `backward`. */
|
|
224
|
+
bwd: Turtle['backward'];
|
|
225
|
+
|
|
226
|
+
/** Marks a half-step line in the given direction.
|
|
227
|
+
* @param direction - Direction (0-3).
|
|
228
|
+
* @returns This Turtle.
|
|
229
|
+
*/
|
|
230
|
+
markHalf(direction: number): this;
|
|
231
|
+
/** Marks a half-step line forward.
|
|
232
|
+
* @returns This Turtle.
|
|
233
|
+
*/
|
|
234
|
+
markHalfForward(): this;
|
|
235
|
+
/** Marks a half-step line backward.
|
|
236
|
+
* @returns This Turtle.
|
|
237
|
+
*/
|
|
238
|
+
markHalfBackward(): this;
|
|
239
|
+
|
|
240
|
+
/** Marks a half-step line up.
|
|
241
|
+
* @returns This Turtle.
|
|
242
|
+
*/
|
|
243
|
+
markHalfUp(): this;
|
|
244
|
+
/** Alias for `markHalfUp`. */
|
|
245
|
+
markHalfNorth: Turtle['markHalfUp'];
|
|
246
|
+
|
|
247
|
+
/** Marks a half-step line down.
|
|
248
|
+
* @returns This Turtle.
|
|
249
|
+
*/
|
|
250
|
+
markHalfDown(): this;
|
|
251
|
+
/** Alias for `markHalfDown`. */
|
|
252
|
+
markHalfSouth: Turtle['markHalfDown'];
|
|
253
|
+
|
|
254
|
+
/** Marks a half-step line left.
|
|
255
|
+
* @returns This Turtle.
|
|
256
|
+
*/
|
|
257
|
+
markHalfLeft(): this;
|
|
258
|
+
/** Alias for `markHalfLeft`. */
|
|
259
|
+
markHalfWest: Turtle['markHalfLeft'];
|
|
260
|
+
|
|
261
|
+
/** Marks a half-step line right.
|
|
262
|
+
* @returns This Turtle.
|
|
263
|
+
*/
|
|
264
|
+
markHalfRight(): this;
|
|
265
|
+
/** Alias for `markHalfRight`. */
|
|
266
|
+
markHalfEast: Turtle['markHalfRight'];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export default Turtle;
|
package/src/turtle/turtle.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import {addAliases} from '../meta.js';
|
|
2
2
|
|
|
3
|
+
/** Turtle graphics on a grid. Supports directional movement, line drawing, state save/restore, and various aliases.
|
|
4
|
+
* @see {@link https://github.com/uhop/console-toolkit/wiki/Package:-turtle}
|
|
5
|
+
*/
|
|
3
6
|
export class Turtle {
|
|
4
7
|
static RIGHT = 0;
|
|
5
8
|
static DOWN = 1;
|
|
@@ -11,6 +14,11 @@ export class Turtle {
|
|
|
11
14
|
static WEST = 2;
|
|
12
15
|
static NORTH = 3;
|
|
13
16
|
|
|
17
|
+
/** Creates a new Turtle on a grid of the given size.
|
|
18
|
+
* @param {number} width - Grid width.
|
|
19
|
+
* @param {number} height - Grid height.
|
|
20
|
+
* @param {number} [theme=1] - The line theme/pen width.
|
|
21
|
+
*/
|
|
14
22
|
constructor(width, height, theme = 1) {
|
|
15
23
|
this.cells = new Array(height);
|
|
16
24
|
for (let i = 0; i < height; ++i) {
|
|
@@ -25,71 +33,136 @@ export class Turtle {
|
|
|
25
33
|
this.stack = [];
|
|
26
34
|
}
|
|
27
35
|
|
|
36
|
+
/** Resets the turtle position to (0, 0).
|
|
37
|
+
* @returns {this}
|
|
38
|
+
*/
|
|
28
39
|
reset() {
|
|
29
40
|
this.position.x = this.position.y = 0;
|
|
30
41
|
return this;
|
|
31
42
|
}
|
|
43
|
+
/** Sets the turtle position (clamped to grid bounds).
|
|
44
|
+
* @param {number} x
|
|
45
|
+
* @param {number} y
|
|
46
|
+
* @returns {this}
|
|
47
|
+
*/
|
|
32
48
|
set(x, y) {
|
|
33
49
|
this.position.x = Math.max(0, Math.min(this.width - 1, x));
|
|
34
50
|
this.position.y = Math.max(0, Math.min(this.height - 1, y));
|
|
35
51
|
return this;
|
|
36
52
|
}
|
|
53
|
+
/** Sets the X coordinate.
|
|
54
|
+
* @param {number} x
|
|
55
|
+
* @returns {this}
|
|
56
|
+
*/
|
|
37
57
|
setX(x) {
|
|
38
58
|
this.position.x = Math.max(0, Math.min(this.width - 1, x));
|
|
39
59
|
return this;
|
|
40
60
|
}
|
|
61
|
+
/** Sets the Y coordinate.
|
|
62
|
+
* @param {number} y
|
|
63
|
+
* @returns {this}
|
|
64
|
+
*/
|
|
41
65
|
setY(y) {
|
|
42
66
|
this.position.y = Math.max(0, Math.min(this.height - 1, y));
|
|
43
67
|
return this;
|
|
44
68
|
}
|
|
69
|
+
/** Moves the turtle by a relative offset without drawing.
|
|
70
|
+
* @param {number} dx - X offset.
|
|
71
|
+
* @param {number} dy - Y offset.
|
|
72
|
+
* @returns {this}
|
|
73
|
+
*/
|
|
45
74
|
add(dx, dy) {
|
|
46
|
-
return this.set(x + dx, y + dy);
|
|
75
|
+
return this.set(this.position.x + dx, this.position.y + dy);
|
|
47
76
|
}
|
|
77
|
+
/** Adds to the X coordinate.
|
|
78
|
+
* @param {number} dx - X offset.
|
|
79
|
+
* @returns {this}
|
|
80
|
+
*/
|
|
48
81
|
addX(dx) {
|
|
49
|
-
return this.setX(x + dx);
|
|
82
|
+
return this.setX(this.position.x + dx);
|
|
50
83
|
}
|
|
84
|
+
/** Adds to the Y coordinate.
|
|
85
|
+
* @param {number} dy - Y offset.
|
|
86
|
+
* @returns {this}
|
|
87
|
+
*/
|
|
51
88
|
addY(dy) {
|
|
52
|
-
return this.setY(y + dy);
|
|
89
|
+
return this.setY(this.position.y + dy);
|
|
53
90
|
}
|
|
54
91
|
|
|
92
|
+
/** Sets the line theme/pen width.
|
|
93
|
+
* @param {number} theme
|
|
94
|
+
* @returns {this}
|
|
95
|
+
*/
|
|
55
96
|
setTheme(theme) {
|
|
56
97
|
this.theme = theme;
|
|
57
98
|
return this;
|
|
58
99
|
}
|
|
59
100
|
|
|
101
|
+
/** Sets the turtle direction (0=right, 1=down, 2=left, 3=up).
|
|
102
|
+
* @param {number} direction
|
|
103
|
+
* @returns {this}
|
|
104
|
+
*/
|
|
60
105
|
setDirection(direction) {
|
|
61
106
|
this.direction = direction % 4;
|
|
62
107
|
return this;
|
|
63
108
|
}
|
|
109
|
+
/** Sets direction to up.
|
|
110
|
+
* @returns {this}
|
|
111
|
+
*/
|
|
64
112
|
setUp() {
|
|
65
113
|
return this.setDirection(Turtle.UP);
|
|
66
114
|
}
|
|
115
|
+
/** Sets direction to down.
|
|
116
|
+
* @returns {this}
|
|
117
|
+
*/
|
|
67
118
|
setDown() {
|
|
68
119
|
return this.setDirection(Turtle.DOWN);
|
|
69
120
|
}
|
|
121
|
+
/** Sets direction to left.
|
|
122
|
+
* @returns {this}
|
|
123
|
+
*/
|
|
70
124
|
setLeft() {
|
|
71
125
|
return this.setDirection(Turtle.LEFT);
|
|
72
126
|
}
|
|
127
|
+
/** Sets direction to right.
|
|
128
|
+
* @returns {this}
|
|
129
|
+
*/
|
|
73
130
|
setRight() {
|
|
74
131
|
return this.setDirection(Turtle.RIGHT);
|
|
75
132
|
}
|
|
133
|
+
/** Turns the turtle 90° to the left.
|
|
134
|
+
* @returns {this}
|
|
135
|
+
*/
|
|
76
136
|
left() {
|
|
77
137
|
return this.setDirection((this.direction + 3) % 4);
|
|
78
138
|
}
|
|
139
|
+
/** Turns the turtle 90° to the right.
|
|
140
|
+
* @returns {this}
|
|
141
|
+
*/
|
|
79
142
|
right() {
|
|
80
143
|
return this.setDirection((this.direction + 1) % 4);
|
|
81
144
|
}
|
|
82
145
|
|
|
146
|
+
/** Saves the current position, direction, and theme onto the stack.
|
|
147
|
+
* @returns {this}
|
|
148
|
+
*/
|
|
83
149
|
save() {
|
|
84
150
|
this.stack.push([this.position.x, this.position.y, this.direction, this.theme]);
|
|
85
151
|
return this;
|
|
86
152
|
}
|
|
153
|
+
/** Restores the position, direction, and theme from the stack.
|
|
154
|
+
* @returns {this}
|
|
155
|
+
*/
|
|
87
156
|
restore() {
|
|
88
|
-
if (!this.stack) throw new ReferenceError('Unmatched restore');
|
|
157
|
+
if (!this.stack.length) throw new ReferenceError('Unmatched restore');
|
|
89
158
|
[this.position.x, this.position.y, this.direction, this.theme] = this.stack.pop();
|
|
90
159
|
return this;
|
|
91
160
|
}
|
|
92
161
|
|
|
162
|
+
/** Moves the turtle up, drawing a line.
|
|
163
|
+
* @param {number} distance - Number of cells to move.
|
|
164
|
+
* @returns {this}
|
|
165
|
+
*/
|
|
93
166
|
moveUp(distance) {
|
|
94
167
|
if (!distance) return this;
|
|
95
168
|
if (distance < 0) return this.moveDown(-distance);
|
|
@@ -116,6 +189,10 @@ export class Turtle {
|
|
|
116
189
|
this.position.y = last;
|
|
117
190
|
return this;
|
|
118
191
|
}
|
|
192
|
+
/** Moves the turtle down, drawing a line.
|
|
193
|
+
* @param {number} distance - Number of cells to move.
|
|
194
|
+
* @returns {this}
|
|
195
|
+
*/
|
|
119
196
|
moveDown(distance) {
|
|
120
197
|
if (!distance) return this;
|
|
121
198
|
if (distance < 0) return this.moveUp(-distance);
|
|
@@ -142,6 +219,10 @@ export class Turtle {
|
|
|
142
219
|
this.position.y = last;
|
|
143
220
|
return this;
|
|
144
221
|
}
|
|
222
|
+
/** Moves the turtle left, drawing a line.
|
|
223
|
+
* @param {number} distance - Number of cells to move.
|
|
224
|
+
* @returns {this}
|
|
225
|
+
*/
|
|
145
226
|
moveLeft(distance) {
|
|
146
227
|
if (!distance) return this;
|
|
147
228
|
if (distance < 0) return this.moveRight(-distance);
|
|
@@ -168,6 +249,10 @@ export class Turtle {
|
|
|
168
249
|
this.position.x = last;
|
|
169
250
|
return this;
|
|
170
251
|
}
|
|
252
|
+
/** Moves the turtle right, drawing a line.
|
|
253
|
+
* @param {number} distance - Number of cells to move.
|
|
254
|
+
* @returns {this}
|
|
255
|
+
*/
|
|
171
256
|
moveRight(distance) {
|
|
172
257
|
if (!distance) return this;
|
|
173
258
|
if (distance < 0) return this.moveLeft(-distance);
|
|
@@ -194,6 +279,11 @@ export class Turtle {
|
|
|
194
279
|
this.position.x = last;
|
|
195
280
|
return this;
|
|
196
281
|
}
|
|
282
|
+
/** Moves the turtle in the given direction, drawing a line.
|
|
283
|
+
* @param {number} direction - Direction (0=right, 1=down, 2=left, 3=up).
|
|
284
|
+
* @param {number} distance - Number of cells to move.
|
|
285
|
+
* @returns {this}
|
|
286
|
+
*/
|
|
197
287
|
move(direction, distance) {
|
|
198
288
|
switch (direction) {
|
|
199
289
|
case Turtle.UP:
|
|
@@ -207,13 +297,25 @@ export class Turtle {
|
|
|
207
297
|
}
|
|
208
298
|
return this;
|
|
209
299
|
}
|
|
300
|
+
/** Moves the turtle forward in its current direction.
|
|
301
|
+
* @param {number} distance - Number of cells to move.
|
|
302
|
+
* @returns {this}
|
|
303
|
+
*/
|
|
210
304
|
forward(distance) {
|
|
211
305
|
return this.move(this.direction, distance);
|
|
212
306
|
}
|
|
307
|
+
/** Moves the turtle backward (opposite of current direction).
|
|
308
|
+
* @param {number} distance - Number of cells to move.
|
|
309
|
+
* @returns {this}
|
|
310
|
+
*/
|
|
213
311
|
backward(distance) {
|
|
214
312
|
return this.move((this.direction + 2) % 4, distance);
|
|
215
313
|
}
|
|
216
314
|
|
|
315
|
+
/** Marks a half-line from the center of the current cell in the given direction.
|
|
316
|
+
* @param {number} direction - Direction (0=right, 1=down, 2=left, 3=up).
|
|
317
|
+
* @returns {this}
|
|
318
|
+
*/
|
|
217
319
|
markHalf(direction) {
|
|
218
320
|
let cell = this.cells[this.position.y][this.position.x];
|
|
219
321
|
if (!cell) cell = this.cells[this.position.y][this.position.x] = {};
|
|
@@ -233,21 +335,39 @@ export class Turtle {
|
|
|
233
335
|
}
|
|
234
336
|
return this;
|
|
235
337
|
}
|
|
338
|
+
/** Marks a half-step line forward.
|
|
339
|
+
* @returns {this}
|
|
340
|
+
*/
|
|
236
341
|
markHalfForward() {
|
|
237
342
|
return this.markHalf(this.direction);
|
|
238
343
|
}
|
|
344
|
+
/** Marks a half-step line backward.
|
|
345
|
+
* @returns {this}
|
|
346
|
+
*/
|
|
239
347
|
markHalfBackward() {
|
|
240
348
|
return this.markHalf((this.direction + 2) % 4);
|
|
241
349
|
}
|
|
350
|
+
/** Marks a half-step line up.
|
|
351
|
+
* @returns {this}
|
|
352
|
+
*/
|
|
242
353
|
markHalfUp() {
|
|
243
354
|
return this.markHalf(Turtle.UP);
|
|
244
355
|
}
|
|
356
|
+
/** Marks a half-step line down.
|
|
357
|
+
* @returns {this}
|
|
358
|
+
*/
|
|
245
359
|
markHalfDown() {
|
|
246
360
|
return this.markHalf(Turtle.DOWN);
|
|
247
361
|
}
|
|
362
|
+
/** Marks a half-step line left.
|
|
363
|
+
* @returns {this}
|
|
364
|
+
*/
|
|
248
365
|
markHalfLeft() {
|
|
249
366
|
return this.markHalf(Turtle.LEFT);
|
|
250
367
|
}
|
|
368
|
+
/** Marks a half-step line right.
|
|
369
|
+
* @returns {this}
|
|
370
|
+
*/
|
|
251
371
|
markHalfRight() {
|
|
252
372
|
return this.markHalf(Turtle.RIGHT);
|
|
253
373
|
}
|