Skip to content

Commit

Permalink
add colors to node scanner example
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Nov 10, 2024
1 parent b285a70 commit aec0850
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"build": "npx tsc",
"lint": "yarn run eslint ./ts/*.ts && yarn run prettier ./ts/*.ts --check",
"format": "yarn run prettier --write ./ts/*"
},
"dependencies": {
"ansi-colors": "^4.1.3"
}
}
20 changes: 18 additions & 2 deletions examples/node/ts/scanner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as readline from "readline/promises";
import * as fs from "fs";
import * as timer from "timers/promises";
import * as colors from "ansi-colors";
import { RF24, CrcLength, DataRate, FifoState } from "@rf24/rf24";

const io = readline.createInterface({
Expand Down Expand Up @@ -82,6 +83,21 @@ export class App {
console.log(divider);
}

/**
* Print a colored hexadecimal digit to represent the `total` signal count for a single channel.
*/
print_signals(total: number) {
if (total == 0) {
process.stdout.write("-");
} else if (total < 5) {
process.stdout.write(colors.green(total.toString(16).toUpperCase()));
} else if (total < 10) {
process.stdout.write(colors.yellow(total.toString(16).toUpperCase()));
} else {
process.stdout.write(colors.red(total.toString(16).toUpperCase()));
}
}

/**
* The scanner behavior.
*/
Expand Down Expand Up @@ -109,7 +125,7 @@ export class App {
this.radio.flushRx(); // discard any packets (noise) saved in RX FIFO
}
const total = caches[channel];
process.stdout.write(total > 0 ? total.toString(16) : "-");
this.print_signals(total);

channel += 1;
let endl = false;
Expand All @@ -133,7 +149,7 @@ export class App {
// finish printing current cache of signals
for (let i = channel; i < CHANNELS; i++) {
const total = caches[i];
process.stdout.write(total > 0 ? total.toString(16) : "-");
this.print_signals(total);
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ ajv@^6.12.4:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ansi-colors@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.3.tgz#37611340eb2243e70cc604cad35d63270d48781b"
integrity sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==

ansi-styles@^4.1.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.3.0.tgz#edd803628ae71c04c85ae7a0906edad34b648937"
Expand Down

0 comments on commit aec0850

Please sign in to comment.