#Need explanation
65 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @trail cipher! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 720 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
public void runApp() throws Exception {
try (var tui = TuiRunner.create()) {
tui.run((event, runner) ->
switch (event) {
case KeyEvent k when k.isChar('q') && k.hasCtrl() -> {
runner.quit();
yield true;
}
default -> false;
},
frame -> {
frame.renderWidget(Paragraph.from("Hello"), frame.area());
}
);
}
}
i get the try part and the lamba part. i am very confused in the event and switch case statements
event is a variable from the lambda
yes i know that i am trying to convert this into if else statement
and the switch here uses switch patternmatching which is a fairly new feature
and i also wonder why we need to return true / false in switch case
well i read the java docs and i got it somewhat but this is like next level
if (event instanceof KeyEvent k && k.isChar('q') && k.hasCtrl())
this is because it's using switch expressions as well and that allows returning a value from a case block resulting in the switch block returning the corresponding value
ig the lambda fof tui.run needs to return a boolean
for some reason i am more confused now
This is more or less a translation of the first case
case KeyEvent k means "this has to be a KeyEvent for it to be executed and ut is assigned to a variable named k
when means "here comes an additional condition that has to be true for executing the block
and the part of returning works like this:
int something = 1;
String s = switch(something) {
case 0 -> "zero";
case 1 -> "one";
case 2 -> "two";
default -> "something else";
};
Can you understand that code?
we can only make lambda expression for a functional inteface/SAM and i figured out tui is a instance of TuiRunner. so it means TUIRunner is a functinal interface right?
but TuiRunner is a class
No. The TuiRunner#run method has a parameter and the type of that parameter must be of a functional interface
e.g. BiPredicate<Event, TuiRunner>
You are calling the run method and that method expects a parameter. That parameter is a functional interface so you can use a lambda for it.
ohh got it it its like tui.run( lambda )
i was thinking like tui.run()->lambda
yes
ok its like we have
tui.run(
(event, runner) -> {
if (event instanceof KeyEvent k && k.hasCtrl() && k.isCharIgnoreCase('q')) {
tui.close();
}
}
,
frame -> {
frame.renderWidget(Paragraph.from("Hello"), frame.area());
}
);
we got two lambda interface in run right?
one is (event, runner)->{}
And another is (frame)->{}
yes, two parameters that are using different functional interfaces
Does that compile without the first lambda returning true?
nope
i don't get it what is point of returning anything there nothing seems to change anything
then the functional interface probably expects returning a boolean
I don't know what the run() method uses the returned value for
maybe check the documentation
yup got it
@FunctionalInterface
public interface EventHandler {
/**
* Handles an event.
*
* @param event the event to handle
* @param runner the TUI runner (can be used to call {@link TuiRunner#quit()})
* @return {@code true} if the UI should be redrawn, {@code false} otherwise
*/
boolean handle(Event event, TuiRunner runner);
}
so if you return true from the first lambda it means it should redraw the UI
yup i read that but earlier i tried using false too but i don't think it changed anything
let me try once again
I guess you aren't doing anything that requires redrawing in the lambda
but if you change something in the UI, it should probably redraw it
and in that case, you need to return true
things make sence now it don't work if everything returns false
but i guess in earler version i made default -> true so thats why it gets updated
The default is just used when nothing else matches in the switch
yes i was playing around to figure out earlier
You only need to return true if you do something that requires redrawing
switch (event) {
case KeyEvent k when k.isChar('q') && k.hasCtrl() -> {
runner.quit();
yield false;
}
case KeyEvent k when k.isKey(KeyCode.DOWN)->{
state.selectNext(3);
yield true;
}case KeyEvent k when k.isKey(KeyCode.UP)->{
state.selectPrevious();
yield true;
}case KeyEvent k when k.isKey(KeyCode.CHAR)->{
textState.insert(k.character());
yield true;
}case KeyEvent k when k.isKey(KeyCode.BACKSPACE)->{
textState.deleteBackward();
yield true;
}case KeyEvent k when k.isKey(KeyCode.DELETE)->{
textState.deleteForward();
yield true;
}case KeyEvent k when k.isKey(KeyCode.LEFT)->{
textState.moveCursorLeft();
yield true;
}case KeyEvent k when k.isKey(KeyCode.RIGHT)->{
textState.moveCursorRight();
yield true;
}
default -> false;
for some reason pressing BACKSPACE makes textState.insert('h') ;
i am using https://tamboui.dev/ btw
TamboUI - Build beautiful terminal UIs in Java. Modern, powerful, and fun. Inspired by ratatui and bubbletea.
What do you mean with that?
like its a InputTextWedget and so i want when i press backspace it deletes backwards but instead it just add h in my input text
would you like to try my code in your machine?
Didn't you ask like the same question one month ago? #1486760641002733821 message
man am i really that stupid. i didn't even know that
well this time i was confused because of lambda to be honest i knew the switch case statement
@terse pollen can you help me with this ?
Do you have a minimal reproducer?
just run.bat https://github.com/Neo-0x01/TUI
Maybe a bug in the library? idk
did you checked it?
like if you did thats like very fast
so shall i close this ?
I looked at the code but I'm not running it
(and the batch file is specific to your system btw)
f i reveled my name .