@slugbugblue/trax-tty 1.0.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.
- package/CHANGELOG.md +6 -0
- package/CONTRIBUTING.md +102 -0
- package/LICENSE +201 -0
- package/README.md +17 -0
- package/package.json +46 -0
- package/tty.js +317 -0
package/CHANGELOG.md
ADDED
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Contributing to slugbugblue/trax-tty
|
|
2
|
+
|
|
3
|
+
Hi, thanks for considering contributing to this project.
|
|
4
|
+
|
|
5
|
+
If this is your first time working on an open source project, it is recommended
|
|
6
|
+
that you start by reading [How to Contribute to Open Source][howto].
|
|
7
|
+
|
|
8
|
+
## Ways to contribute
|
|
9
|
+
|
|
10
|
+
### Improve documentation
|
|
11
|
+
|
|
12
|
+
One of the simplest ways to get started contributing is to find areas of the
|
|
13
|
+
documentation that are unclear, incorrect, or that have typos or other errors,
|
|
14
|
+
and providing feedback and corrections. You don't have to know how to code or
|
|
15
|
+
even how to use git.
|
|
16
|
+
|
|
17
|
+
### Identify bugs
|
|
18
|
+
|
|
19
|
+
If you are aware of something not working as you think it should, feel free to
|
|
20
|
+
open a new issue to let us know. Browsing through the existing open issues and
|
|
21
|
+
adding comments or making clarifications can also be a great way to get started.
|
|
22
|
+
|
|
23
|
+
### Fixing bugs
|
|
24
|
+
|
|
25
|
+
If you feel you have a solution to an existing bug, you are welcome to code up
|
|
26
|
+
your solution and submit a merge request. It may take some time to get fully up
|
|
27
|
+
to speed with our coding styles and with the structure of the code, so don't
|
|
28
|
+
feel bad if your merge request receives comments rather than being immediately
|
|
29
|
+
accepted. Simply make any necessary changes, and update the merge request, or
|
|
30
|
+
let us know in a subsequent comment if you think we are misunderstanding the
|
|
31
|
+
merge request.
|
|
32
|
+
|
|
33
|
+
## Submitting an issue
|
|
34
|
+
|
|
35
|
+
If you choose to submit an issue, please take the time to ensure that you
|
|
36
|
+
provide as much detail as possible about the issue. A poorly worded or poorly
|
|
37
|
+
researched issue report will unnecessarily delay the issue resolution.
|
|
38
|
+
|
|
39
|
+
Even if you don't have the time or expertise to fully diagnose the problem, a
|
|
40
|
+
good issue report can be very helpful to us as we seek to improve the code.
|
|
41
|
+
|
|
42
|
+
## Submitting a merge request
|
|
43
|
+
|
|
44
|
+
Good merge requests are a fantastic way to contribute to this project. Your
|
|
45
|
+
merge request is going to be accepted more quickly if it is easy to understand.
|
|
46
|
+
Ideally it should be focused on a single item and avoid unrelated commits.
|
|
47
|
+
|
|
48
|
+
Please ask first before embarking on a major change, such as adding a new
|
|
49
|
+
feature or refactoring the code, otherwise you risk spending a significant
|
|
50
|
+
amount of time working on something that will not be merged into the project.
|
|
51
|
+
|
|
52
|
+
If you have never submitted a merge request before, no fear. Here is a [quick
|
|
53
|
+
tutorial][howto-mr] on how to create one.
|
|
54
|
+
|
|
55
|
+
### Merge request steps
|
|
56
|
+
|
|
57
|
+
- Fork the project and clone it to your own workspace.
|
|
58
|
+
- Get the latest changes and update the project dependencies:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
$ git checkout develop
|
|
62
|
+
$ git pull upstream develop
|
|
63
|
+
$ rm -rf node_modules
|
|
64
|
+
$ npm install
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
- Create a new branch to contain your changes:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
$ git checkout -b <new-branch-name>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- Make your changes.
|
|
74
|
+
- Test your changes.
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
$ npm test
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- Push your changes back to your fork.
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
$ git push -u origin <new-branch-name>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
1. Open a merge request in the [slugbugblue/trax-tty][trax-tty-repo] repo.
|
|
87
|
+
|
|
88
|
+
## Working with the code
|
|
89
|
+
|
|
90
|
+
This repository uses [XO][xo] for linting and [Prettier][prettier] for
|
|
91
|
+
formatting. Running `npm test` will produce an error if your changes do not
|
|
92
|
+
conform. You should consider installing the [XO plugin][xo-plugin] for your
|
|
93
|
+
editor to have the linting errors highlighted for you and automatically fixed on
|
|
94
|
+
save. You can also run `npx xo --fix` from the command line to fix most linting
|
|
95
|
+
errors.
|
|
96
|
+
|
|
97
|
+
[howto]: https://opensource.guide/how-to-contribute/
|
|
98
|
+
[howto-mr]: https://opensource.guide/how-to-contribute/#opening-a-pull-request
|
|
99
|
+
[prettier]: https://prettier.io
|
|
100
|
+
[trax-tty-repo]: https://gitlab.com/slugbugblue/trax-tty
|
|
101
|
+
[xo]: https://github.com/sidresorhus/xo
|
|
102
|
+
[xo-plugin]: https://github.com/sidresorhus/xo#editor-plugins
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright © 2021-2023 Chad Transtrum
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# trax-tty
|
|
2
|
+
|
|
3
|
+
This javascript module provides a simple to use interface for displaying a Trax
|
|
4
|
+
game on a tty console (ie, from nodejs).
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
|
|
8
|
+
```javascript
|
|
9
|
+
import { Trax } from '@slugbugblue/trax'
|
|
10
|
+
import * as tty from '@slugbugblue/trax-tty'
|
|
11
|
+
|
|
12
|
+
const game = new Trax('trax', '@0/ A0+')
|
|
13
|
+
const players = ['first player', 'player 2']
|
|
14
|
+
tty.display(game, players)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
See the [source](tty.js) for more details.
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slugbugblue/trax-tty",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple tty graphics for displaying trax games",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"trax",
|
|
7
|
+
"tty"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://gitlab.com/slugbugblue/trax-tty",
|
|
10
|
+
"bugs": "https://gitlab.com/slugbugblue/trax-tty/issues",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"author": "Chad Transtrum <chad@transtrum.net>",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./tty.js"
|
|
15
|
+
},
|
|
16
|
+
"repository": "gitlab:slugbugblue/trax-tty",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"postversion": "git push && git push --tags && npm publish"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@slugbugblue/trax": ">=0.21.0",
|
|
22
|
+
"prettier": "^2.6.1",
|
|
23
|
+
"xo": "0.*"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=16.15.0"
|
|
28
|
+
},
|
|
29
|
+
"prettier": {
|
|
30
|
+
"bracketSpacing": true,
|
|
31
|
+
"proseWrap": "always",
|
|
32
|
+
"semi": false,
|
|
33
|
+
"singleQuote": true,
|
|
34
|
+
"trailingComma": "all",
|
|
35
|
+
"useTabs": false
|
|
36
|
+
},
|
|
37
|
+
"xo": {
|
|
38
|
+
"prettier": true,
|
|
39
|
+
"rules": {
|
|
40
|
+
"curly": [
|
|
41
|
+
"error",
|
|
42
|
+
"multi-line"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
package/tty.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/* Copyright 2021-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.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// Ever wanted to draw Trax boards on the console? I have.
|
|
17
|
+
|
|
18
|
+
import process from 'node:process'
|
|
19
|
+
|
|
20
|
+
import { Trax } from '@slugbugblue/trax'
|
|
21
|
+
|
|
22
|
+
const esc = '\u001B[' // Start of console color escape code
|
|
23
|
+
const off = esc + 'm' // Color reset code
|
|
24
|
+
const ENV = process.env
|
|
25
|
+
const NERD = ENV.NERDFONT || ENV.NERDFONTS
|
|
26
|
+
const POWER = ENV.POWERLINE || ENV.P9K_TTY || ENV.P9K_SSH
|
|
27
|
+
const leftBubble = NERD ? '' : POWER ? '' : '▐'
|
|
28
|
+
const rightBubble = NERD ? '' : POWER ? '' : '▌'
|
|
29
|
+
|
|
30
|
+
/** Colorize a string.
|
|
31
|
+
* @arg {string} contents
|
|
32
|
+
* @arg {string|number} [fgcolor]
|
|
33
|
+
* @arg {string|number} [bgcolor]
|
|
34
|
+
*/
|
|
35
|
+
export const color = (contents = '', fgcolor = '', bgcolor = '') => {
|
|
36
|
+
const c = []
|
|
37
|
+
if (fgcolor) c.push('38;5;' + fgcolor)
|
|
38
|
+
if (bgcolor) c.push('48;5;' + bgcolor)
|
|
39
|
+
return esc + c.join(';') + 'm' + contents
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Display a message with optional color
|
|
43
|
+
export const outty = (message, fgcolor, bgcolor) => {
|
|
44
|
+
console.log(color(message, fgcolor, bgcolor) + off)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// A little magic to display the game in progress
|
|
48
|
+
const colorSets = {
|
|
49
|
+
// Colors for terminal sequence: <Esc>[38;5;<color_num>m
|
|
50
|
+
// b, B, w, W: color of line for each player, capital for a winning line
|
|
51
|
+
// e: empty (ie, board) color, t: tile color
|
|
52
|
+
trax: { b: 238, B: 57, e: 19, t: 74, w: 255, W: 184 },
|
|
53
|
+
trax8: { b: 236, B: 21, e: 58, t: 137, w: 254, W: 117 },
|
|
54
|
+
traxloop: { b: 235, B: 20, e: 17, t: 61, w: 253, W: 222 },
|
|
55
|
+
tentative: { b: 235, B: 18, e: 19, t: 244, w: 252, W: 217 },
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let set = colorSets.trax // Which color set should we draw with?
|
|
59
|
+
|
|
60
|
+
export const name = (rules) => {
|
|
61
|
+
if (Trax.variants.has(rules)) {
|
|
62
|
+
return color(Trax.names[rules], colorSets[rules].t)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return rules
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const bubble = (clr, text) => {
|
|
69
|
+
const colors = {
|
|
70
|
+
w: { fg: 236, bg: 153 },
|
|
71
|
+
b: { fg: 250, bg: 18 },
|
|
72
|
+
wd: { fg: 236, bg: 248 },
|
|
73
|
+
bd: { fg: 248, bg: 236 },
|
|
74
|
+
wh: { fg: 63, bg: 123 },
|
|
75
|
+
bh: { fg: 220, bg: 20 },
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const c = colors[clr]
|
|
79
|
+
return (
|
|
80
|
+
color(leftBubble, c.bg) +
|
|
81
|
+
color(text, c.fg, c.bg) +
|
|
82
|
+
off +
|
|
83
|
+
color(rightBubble, c.bg)
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const ln = 'm\u2584' // Lower half block -- with the leading m to terminate ansi color escape
|
|
88
|
+
const up = 'm\u2588' // Full block
|
|
89
|
+
const lt = 'm\u2599' // Lower half and left half block
|
|
90
|
+
const rt = 'm\u259F' // Lower half and right half block
|
|
91
|
+
const fg = (n) => '38;5;' + String(set[n]) // Foreground color_num
|
|
92
|
+
const bg = (n) => '48;5;' + String(set[n]) // Background color_num
|
|
93
|
+
const sp = () => esc + bg('e') + 'm ' // Lower space
|
|
94
|
+
// tiles are displayed on two lines; the top half of the first line is empty
|
|
95
|
+
// and the second half is composed of three characters, up color surrounded by tile color
|
|
96
|
+
const uptty = (clr) => {
|
|
97
|
+
const left = esc + fg('t') + ';' + bg('e') + ln
|
|
98
|
+
const center = esc + fg(clr) + ln
|
|
99
|
+
const right = esc + fg('t') + ln + ' '
|
|
100
|
+
return left + center + right
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The top half of the second line has both horizontal lines with a center transition
|
|
104
|
+
// while the bottom half is the tile background color surrounding the down color
|
|
105
|
+
const horiztty = (clr) => esc + fg('t') + ';' + bg(clr) + ln
|
|
106
|
+
const dntty = (clr, oppclr, char) =>
|
|
107
|
+
esc + fg(clr) + (char === up ? '' : ';' + bg(oppclr)) + char
|
|
108
|
+
// List of icon_char => tile line colors for easy reference in order up, right, down, left
|
|
109
|
+
const tiles = {
|
|
110
|
+
a: 'bbww',
|
|
111
|
+
b: 'bwbw',
|
|
112
|
+
c: 'bwwb',
|
|
113
|
+
d: 'wbbw',
|
|
114
|
+
e: 'wbwb',
|
|
115
|
+
f: 'wwbb',
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Return the ANSI codes to display a 3x1.5 block tile, one line at a time
|
|
119
|
+
const tileTty = (tile, top) => {
|
|
120
|
+
// Top: true for first line, false for second
|
|
121
|
+
// quick access to tile color and its opposite in upper case
|
|
122
|
+
const colors = { b: 'b', w: 'w', B: 'w', W: 'b' }
|
|
123
|
+
const save = set
|
|
124
|
+
const tentative = tile !== tile.toLowerCase()
|
|
125
|
+
if (tentative) set = colorSets.tentative
|
|
126
|
+
tile = tile.toLowerCase()
|
|
127
|
+
if (!(tile in tiles)) {
|
|
128
|
+
// Winning paths are represented by adding 6 to the tile char code
|
|
129
|
+
tile = String.fromCodePoint(tile.codePointAt(0) - 6)
|
|
130
|
+
if (tile in tiles) {
|
|
131
|
+
colors.w = 'W'
|
|
132
|
+
colors.B = 'W'
|
|
133
|
+
} else {
|
|
134
|
+
// And another six for a black win
|
|
135
|
+
tile = String.fromCodePoint(tile.codePointAt(0) - 6)
|
|
136
|
+
colors.b = 'B'
|
|
137
|
+
colors.W = 'B'
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let value = ''
|
|
142
|
+
if (tile in tiles) {
|
|
143
|
+
tile = tiles[tile]
|
|
144
|
+
if (top) {
|
|
145
|
+
value = uptty(colors[tile[0]])
|
|
146
|
+
} else {
|
|
147
|
+
const centerChar =
|
|
148
|
+
tile[1] === tile[2] ? rt : tile[2] === tile[3] ? lt : up
|
|
149
|
+
const centertty = dntty(
|
|
150
|
+
colors[tile[2]],
|
|
151
|
+
colors[tile[2].toUpperCase()],
|
|
152
|
+
centerChar,
|
|
153
|
+
)
|
|
154
|
+
value =
|
|
155
|
+
horiztty(colors[tile[3]]) + centertty + horiztty(colors[tile[1]]) + sp()
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
set = save
|
|
160
|
+
return value || (top ? '??? ' : "''' ")
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const mergeTentative = () => {
|
|
164
|
+
colorSets.tentative.e = set.e // Copy empty color to tentative set
|
|
165
|
+
colorSets.tentative.W = set.W
|
|
166
|
+
colorSets.tentative.B = set.B
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const printIcon = (icon, setName) => {
|
|
170
|
+
set = colorSets[setName] || colorSets.trax
|
|
171
|
+
mergeTentative()
|
|
172
|
+
const on = esc + '38;5;' + String(set.t) + ';48;5;' + String(set.e) + 'm'
|
|
173
|
+
let o1 = ' '
|
|
174
|
+
let o2 = '1 '
|
|
175
|
+
let row = 1
|
|
176
|
+
let width = 1
|
|
177
|
+
const info = []
|
|
178
|
+
let lineWidth = 0
|
|
179
|
+
while (icon.length > 0) {
|
|
180
|
+
const c = icon[0]
|
|
181
|
+
icon = icon.slice(1)
|
|
182
|
+
if (c === ':') {
|
|
183
|
+
info.push({ o: o1, w: lineWidth }, { o: o2, w: lineWidth })
|
|
184
|
+
row++
|
|
185
|
+
width = Math.max(width, lineWidth)
|
|
186
|
+
lineWidth = 0
|
|
187
|
+
o1 = ' '
|
|
188
|
+
o2 = String(row).padEnd(3)
|
|
189
|
+
} else if (c >= '0' && c <= '9') {
|
|
190
|
+
const empty = Number(c) + 1
|
|
191
|
+
o1 += ' '.repeat(empty * 4)
|
|
192
|
+
o2 += ' '.repeat(empty * 4)
|
|
193
|
+
lineWidth += empty
|
|
194
|
+
} else {
|
|
195
|
+
o1 += tileTty(c, 'top')
|
|
196
|
+
o2 += tileTty(c)
|
|
197
|
+
lineWidth++
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (o1.length > 0) {
|
|
202
|
+
info.push({ o: o1, w: lineWidth }, { o: o2, w: lineWidth })
|
|
203
|
+
width = Math.max(width, lineWidth)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const columns = process.stdout.columns || 0
|
|
207
|
+
const rows = process.stdout.rows || 0
|
|
208
|
+
if (width * 4 < columns - 4 && info.length < rows - 2) {
|
|
209
|
+
let colOut = ' '
|
|
210
|
+
for (let col = 1; col <= width; col++) {
|
|
211
|
+
colOut += ' ' + Trax.encodeCol(col).padEnd(3)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
console.info(on + colOut.padEnd((width + 1) * 4) + off)
|
|
215
|
+
for (const line of info) {
|
|
216
|
+
console.info(on + line.o + ''.padEnd((width - line.w) * 4) + ' ' + off)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
console.info(on + ''.padEnd((width + 1) * 4) + off)
|
|
220
|
+
} else {
|
|
221
|
+
console.warn('Console is too small to display this game')
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Flexible way to get a Trax game object.
|
|
226
|
+
* @arg {Trax|TraxVariant} [game] - existing Trax instance or a string indicating the variant name
|
|
227
|
+
* @arg {string|string[]} [moves] - optional list or string of moves to be made on the game
|
|
228
|
+
* @returns {Trax} a Trax instance
|
|
229
|
+
*/
|
|
230
|
+
export const traxGame = (game = 'trax', moves = '') => {
|
|
231
|
+
let trax
|
|
232
|
+
if (game instanceof Trax) {
|
|
233
|
+
trax = game
|
|
234
|
+
if (moves) {
|
|
235
|
+
if (typeof moves === 'string') moves = moves.split(' ')
|
|
236
|
+
for (const move of moves) trax.dropTile(move)
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
trax = new Trax(game, moves, 'cli')
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return trax
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const printNotation = (trax, tentative, show = Number.POSITIVE_INFINITY) => {
|
|
246
|
+
let b = false
|
|
247
|
+
let n = 1
|
|
248
|
+
let out = ''
|
|
249
|
+
let length = 0
|
|
250
|
+
const t = tentative ? trax.moves.length - 1 : show
|
|
251
|
+
for (const move of trax.moves) {
|
|
252
|
+
const c = (b ? 'b' : 'w') + (t < n ? 'd' : show === n ? 'h' : '')
|
|
253
|
+
const notation = String(n) + ' ' + move
|
|
254
|
+
if (length + notation.length + 3 > process.stdout.columns) {
|
|
255
|
+
out += '\n'
|
|
256
|
+
length = 0
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
out += bubble(c, notation) + ' '
|
|
260
|
+
length += notation.length + 3
|
|
261
|
+
b = !b
|
|
262
|
+
n++
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
outty(out)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export const display = (
|
|
269
|
+
game,
|
|
270
|
+
players,
|
|
271
|
+
tentative = '',
|
|
272
|
+
showMove = Number.POSITIVE_INFINITY,
|
|
273
|
+
) => {
|
|
274
|
+
const trax = traxGame(game)
|
|
275
|
+
const saved = trax.save()
|
|
276
|
+
const colors = ['white', 'black']
|
|
277
|
+
if (!Array.isArray(players)) players = colors
|
|
278
|
+
let { icon } = trax
|
|
279
|
+
if (tentative) {
|
|
280
|
+
const drops = trax.dropTile(tentative)
|
|
281
|
+
if (drops.valid || drops.dropped.length > 0) {
|
|
282
|
+
icon = trax.dropsIcon(drops)
|
|
283
|
+
} else {
|
|
284
|
+
tentative = ''
|
|
285
|
+
}
|
|
286
|
+
} else if (showMove <= trax.moves.length) {
|
|
287
|
+
icon = trax.dropsIcon({
|
|
288
|
+
dropped: Object.values(trax.tiles).filter((t) => t.move > showMove),
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
printNotation(trax, tentative, showMove)
|
|
293
|
+
printIcon(icon, trax.rules)
|
|
294
|
+
const nameColor = { 1: 'w', 2: 'b' }[trax.turn]
|
|
295
|
+
if (tentative) {
|
|
296
|
+
const otherColor = { w: 'bd', b: 'wd' }[nameColor] || 'wd'
|
|
297
|
+
outty('---' + bubble(otherColor, 'tentative position') + off + '---')
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const name = players[trax.turn - 1] || colors[trax.turn - 1]
|
|
301
|
+
if (trax.over) {
|
|
302
|
+
if (trax.turn) {
|
|
303
|
+
outty(bubble(nameColor + 'h', name) + off + ' wins')
|
|
304
|
+
} else {
|
|
305
|
+
outty('game end' + (tentative ? 's' : 'ed') + ' in a draw')
|
|
306
|
+
}
|
|
307
|
+
} else if (trax.turn) {
|
|
308
|
+
outty(
|
|
309
|
+
(tentative ? 'leaving ' : '') +
|
|
310
|
+
bubble(nameColor, name) +
|
|
311
|
+
off +
|
|
312
|
+
' to play',
|
|
313
|
+
)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (tentative) trax.restore(saved)
|
|
317
|
+
}
|