interacter 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/README.md ADDED
@@ -0,0 +1 @@
1
+ # IOS
package/colors.js ADDED
@@ -0,0 +1,25 @@
1
+ export function colors(){
2
+ let color = {
3
+
4
+ RESET:"\x1b[0m",
5
+ BOLD:"\x1b[1m",
6
+ UND:"\x1b[4m",
7
+ black:"\x1b[30m",
8
+ red:"\x1b[31m",
9
+ green:"\x1b[32m",
10
+ yellow:"\x1b[33m",
11
+ blue:"\x1b[34m",
12
+ magenta:"\x1b[35m",
13
+ cyan:"\x1b[36m",
14
+ white:"\x1b[37m",
15
+ bg_black:"\x1b[40m",
16
+ bg_red:"\x1b[41m",
17
+ bg_green:"\x1b[42m",
18
+ bg_yellow:"\x1b[43m",
19
+ bg_blue:"\x1b[44m",
20
+ bg_magenta:"\x1b[45m",
21
+ bg_cyan:"\x1b[46m",
22
+ bg_white:"\x1b[47m"
23
+ }
24
+ return color;
25
+ }
package/exp.js ADDED
@@ -0,0 +1,33 @@
1
+ import {highlight, main, int, Input, writeLine, keyPress} from "./IOS.js";
2
+ import {colors} from "./colors.js";
3
+
4
+
5
+ //usage
6
+ main(async () => {
7
+
8
+ const y = new Input();
9
+
10
+ //accepting input from user
11
+ await y.readl("Enter Name: ", keyPress((key)=> {
12
+ if (key == "a"){
13
+
14
+ y.alt("a"); // restrict the character a
15
+ }
16
+ try{
17
+ // Only accept integer input whiles on input
18
+ int(y);
19
+ }
20
+ catch (e) {
21
+
22
+ for (let i = 0; i < 10; i++){
23
+ //restrict user to type only integers.
24
+ y.write(i.toString());
25
+ }
26
+ }
27
+ }));
28
+
29
+ let f = new Input();
30
+ await f.readl("Enter another name: ");
31
+ writeLine(typeof(int(y.value)), " ", f.value)
32
+
33
+ });
package/ios.js ADDED
@@ -0,0 +1,218 @@
1
+ import { colors } from "./colors.js"
2
+ let c = new colors();
3
+
4
+
5
+ export class Input {
6
+
7
+ constructor() {
8
+ this.input = "";
9
+ this.freezeChar = [];
10
+ this.altFreezeChar = [];
11
+
12
+ }
13
+ /**
14
+ * Returns the value of the object.
15
+ */
16
+ get value() {
17
+ return this.input;
18
+ }
19
+
20
+ /**
21
+ *
22
+ * @param {string} arg
23
+ * Prevent character from displaying.
24
+ */
25
+ alt(arg) {
26
+ this.freezeChar.push(arg);
27
+ }
28
+ /**
29
+ *
30
+ * @param {string} arg
31
+ * realse alt character enabling it to diplay.
32
+ */
33
+ release(arg) {
34
+ this.freezeChar.filter(v => v != arg);
35
+ }
36
+ /**
37
+ *
38
+ * @param {string} arg
39
+ * An inverse display. Only the given charactes will be display.
40
+ */
41
+ write (arg) {
42
+ this.altFreezeChar.push(arg);
43
+ }
44
+ /**
45
+ * reset the objects properties to it's default
46
+ */
47
+ reset() {
48
+ this.input = "";
49
+ this.altFreezeChar = [];
50
+ this.freezeChar = [];
51
+ }
52
+ /**
53
+ *
54
+ * @param {string} prompt -- What need to display as prompt to the user.
55
+ * @param {list} ch_res -- A list argument that is used to restrict character.
56
+ *
57
+ * The readl can accept the keyPress function to define a certain behavour when a key is pressed.
58
+ *@example
59
+ * await readl('Enter name' [], keypress(() => {
60
+ * if (key === "@"){
61
+ * process.exit();
62
+ * }
63
+ * }));
64
+ *
65
+ *Note: The readl must be used in the main function for proper behavour.
66
+ * @example
67
+ * main(async () => {
68
+ * let name = await readl('Enter name: ');
69
+ * });
70
+ * @returns
71
+ *
72
+ */
73
+ readl(prompt) {
74
+
75
+ process.stdin.setRawMode(true);
76
+ process.stdin.resume();
77
+ process.stdin.setEncoding('utf8');
78
+
79
+ return new Promise((res) => {
80
+
81
+ process.stdout.write(prompt);
82
+
83
+ const onData = (key) => {
84
+
85
+
86
+ if (key === '\u0003') {
87
+ process.stdin.removeListener('data', onData)
88
+ process.exit();
89
+ }
90
+ else if (key === "\r" || key == "\n") {
91
+ process.stdout.write('\n');
92
+ process.stdin.removeListener('data', onData)
93
+ res(this.input);
94
+
95
+ }
96
+ else if (key === '\u007f') {
97
+ if (this.input.length > 0) {
98
+ this.input = this.input.slice(0, -1);
99
+ process.stdout.clearLine();
100
+ process.stdout.cursorTo(0);
101
+ process.stdout.write(prompt + this.input);
102
+ return;
103
+ }
104
+ }
105
+ else {
106
+ if (this.altFreezeChar.length !== 0){
107
+ this.input += this.altFreezeChar.includes(key) ? key : "";
108
+ process.stdout.write(this.altFreezeChar.includes(key) ? key : "")
109
+ }
110
+ else {
111
+ this.input += this.freezeChar.length !== 0 & this.freezeChar.includes(key) ? "" : key;
112
+ process.stdout.write(this.freezeChar.length !== 0 & this.freezeChar.includes(key) ? "" : key);
113
+ }
114
+ }
115
+ }
116
+ process.stdin.on('data', onData);
117
+ });
118
+ }
119
+
120
+ }
121
+
122
+ /**
123
+ *
124
+ * @param {event} callback -- A keyboard function
125
+ */
126
+ export async function keyPress(callback) {
127
+ process.stdin.setRawMode(true);
128
+ process.stdin.resume();
129
+ process.stdin.setEncoding("utf8");
130
+
131
+ process.stdin.on("data", (key) => {
132
+ if (key === "\r" || key === "\n") {
133
+ return callback("ENTER");
134
+ }
135
+ if (key === "\u0003") {
136
+ writeLine("\noperation cancel by user.");
137
+ process.exit();
138
+ }
139
+ return callback(key);
140
+ });
141
+ }
142
+
143
+ /**
144
+ *
145
+ * @param {string} value - expected a string
146
+ * A function to display output on the screen.
147
+ */
148
+ export async function writeLine(...arg) {
149
+ let val = [];
150
+ let final = "";
151
+ for (let n of arg) {
152
+ val.push(n);
153
+ }
154
+ for (let i = 0; i < val.length; i++) {
155
+ final += val[i];
156
+ }
157
+ console.log(final);
158
+ }
159
+
160
+
161
+ export const highlight = (arg, prmpt) => {
162
+
163
+ process.stdout.write('\r\x1b[2K');
164
+
165
+ if (
166
+ arg.includes(this.input.split(" ")[this.input.split(" ").length - 1]) &
167
+ this.input.split(" ")[this.input.split(" ").length - 1] != ""
168
+ ) {
169
+ const word = this.input.split(" ")[this.input.split(" ").length - 1];
170
+ const wd = new RegExp(word, "g");
171
+ this.input = this.input.replaceAll(wd, c.yellow + word + c.RESET)
172
+ }
173
+ else { this.input = this.input }
174
+ process.stdout.write(this.input)
175
+ }
176
+
177
+ /**
178
+ *
179
+ * @param {any} callable
180
+ * The main function that wrap every asyncjs
181
+ */
182
+ export async function main(callable) {
183
+ await callable(callable);
184
+ process.exit();
185
+ }
186
+
187
+ /**
188
+ *
189
+ * @param {string} arg
190
+ * @param {float} arg -- convert string or float to integer values
191
+ */
192
+ export function int(arg) {
193
+ if (isNaN(arg)) {
194
+ throw "ValueError: expected integer value but string was given. " + arg;
195
+ }
196
+ else {
197
+ return parseInt(arg);
198
+ }
199
+ }
200
+
201
+ export function float(arg) {
202
+ if (isNaN(arg)) {
203
+ throw "ValueError: expected float value but string char was given. " + arg;
204
+ }
205
+ else {
206
+ return parseFloat(arg);
207
+ }
208
+ }
209
+
210
+ export function str(arg) {
211
+ return arg.toString();
212
+ }
213
+
214
+
215
+
216
+
217
+
218
+
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "interacter",
3
+ "version": "1.0.0",
4
+ "description": " A simple package that allows dev to interact with the CLI by accepting user input and displaying output with customizable behavour like restricting the some characters. It can live listen to user input to prevent so",
5
+ "keywords": [
6
+ "input",
7
+ "input_and_output",
8
+ "iostream",
9
+ "cli",
10
+ "interacter",
11
+ "output",
12
+ "reader",
13
+ "debug",
14
+ "userinput",
15
+ "ios"
16
+ ],
17
+ "homepage": "https://github.com/ElktrumElk/IOS#readme",
18
+ "bugs": {
19
+ "url": "https://github.com/ElktrumElk/IOS/issues"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/ElktrumElk/IOS.git"
24
+ },
25
+ "license": "ISC",
26
+ "author": "Elktrum Elk",
27
+ "type": "module",
28
+ "main": "ios.js",
29
+ "scripts": {
30
+ "test": "echo \"Error: no test specified\" && exit 1"
31
+ }
32
+ }
package/test.js ADDED
@@ -0,0 +1,5 @@
1
+
2
+ let value = "hey how are you"
3
+ let g = value.split(" ", 2)
4
+
5
+ console.log(g);