@k03mad/dice 3.0.0 → 3.2.1
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/app/run.js +9 -19
- package/app/utils/config.js +20 -12
- package/package.json +1 -1
package/app/run.js
CHANGED
|
@@ -7,38 +7,28 @@ import {emitKeypressEvents} from 'node:readline';
|
|
|
7
7
|
import {log} from '@k03mad/simple-log';
|
|
8
8
|
import image from 'terminal-image';
|
|
9
9
|
|
|
10
|
-
import
|
|
11
|
-
DICE_DEFAULT_COUNT,
|
|
12
|
-
DICE_HEIGHT,
|
|
13
|
-
DICE_PICTURES_FOLDER,
|
|
14
|
-
EXIT_CTRL_MODIFIER,
|
|
15
|
-
EXIT_EXTRA_KEY,
|
|
16
|
-
texts,
|
|
17
|
-
} from './utils/config.js';
|
|
10
|
+
import config from './utils/config.js';
|
|
18
11
|
import {getRandomArrElem} from './utils/helpers.js';
|
|
19
12
|
|
|
20
|
-
const imgFolderAbs = path.join(import.meta.dirname,
|
|
13
|
+
const imgFolderAbs = path.join(import.meta.dirname, config.dice.picturesFolder);
|
|
21
14
|
|
|
22
15
|
const imgFiles = await fs.readdir(imgFolderAbs);
|
|
23
16
|
const imgFilesAbs = imgFiles.map(img => path.join(imgFolderAbs, img));
|
|
24
17
|
|
|
25
18
|
emitKeypressEvents(process.stdin);
|
|
19
|
+
process.stdin.setRawMode(true);
|
|
26
20
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let diceCurrentCount = DICE_DEFAULT_COUNT;
|
|
32
|
-
log(texts.message);
|
|
21
|
+
let diceCurrentCount = config.dice.defaultCount;
|
|
22
|
+
log(config.messages.welcome);
|
|
33
23
|
|
|
34
24
|
process.stdin.on('keypress', async (char, key) => {
|
|
35
25
|
if (
|
|
36
|
-
(key.ctrl === true && key.name ===
|
|
37
|
-
|| char ===
|
|
26
|
+
(key.ctrl === true && key.name === config.exit.ctrlKeyModifier)
|
|
27
|
+
|| char === config.exit.extraKey
|
|
38
28
|
) {
|
|
39
29
|
process.exit();
|
|
40
30
|
} else {
|
|
41
|
-
log(
|
|
31
|
+
log(config.dice.separator);
|
|
42
32
|
}
|
|
43
33
|
|
|
44
34
|
diceCurrentCount = Number(char) || diceCurrentCount;
|
|
@@ -46,7 +36,7 @@ process.stdin.on('keypress', async (char, key) => {
|
|
|
46
36
|
const output = await Promise.all(
|
|
47
37
|
Array.from({length: diceCurrentCount}, () => {
|
|
48
38
|
const diceImg = getRandomArrElem(imgFilesAbs);
|
|
49
|
-
return image.file(diceImg, {height:
|
|
39
|
+
return image.file(diceImg, {height: config.dice.height});
|
|
50
40
|
}),
|
|
51
41
|
);
|
|
52
42
|
|
package/app/utils/config.js
CHANGED
|
@@ -2,18 +2,26 @@ import chalk from 'chalk';
|
|
|
2
2
|
|
|
3
3
|
const {green, dim, bold} = chalk;
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
export const DICE_PICTURES_FOLDER = 'png';
|
|
7
|
-
export const DICE_HEIGHT = '30%';
|
|
5
|
+
const EXIT_EXTRA_KEY = 'q';
|
|
8
6
|
|
|
9
|
-
export
|
|
10
|
-
|
|
7
|
+
export default {
|
|
8
|
+
dice: {
|
|
9
|
+
defaultCount: 1,
|
|
10
|
+
picturesFolder: 'png',
|
|
11
|
+
height: '30%',
|
|
12
|
+
separator: dim('>\n'),
|
|
13
|
+
},
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
exit: {
|
|
16
|
+
ctrlKeyModifier: 'c',
|
|
17
|
+
extraKey: EXIT_EXTRA_KEY,
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
messages: {
|
|
21
|
+
welcome: green([
|
|
22
|
+
'— type any digit to generate that number of dice',
|
|
23
|
+
'— press any key to reroll dice',
|
|
24
|
+
`— press ${bold('CTRL+C')} or type '${bold(EXIT_EXTRA_KEY)}' to exit`,
|
|
25
|
+
].join('\n')),
|
|
26
|
+
},
|
|
19
27
|
};
|