NodeJS Input prompt-sync module unexpected behavior - node.js

I am using node js npm prompt-sync module. I have read that prompt-sync does not handle multiline inputs well, but none of my prompt inputs have \n characters. My expected output is to prompt, receive the input, then begin handling different prompts. This same code (similar to Main.js) works fine in Jest and produces the expected result. The observed result is that, upon answering anything, the line is printed again automatically with the given input without asking again. Then, there is no prompt given. But, upon typing more characters, it will repeat the prompt. It will also add previously repeated characters to the list of characters being returned.
Why is this happening and how can I produce the expected behavior?
UserInteraction.js:
class UserInteraction {
createPL(): {
console.log("Generating new PL: ");
let double_sided = prompt("DS? (Y/N): ").toLowerCase() === "y";
// other, different prompts...the expected output is a different prompt each time.
// use inputs for creating an object
}
createPLMap(): {
let cont = true;
let plMap = new Map();
while (cont) {
let pl= this.createPL();
// CODE REMOVED
}
return plMap;
}
Main.js:
const UserInteraction = require('./UserInteraction').UserInteraction;
async function MainAwait() {
let ui = new UserInteraction();
let pricelineSMap = ui.createPLMap();
}
MainAwait();
Shell file:
Main.sh:
node --trace-warnings ./js/Main.js
Output:
./Main.sh
Generating new PL:
DS? (Y/N): y
DS? (Y/N): y
y
DS? (Y/N): y
y
y
DS? (Y/N): y
y
y
y
DS? (Y/N): y
y
y
y

Related

How to print more than one value in the same line?

A simple example
Print("This is a number {0}", 1) // results in 4
How can I print 1 2 3 in the same line?
I have tried
Print(1, 2, 3) // Does not work
The forloop also does not work.
The while loop below prints the elements but each in a separate line since we do not have control over the line feed character \n:
fn Main() -> i32 {
var a: [i32;3] = (1, 2, 3); // Define array containing the numbers 1,2,3
var i:i32 =0;
while(i<3){
Print("{0}", a[i]);
i= i+1;
}
return 0;
}
which results in
1
2
3
here is the code
How can I get 1 2 3?
Short explanation: Providing more than 2 arguments to Print is currently not supported, so is not including a line break.
Details
This is definitely something that will change in the future, as the language is in its early design phases. You can find the source for Print as an instrinsic (non-carbon native) here: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/interpreter/type_checker.cpp#L2297
case IntrinsicExpression::Intrinsic::Print:
// TODO: Remove Print special casing once we have variadics or
// overloads. Here, that's the name Print instead of __intrinsic_print
// in errors.
if (args.size() < 1 || args.size() > 2) {
return CompilationError(e->source_loc())
<< "Print takes 1 or 2 arguments, received " << args.size();
}
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 0", arena_->New<StringType>(),
&args[0]->static_type(), impl_scope));
if (args.size() >= 2) {
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 1", arena_->New<IntType>(),
&args[1]->static_type(), impl_scope));
}
If you look at the code you will also see that the type of input variable is limited to just integer types. It would be easy enough create a PR to manually add support for 2-3 if you want :)

Game Maker - Checking position of multiple objects of same name

How do you check the position values of two of the same object in a room? I have two of the same object in my room and need to find the x and y positioning of each.
with (objName) {
// now x is each objName.x, and y is each objName.x, while to access object from which you called you need to use other.xxx (except VAR defined variables)
}
for example, to get all X positions:
var xpos = [], _a = 0;
with (objName) {
xpos[a++] = x;
}
// xpos[0] is x of first object, xpos[1] of second etc.

single line console output as a variable changes instead of one console.log per variable value?

Is there means in node.js to advance a counter within the console display to a single line, instead of a console.log output per line?
e.g.
let x = 1
while (x <= 10) {
console.log(`The value assigned to 'x' is now: ${x}`);
x++;
}
prints 10 lines for x = 1 through 10.
Is it possible instead to have a single line which remains static as the x value increments? As a use case, if the value of x incremented to one million, the console would display the one line until x was 1000000.
This does the trick for a single line:
const process = require('process');
let x = 1;
process.stdout.write(`The count is now at: ${x}`);
const counter = () => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
x++;
process.stdout.write(`The count is now at: ${x}`);
if(x >= 10) {
clearInterval(cycle);
process.stdout.write('\n');
}
};
const cycle = setInterval(counter, 1000);
...and then there's this: https://github.com/sindresorhus/log-update

perl6 Thread reading interference

I need to have multiple threads each read from the same socket or from
$*IN; however, there seems to be error because each is trying to read from the same source (I think). What is the best way to resolve this problem? Thanks !!
my $x = start { prompt("I am X: Enter x: "); }
my $y = start { prompt("I am Y: Enter y: "); }
await $x, $y;
say $x;
say $y;
And here are the errors:
I am X: Enter x: I am Y: Enter y: Tried to get the result of a broken Promise
in block <unit> at zz.pl line 4
Original exception:
Tried to read() from an IO handle outside its originating thread
in block at zz.pl line 1
Thanks !!
On the latest development snapshot of Rakudo, your code actually works without throwing any exception on my system...
However, it still immediately asks for both values (I am X: Enter x: I am Y: Enter y:).
To make the second prompt wait until the first one has completed, you could use a Lock:
#--- Main code ---
my $x = start { synchronized-prompt "I am X: Enter x: "; }
my $y = start { synchronized-prompt "I am Y: Enter y: "; }
await $x, $y;
say $x.result;
say $y.result;
#--- Helper routines ---
BEGIN my $prompt-lock = Lock.new;
sub synchronized-prompt ($message) {
$prompt-lock.protect: { prompt $message; }
}
The tricky part is that the lock needs to be initialized before the threads start using it concurrently. That's why I call Lock.new outside of the synchronized-prompt subroutine, in the mainline of the program. Instead of doing it at the top of the program, I use a BEGIN phaser so that I can place it next to the subroutine.

D3, retrieve the x position of a <g> element and apply multiple transform

With D3.js I want to transform the x of a group many times (eg. by clicking a button a causing a transform for every click), starting from the current position of the group. Problem is I can't retrieve the x of the group and, even if I find it, I can't find any built-in function that lets me change the x of a group starting from its current x. Am I missing something important? It's very strange I can't get the x of an element added to an SVG "stage".
My code is the following (edited for Lars): I created the nodes the way you suggested me yesterday (cloning them).
var m=[5,10,15,20,25,30];
var count=0;
d3.select('svg').on("mouseup", function(d, i){
count+=0.5;
var n = d3.selectAll(".myGroup");
n.each(function(d,i) {
if(i!=0){
// here I need to know the current x of the current n element but I
// can't get it
// this causes error "Uncaught TypeError: undefined is not a function"
// var currentx = d3.transform(this.attr("transform")).translate[0];
this.setAttribute("transform", "translate("+((100/m[i])*count)+",10)");
}
});
});
Getting and changing the position of a g element is relatively straightforward. For example like this:
var g = d3.select("#myG");
// get x position
var currentx = d3.transform(g.attr("transform")).translate[0];
// set x position
g.attr("transform", "translate(" + (currentx + 100) + ",0)");
Edit:
If you have a raw DOM element, select it with D3 first:
var currentx = d3.transform(d3.select(this).attr("transform")).translate[0];

Resources