The second approach should definitely work, see

https://github.com/apache/karaf/blob/karaf-3.0.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/MoreAction.java#L40

The exception is a bit unexpected.  Maybe you're compiling against a very
old version of jline ? In JLine 1.x, the Terminal was an abstract class,
but it has been changed to an interface in jline 2.x.

2016-12-07 19:16 GMT+01:00 Max Spring <m2spr...@springdot.org>:

> Is there a way to get the actual jline terminal object in a Karaf command?
> I want to format the command output depending on the terminal width.
> I'm on Ubuntu 16.04, Java 1.8.0_60-x64, and Karaf 3.0.5.
>
> This one
>
>   package org.example;
>
>   import jline.TerminalFactory;
>   import org.apache.karaf.shell.commands.Command;
>   import org.apache.karaf.shell.console.OsgiCommandSupport;
>
>   @Command(scope = "example", name = "term-wdith", description = "get the
> terminal width")
>   public class TermWidthCmd extends OsgiCommandSupport{
>
>       protected Object doExecute() throws Exception {
>           int w = TerminalFactory.get().getTerminalWidth();
>
>           return null;
>       }
>   }
>
> gives:
>
> | 2016-12-07 10:10:17,443 | ERROR | l for user karaf | ShellUtil
>               | 27 - org.apache.karaf.shell.console - 3.0.5 | Exception
> caught while executing command
> | java.lang.IncompatibleClassChangeError: Found interface jline.Terminal,
> but class was expected
> |       at org.example.TermWidthCmd.doExecute(TermWidthCmd.java:11)
> |       at org.apache.karaf.shell.console.AbstractAction.execute(
> AbstractAction.java:33)
> |       at org.apache.karaf.shell.console.OsgiCommandSupport.execute(
> OsgiCommandSupport.java:39)
> |       at org.apache.karaf.shell.commands.basic.AbstractCommand.
> execute(AbstractCommand.java:33)
> |       at Proxy68e7a8f7_5f52_4001_8cfa_f4b37a3fc958.execute(Unknown
> Source)
> |       at Proxy68e7a8f7_5f52_4001_8cfa_f4b37a3fc958.execute(Unknown
> Source)
> |       at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandPr
> oxy.java:78)
> |       at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.jav
> a:480)
> |       at org.apache.felix.gogo.runtime.Closure.executeStatement(Closu
> re.java:406)
> |       at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
> |       at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
> |       at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
> |       at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(Com
> mandSessionImpl.java:94)
> |       at org.apache.karaf.shell.console.impl.jline.ConsoleImpl.run(
> ConsoleImpl.java:210)
> |       at org.apache.karaf.shell.console.impl.jline.LocalConsoleManage
> r$2$1$1.run(LocalConsoleManager.java:109)
> |       at java.security.AccessController.doPrivileged(Native
> Method)[:1.8.0_60]
> |       at org.apache.karaf.jaas.modules.JaasHelper.doAs(JaasHelper.jav
> a:57)[28:org.apache.karaf.jaas.modules:3.0.5]
> |       at org.apache.karaf.shell.console.impl.jline.LocalConsoleManage
> r$2$1.run(LocalConsoleManager.java:102)[27:org.apache.karaf.
> shell.console:3.0.5]
>
> The 2nd approach
>
>   package org.example;
>
>   import jline.NoInterruptUnixTerminal;
>   import jline.Terminal;
>   import org.apache.karaf.shell.commands.Command;
>   import org.apache.karaf.shell.console.OsgiCommandSupport;
>
>   @Command(scope = "example", name = "term-wdith", description = "get the
> terminal width")
>   public class TermWidthCmd extends OsgiCommandSupport{
>
>       protected Object doExecute() throws Exception {
>           Terminal term = session != null ? (Terminal)
> session.get(".jline.terminal") : null;
>           NoInterruptUnixTerminal uterm = (NoInterruptUnixTerminal)term;
>           int w = term.getTerminalWidth();
>
>           return null;
>       }
>   }
>
> basically gives the same exception:
>
> | 2016-12-07 10:09:11,964 | ERROR | l for user karaf | ShellUtil
>               | 27 - org.apache.karaf.shell.console - 3.0.5 | Exception
> caught while executing command
> | java.lang.IncompatibleClassChangeError: Found interface jline.Terminal,
> but class was expected
> |       at org.example.TermWidthCmd.doExecute(TermWidthCmd.java:14)
> |       at org.apache.karaf.shell.console.AbstractAction.execute(
> AbstractAction.java:33)
> |       at org.apache.karaf.shell.console.OsgiCommandSupport.execute(
> OsgiCommandSupport.java:39)
> |       at org.apache.karaf.shell.commands.basic.AbstractCommand.
> execute(AbstractCommand.java:33)
> |       at Proxy1c55bc35_2cb4_47b2_b9f4_f593e49a68ce.execute(Unknown
> Source)
> |       at Proxy1c55bc35_2cb4_47b2_b9f4_f593e49a68ce.execute(Unknown
> Source)
> |       at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandPr
> oxy.java:78)
> |       at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.jav
> a:480)
> |       at org.apache.felix.gogo.runtime.Closure.executeStatement(Closu
> re.java:406)
> |       at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
> |       at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
> |       at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
> |       at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(Com
> mandSessionImpl.java:94)
> |       at org.apache.karaf.shell.console.impl.jline.ConsoleImpl.run(
> ConsoleImpl.java:210)
> |       at org.apache.karaf.shell.console.impl.jline.LocalConsoleManage
> r$2$1$1.run(LocalConsoleManager.java:109)
> |       at java.security.AccessController.doPrivileged(Native
> Method)[:1.8.0_60]
> |       at org.apache.karaf.jaas.modules.JaasHelper.doAs(JaasHelper.jav
> a:57)[28:org.apache.karaf.jaas.modules:3.0.5]
> |       at org.apache.karaf.shell.console.impl.jline.LocalConsoleManage
> r$2$1.run(LocalConsoleManager.java:102)[27:org.apache.karaf.
> shell.console:3.0.5]
>
> What am I missing?
>
> Thanks!
> -Max
>
>


-- 
------------------------
Guillaume Nodet
------------------------
Red Hat, Open Source Integration

Email: gno...@redhat.com
Web: http://fusesource.com
Blog: http://gnodet.blogspot.com/

Reply via email to