#Need explanation

65 messages · Page 1 of 1 (latest)

trail cipher
#

I already tried ai to understand this code but I still can't figure it put

mild slateBOT
#

This post has been reserved for your question.

Hey @trail cipher! Please use /close or the Close Post button 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.

trail cipher
#
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

cold forge
#

event is a variable from the lambda

trail cipher
cold forge
#

and the switch here uses switch patternmatching which is a fairly new feature

trail cipher
#

and i also wonder why we need to return true / false in switch case

trail cipher
cold forge
#

if (event instanceof KeyEvent k && k.isChar('q') && k.hasCtrl())

cold forge
#

ig the lambda fof tui.run needs to return a boolean

trail cipher
#

for some reason i am more confused now

cold forge
#

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";
};
cold forge
trail cipher
#

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

cold forge
cold forge
#

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.

trail cipher
#

ohh got it it its like tui.run( lambda )
i was thinking like tui.run()->lambda

cold forge
#

yes

trail cipher
#

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)->{}

cold forge
#

yes, two parameters that are using different functional interfaces

cold forge
trail cipher
#

i don't get it what is point of returning anything there nothing seems to change anything

cold forge
#

then the functional interface probably expects returning a boolean

cold forge
#

maybe check the documentation

trail cipher
# cold forge I don't know what the `run()` method uses the returned value for

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);
}
cold forge
trail cipher
#

yup i read that but earlier i tried using false too but i don't think it changed anything

#

let me try once again

cold forge
#

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

cold forge
trail cipher
#

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

cold forge
trail cipher
#

yes i was playing around to figure out earlier

cold forge
#

You only need to return true if you do something that requires redrawing

trail cipher
#
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') ;

trail cipher
#

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?

terse pollen
trail cipher
#

well this time i was confused because of lambda to be honest i knew the switch case statement

trail cipher
cold forge
#

Do you have a minimal reproducer?

trail cipher
cold forge
#

Maybe a bug in the library? idk

trail cipher
#

like if you did thats like very fast

#

so shall i close this ?

cold forge
#

I looked at the code but I'm not running it

#

(and the batch file is specific to your system btw)

trail cipher