Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination in show images misbehaves with PgUp/PgDown #108

Open
pupssman opened this issue Apr 12, 2016 · 8 comments
Open

Pagination in show images misbehaves with PgUp/PgDown #108

pupssman opened this issue Apr 12, 2016 · 8 comments
Labels

Comments

@pupssman
Copy link

What to do:

  • open perspective
  • say show images
  • see pagination activate
  • press PgUp/PgDown
  • observe error messages.

Example output:

perspective>show images
Results contain 43 entries. Showing first 20 entries.
Press Space, Enter or n to show next page, p or w to show previous page, g to show first page, G to show last page, a number key to show specific page and q to type next command.
Showing page 1 of 3
....
├────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ Skeleton 1                                 │ <snip> │ saved                                      │ 5 months ago                              │
│                                            │ <snip>          │                                            │                                           │
│                                            │ <snip>                                  │                                            │                                           │
├────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ Ubuntu 08.04 hardy (updated 2014-09-19)    │ <snip> │ saved                                      │ 2 years ago                               │
│                                            │ <snip>          │                                            │                                           │
│                                            │ <snip>                                  │                                            │                                           │
└────────────────────────────────────────────┴────────────────────────────────────────────┴────────────────────────────────────────────┴───────────────────────────────────────────┘

Wrong page number: 6. Should be one of 1..3.
Wrong page number: 5. Should be one of 1..3.
Wrong page number: 6. Should be one of 1..3.
Wrong page number: 5. Should be one of 1..3.
Wrong page number: 6. Should be one of 1..3.
Wrong page number: 5. Should be one of 1..3.
Wrong page number: 6. Should be one of 1..3.
Wrong page number: 5. Should be one of 1..3.

Also, pagination notice is gone off-the-screen due to line wrap of the output.

I am running latest (of now) perspective docker container from Ubuntu.
Terminal dims are

pupssman@zeppelin:~ $ tput cols
118
pupssman@zeppelin:~ $ tput lines
103
pupssman@zeppelin:~ $ 
@vania-pooh vania-pooh added this to the 1.2.1 milestone Apr 12, 2016
@vania-pooh
Copy link
Member

@pupssman will test PgUp\PgDn soon. You may want to modify the following settings to fix width issue:

perspective> set table_width = 100 # By default it's 180 columns, you can set something about 100
perspective> set page_size = 5 # This is about page size

@pupssman
Copy link
Author

Would be super-tool for perspective to automatically detect shell properties

@vania-pooh
Copy link
Member

@pupssman haven't already implemented this because of http://stackoverflow.com/questions/1286461/can-i-find-the-console-width-with-java Seems that JLine does not work reliably on all platforms. And you can also save these settings to RC file and forget about this issue.

@vania-pooh
Copy link
Member

@pupssman any idea how to check that PgDn is presed in Java? :) Can't find the way how to do this.

@pupssman
Copy link
Author

Ehm, it looks like it is already being detected but interpreted wrong.
Did you by any chance manage to reproduce the issue?

@vania-pooh
Copy link
Member

@pupssman yes, same thing on my Mac. For some reason PgDn key is being interpreted just as I'm pressing numeric key = 6. In your example you have only 3 pages that's why it complains about wrong page number. If you have more than 6 pages it switches to page 5 or 6 depending on key.

@vania-pooh vania-pooh modified the milestones: 1.2.2, 1.2.1 Apr 15, 2016
@vania-pooh
Copy link
Member

I will postpone this for the future because after deep investigation it seems that handling PageDown is not a trivial task because in Bash it's represented as an Ansi escape sequence (i.e. 3 or 4 chars). Right now we expect only one char so the working solution to handle PageDown is like that (it even requires Executors!!):

    static <T> Optional<T> routeByKey(ConsoleReader consoleReader, Map<Predicate<String>, Function<String, T>> routes, Consumer<String> onInvalidKey, Consumer<Exception> onException) {
        boolean needShutdown = false;
        try {
            if (consoleReader == null) {
                consoleReader = new ConsoleReader();
                needShutdown = true;
            }

            //Trying to read one character first
            String key = String.valueOf((char) consoleReader.readCharacter());
            for (Predicate<String> keyPredicate : routes.keySet()) {
                if (keyPredicate.test(key)) {
                    return Optional.ofNullable(routes.get(keyPredicate).apply(key));
                }
            }

            //Ansi escape sequences like PageUp or PageDown
            final StringBuilder stringBuilder = new StringBuilder(key);
            final ConsoleReader cr = consoleReader;
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            Runnable task = () -> {
                while (true) {
                    try {
                        NonBlockingInputStream inputStream = (NonBlockingInputStream) cr.getInput();
                        int code = inputStream.read(5);
                        if (code < 0) {
                            break;
                        }
                        stringBuilder.append((char) code);
                    } catch (IOException ignored) {
                    }
                }
            };
            executorService.submit(task);
            executorService.awaitTermination(50, TimeUnit.MILLISECONDS);
            executorService.shutdown();
            String anotherKey = stringBuilder.toString();
            for (Predicate<String> keyPredicate : routes.keySet()) {
                if (keyPredicate.test(anotherKey)) {
                    return Optional.ofNullable(routes.get(keyPredicate).apply(anotherKey));
                }
            }

            //No valid key found
            if (onInvalidKey != null) {
                onInvalidKey.accept(anotherKey);
            }
            return Optional.empty();
        } catch (Exception e) {
            if (onException != null) {
                onException.accept(e);
            }
            return Optional.empty();
        } finally {
            if (consoleReader != null && needShutdown) {
                consoleReader.shutdown();
            }
        }
    }

The PageDown key can be coded like:

    public static final String PAGE_DOWN = new String(new char[]{
            (char) 27,
            (char) 91,
            (char) 54,
            (char) 126
    });

I implemented all this stuff and even added a test but it only works for the first page down press. Then something internal in ConsoleReader prevents next key press to work (probably some colored output processing logic).

@vania-pooh vania-pooh removed this from the 1.2.2 milestone Apr 24, 2016
@pupssman
Copy link
Author

Wow, weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants