@slugbugblue/trax-cli 0.12.0 → 0.13.0

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.
@@ -1,19 +1,10 @@
1
- /* Copyright 2022-2023 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
1
+ /** CLI suggest command
2
+ * @copyright 2022-2026
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
14
5
  */
15
6
 
16
- // CLI suggest command
7
+ /** @typedef {import('../cli.js').CLIContext} CLIContext */
17
8
 
18
9
  import { suggest } from '@slugbugblue/trax-analyst'
19
10
  import { timeString } from '@slugbugblue/trax-cli/utils'
@@ -23,33 +14,33 @@ export const suggestCmd = {
23
14
  alt: ['bot'],
24
15
  desc: 'suggest a move',
25
16
  help: ['Suggest a move in the current game.'],
26
- }
17
+ /** @param {CLIContext} CLI @param {string} [debug] */
18
+ fn(CLI, debug) {
19
+ if (!String(CLI.GAME?.id)) {
20
+ return CLI.error('No active game. Type "new" to start a game.')
21
+ }
27
22
 
28
- suggestCmd.fn = (CLI, debug) => {
29
- if (!String(CLI.GAME?.id)) {
30
- return CLI.error('No active game. Type "new" to start a game.')
31
- }
23
+ const game = CLI.TRAX
24
+ const suggestion = suggest(game)
32
25
 
33
- const game = CLI.TRAX
34
- const suggestion = suggest(game)
26
+ if (!suggestion.best) return
35
27
 
36
- if (!suggestion.best) return
28
+ CLI.out(
29
+ CLI.bubble(
30
+ game.turn === 1 ? 'w' : 'b',
31
+ String(game.move + 1) + '. ' + suggestion.pick.move,
32
+ ),
33
+ )
37
34
 
38
- CLI.out(
39
- CLI.bubble(
40
- game.turn === 1 ? 'w' : 'b',
41
- String(game.move + 1) + '. ' + suggestion.pick.move,
42
- ),
43
- )
35
+ if (debug && 'debug'.startsWith(debug)) {
36
+ const m = []
37
+ for (const move of suggestion.options) {
38
+ m.push(move.move + ' ' + move.score)
39
+ }
44
40
 
45
- if ('debug'.startsWith(debug)) {
46
- const m = []
47
- for (const move of suggestion.options) {
48
- m.push(move.move + ' ' + move.score)
41
+ CLI.out(
42
+ '[' + m.join(', ') + '] ' + CLI.color.black(timeString(suggestion.ms)),
43
+ )
49
44
  }
50
-
51
- CLI.out(
52
- '[' + m.join(', ') + '] ' + CLI.color.black(timeString(suggestion.ms)),
53
- )
54
- }
45
+ },
55
46
  }
package/src/cmds/undo.js CHANGED
@@ -1,19 +1,12 @@
1
- /* Copyright 2022 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
1
+ /** CLI undo command
2
+ * @copyright 2022-2026
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
14
5
  */
15
6
 
16
- // CLI undo command
7
+ /** @typedef {import('../cli.js').CLIContext} CLIContext */
8
+
9
+ import { puzzles } from '@slugbugblue/trax-puzzles'
17
10
 
18
11
  export const undoCmd = {
19
12
  name: 'undo',
@@ -24,17 +17,29 @@ export const undoCmd = {
24
17
  'Backtrack one or more moves in a game. Type "undo" to take back the last',
25
18
  'move, or "undo 2" to undo the last two moves.',
26
19
  ],
27
- }
28
-
29
- undoCmd.fn = (CLI, n) => {
30
- n = Math.max(1, Math.floor(Number(n) || 1)) // Get an integer 1 or higher
20
+ /** @param {CLIContext} CLI @param {string} [n] */
21
+ fn(CLI, n) {
22
+ const count = Math.max(1, Math.floor(Number(n) || 1))
23
+ const { moves } = CLI.TRAX
31
24
 
32
- const { moves } = CLI.TRAX
33
- const undo = moves.slice(0, 0 - n)
34
- if (moves.length === undo.length) {
35
- CLI.error('Unable to undo.')
36
- } else {
37
- CLI.setGame(undo)
38
- CLI.do('view')
39
- }
25
+ if (CLI.GAME?.puzzle) {
26
+ const puzzle = puzzles.find((p) => p.id === CLI.GAME.puzzle)
27
+ const offset = puzzle ? puzzle.notation.split(' ').length : 0
28
+ const undoTo = Math.max(offset, moves.length - count * 2)
29
+ if (undoTo === moves.length) {
30
+ CLI.error('Cannot undo before the start of the puzzle.')
31
+ } else {
32
+ CLI.setGame(moves.slice(0, undoTo))
33
+ CLI.do('view')
34
+ }
35
+ } else {
36
+ const undo = moves.slice(0, 0 - count)
37
+ if (moves.length === undo.length) {
38
+ CLI.error('Unable to undo.')
39
+ } else {
40
+ CLI.setGame(undo)
41
+ CLI.do('view')
42
+ }
43
+ }
44
+ },
40
45
  }
package/src/cmds/view.js CHANGED
@@ -1,19 +1,10 @@
1
- /* Copyright 2022-2023 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
1
+ /** CLI view command
2
+ * @copyright 2022-2026
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
14
5
  */
15
6
 
16
- // CLI view command
7
+ /** @typedef {import('../cli.js').CLIContext} CLIContext */
17
8
 
18
9
  export const viewCmd = {
19
10
  name: 'view',
@@ -25,41 +16,41 @@ export const viewCmd = {
25
16
  'Show the current game. Include a move number to see the board as it was at',
26
17
  'that move.',
27
18
  ],
28
- }
29
-
30
- viewCmd.fn = (CLI, move, ...extra) => {
31
- if (!CLI.GAME?.id) {
32
- return CLI.error('No active game. Type "new" to start a game.')
33
- }
34
-
35
- if (move?.length > 1 && 'puzzles'.startsWith(move)) {
36
- return CLI.do('puzzles', 'list', ...extra)
37
- }
38
-
39
- const trax = CLI.TRAX
40
-
41
- let show = Number.POSITIVE_INFINITY
42
- if (move && Number(move) >= 0 && Number(move) <= trax.moves.length) {
43
- show = Number(move)
44
- }
45
-
46
- const game = CLI.GAME
47
- const players = game.players || ['white', 'black']
48
- const puzzle = game.puzzle || ''
49
- const { note } = [...(game.notes || [{}])].pop()
50
-
51
- CLI.out(
52
- CLI.color('#' + CLI.ID + ' ') +
53
- CLI.name(trax.rules) +
54
- ' ' +
55
- CLI.bubble('w' + (trax.turn === 1 ? 'h' : ''), players[0]) +
56
- CLI.color(' vs ') +
57
- CLI.bubble('b' + (trax.turn === 2 ? 'h' : ''), players[1]),
58
- )
59
- CLI.display(trax, players, undefined, show)
60
- if (puzzle && (!note || !note.includes(puzzle))) {
61
- CLI.out(CLI.color('Puzzle ' + puzzle))
62
- }
63
-
64
- if (note) CLI.out(CLI.color.help(note))
19
+ /** @param {CLIContext} CLI @param {string} [move] @param {...string} extra */
20
+ fn(CLI, move, ...extra) {
21
+ if (!CLI.GAME?.id) {
22
+ return CLI.error('No active game. Type "new" to start a game.')
23
+ }
24
+
25
+ if (move && move.length > 1 && 'puzzles'.startsWith(move)) {
26
+ return CLI.do('puzzles', 'list', ...extra)
27
+ }
28
+
29
+ const trax = CLI.TRAX
30
+
31
+ let show = Number.POSITIVE_INFINITY
32
+ if (move && Number(move) >= 0 && Number(move) <= trax.moves.length) {
33
+ show = Number(move)
34
+ }
35
+
36
+ const game = CLI.GAME
37
+ const players = game.players || ['white', 'black']
38
+ const puzzle = game.puzzle || ''
39
+ const note = game.notes?.at(-1)?.note
40
+
41
+ CLI.out(
42
+ CLI.color('#' + CLI.ID + ' ') +
43
+ CLI.name(trax.rules) +
44
+ ' ' +
45
+ CLI.bubble('w' + (trax.turn === 1 ? 'h' : ''), players[0]) +
46
+ CLI.color(' vs ') +
47
+ CLI.bubble('b' + (trax.turn === 2 ? 'h' : ''), players[1]),
48
+ )
49
+ CLI.display(trax, players, undefined, show)
50
+ if (puzzle && (!note || !note.includes(puzzle))) {
51
+ CLI.out(CLI.color('Puzzle ' + puzzle))
52
+ }
53
+
54
+ if (note) CLI.out(CLI.color.help(note))
55
+ },
65
56
  }
package/src/utils.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /** Shared utilities
2
- * @copyright 2023
2
+ * @copyright 2023-2026
3
3
  * @author Chad Transtrum <chad@transtrum.net>
4
4
  * @license Apache-2.0
5
5
  */
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.12.0'
2
+ export const version = '0.13.0'
package/xo.config.js ADDED
@@ -0,0 +1,10 @@
1
+ /** @type {import('xo').FlatXoConfig} */
2
+ const xoConfig = [
3
+ { ignores: ['exports/**'] },
4
+ {
5
+ prettier: true,
6
+ space: true,
7
+ },
8
+ ]
9
+
10
+ export default xoConfig
package/src/types.d.ts DELETED
@@ -1,33 +0,0 @@
1
- /** Type information.
2
- * @copyright 2022-2023
3
- * @author Chad Transtrum <chad@transtrum.net>
4
- * @license Apache-2.0
5
- */
6
-
7
- /** Colorize is a fancy function for colorizing text. */
8
- type Colorize = (text: string, defaultColor?: string | number) => string
9
-
10
- /** Colorer is a little too fancy. Hence the gnarly typescript. */
11
- type Colorer = {
12
- (text: string, defaultColor?: string | number): string
13
- black: Colorize
14
- command: Colorize
15
- default: Colorize
16
- error: Colorize
17
- fatal: Colorize
18
- help: Colorize
19
- id: Colorize
20
- variable: Colorize
21
- optional: Colorize
22
- short: Colorize
23
- white: Colorize
24
- }
25
-
26
- /** A note with a move number */
27
- type GameNote = {
28
- move: number
29
- note: string
30
- }
31
-
32
- /** Notes saved in the game object in the CLI. */
33
- type GameNotes = GameNote[]