ajalt / mordant

Multiplatform text styling for Kotlin command-line applications
https://ajalt.github.io/mordant/
Apache License 2.0
986 stars 34 forks source link

TerminalDetection not detecting IntelliJ #67

Open bkahlert opened 2 years ago

bkahlert commented 2 years ago

The TerminalDetection computes AnsiLevel.NONE although I'm running my unit tests inside of IntelliJ (Build #IU-221.6008.13, built on July 19, 2022).

The runningInIdeaJavaAgent method does not match. On my mac ManagementFactory.getRuntimeMXBean() returns the following elements

0 = "-Dorg.gradle.internal.worker.tmpdir=/Users/bkahlert/Development/com.bkahlert/kommons/build/tmp/jvmTest/work"
1 = "-Dorg.gradle.native=false"
2 = "-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=127.0.0.1:56426"
3 = "-javaagent:/Users/bkahlert/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.6.1/97fd74ccf54a863d221956ffcd21835e168e2aaa/kotlinx-coroutines-core-jvm-1.6.1.jar"
4 = "--add-opens=java.base/java.util=ALL-UNNAMED"
5 = "--add-opens=java.base/java.lang=ALL-UNNAMED"
6 = "-Xms128m"
7 = "-Xmx512m"
8 = "-Dfile.encoding=UTF-8"
9 = "-Duser.country=US"
10 = "-Duser.language=en"
11 = "-Duser.variant"
12 = "-ea"

System.getProperty("sun.java.command") reports "worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Test Executor 61'"

My environment reported by System.getenv() does neither contain IDEA_INITIAL_DIRECTORY, __INTELLIJ_COMMAND_HISTFILE__ nor TERMINAL_EMULATOR.

The following environment properties might help you:

...
"JAVA_MAIN_CLASS_56145" -> "worker.org.gradle.process.internal.worker.GradleWorkerMain"
"TOOLBOX_VERSION" -> "1.25.12424"
"__CFBundleIdentifier" -> "com.jetbrains.intellij"
"SHELL" -> "/bin/zsh"
"__CF_USER_TEXT_ENCODING" -> "0x40E34E6:0x0:0x0"
ajalt commented 2 years ago

Until IntelliJ is consistent about setting an environment variable, it's probably not possible to detect it in all cases. If you know of any envvar etc. that we can use, I'm happy to add it.

Does that __CFBundleIdentifier value change if you invoke your program from gradle on the command line, or via java -jar?

sschuberth commented 1 year ago

For the record, in IntelliJ IDEA 2023.2 I'm getting colored output with Mordant in the Run tab. However, the terminal's width does not seem to be detected correctly (TerminalInfo says it's 79), as my grid is truncated.

ajalt commented 1 year ago

I suspect it's not possible to detect terminal size in IntelliJ's run console. On Windows at least, GetConsoleScreenBufferInfo fails. Even powershell itself can't detect anything.

joseluisgs commented 1 year ago

I have a problem when I try to create a fat jar, no problem with IntellI console when I use a run. But when a create a fat jar, I use a java -jar, mordant crashes

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Library
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
        at com.github.ajalt.mordant.internal.jna.JnaWin32MppImpls.<init>(JnaWin32MppImpls.kt:77)
        at com.github.ajalt.mordant.internal.MppImplKt.<clinit>(MppImpl.kt:107)
        at com.github.ajalt.mordant.terminal.TerminalDetection.isJediTerm(TerminalDetection.kt:160)
        at com.github.ajalt.mordant.terminal.TerminalDetection.isIntellijRunActionConsole(TerminalDetection.kt:196)
        at com.github.ajalt.mordant.terminal.TerminalDetection.detectTerminal(TerminalDetection.kt:16)
        at com.github.ajalt.mordant.terminal.StdoutTerminalInterface.<init>(StdoutTerminalInterface.kt:17)
        at com.github.ajalt.mordant.terminal.Terminal.<init>(Terminal.kt:56)
        at com.github.ajalt.mordant.terminal.Terminal.<init>(Terminal.kt:45)
        at MainKt.<clinit>(Main.kt:14)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)

It is a problem with JNA, you need to include it to execute as a jar https://mvnrepository.com/artifact/net.java.dev.jna/jna/5.13.0

No problem with 2.0.0-beta9

sschuberth commented 8 months ago

Maybe the whole issue will get resolved with https://blog.jetbrains.com/idea/2024/02/the-new-terminal-beta-is-now-in-jetbrains-ides/?

robstoll commented 3 months ago

on Unix like Systems it could work to see if the _ env variable contains idea, most likely also not that reliable but better than nothing IMO.