Feature - Implemented Terminal color output

Obeliks

Member
Attached is the latest version of the patches, unchanged since this post:

I've decided that it makes more sense to have all other tags in the output, but printed in gray, otherwise stuff like tables looks really garbled. It still does not look like much, but better than having the unformatted HTML, and better than just the text:
 

Attachments

  • 0001-fix-don-t-silence-stdout-after-cli_execute_output.patch
    3 KB · Views: 1
  • 0002-feat-support-printing-colors-on-command-line.patch
    293.1 KB · Views: 3

fronobulax

Developer
Staff member
r20686 and r20687

Untested by me :)

My patch utilities did not like the jar file. I found what I hope was the right jar and manually added it to src.
 

blueflamepants

New member
I'm running into an issue with this that's preventing KoLmafia from loading. I'm running FreeBSD and for security reasons I have /tmp mounted with execution turned off.

Code:
% /usr/local/bin/java -jar -Djava.awt.headless=true KoLmafia.jar --CLI

KoLmafia v20.7 r20702
Released on July 30, 2020

Currently Running on FreeBSD
Local Directory is /usr/home/username/.kolmafia
Using Java 1.8.0_275

Failed to load native library:jansi-2.3.2-76e3c82f177e22ef-libjansi.so. osinfo: FreeBSD/x86_64
java.lang.UnsatisfiedLinkError: /tmp/jansi-2.3.2-76e3c82f177e22ef-libjansi.so: /tmp/jansi-2.3.2-76e3c82f177e22ef-libjansi.so: mmap of data failed: Permission denied
Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.fusesource.jansi.AnsiConsole.ansiStream(AnsiConsole.java:239)
        at org.fusesource.jansi.AnsiConsole.initStreams(AnsiConsole.java:542)
        at org.fusesource.jansi.AnsiConsole.systemInstall(AnsiConsole.java:496)
        at net.sourceforge.kolmafia.KoLmafiaTUI.initialize(KoLmafiaTUI.java:23)
        at net.sourceforge.kolmafia.KoLmafia.main(KoLmafia.java:389)
Caused by: java.lang.RuntimeException: Unable to load jansi native library
        at org.fusesource.jansi.internal.JansiLoader.initialize(JansiLoader.java:62)
        at org.fusesource.jansi.internal.CLibrary.<clinit>(CLibrary.java:30)
        ... 5 more
Caused by: java.lang.Exception: No native library found for os.name=FreeBSD, os.arch=x86_64, paths=[/org/fusesource/jansi/internal/native/FreeBSD/x86_64:/usr/local/lib]
        at org.fusesource.jansi.internal.JansiLoader.loadJansiNativeLibrary(JansiLoader.java:333)
        at org.fusesource.jansi.internal.JansiLoader.initialize(JansiLoader.java:60)

% mount
zpool/special/tmp on /tmp (zfs, local, noatime, noexec, nosuid, nfsv4acls)

It seems from https://github.com/fusesource/jansi/issues/204 Jansi doesn't support failing gracefully when the native library fails to load. Is there a way to turn Jansi off on KoLmafia's side or is my only option to revert to a version before this was merged?
 

MCroft

Developer
Staff member
Have you tried messing with the undocumented but in the source code properties for jansi?

JansiLoader.java says ...
Java:
private static File getTempDir() {
    return new File(System.getProperty("jansi.tmpdir", System.getProperty("java.io.tmpdir")));
}
 

heeheehee

Developer
Staff member
Wrapping AnsiConsole.systemInstall() in a try/catch seems to let it fall back to plain text. Let me know if r20703 works for you.
 

heeheehee

Developer
Staff member
(KoLmafiaTUI is a weird hybrid of formatting styles that clash, and I'm rather confused now that I'm looking at my code more closely. Maybe I'll tackle it some other time.)
 

Obeliks

Member
(KoLmafiaTUI is a weird hybrid of formatting styles that clash, and I'm rather confused now that I'm looking at my code more closely. Maybe I'll tackle it some other time.)
Sorry if my code caused any trouble. I tried to keep the formatting as I perceived it is used in the project, even though it's not very "standard Java" in some regards.
 

heeheehee

Developer
Staff member
Oh, it's fine. To be honest, I don't actually care about the formatting style. My preference would be to move to some sort of automatic formatting (enforced on commit etc) so that we stop having this discussion.

(also for reasons we use tabs instead of spaces and I want to not care about these things.)
 

MCroft

Developer
Staff member
If you’re using IntelliJ, there’s a syntax file on the wiki. There may be files for other IDEs. Otherwise, I agree with heeheehee. I would be happy to add an ANT target to format modified source pages if I had a tool with that feature.
 

blueflamepants

New member
@MCroft Thanks. The overriding jansi.tmpdir worked for my case.
Code:
% /usr/local/bin/java -jar -Djava.awt.headless=true -Djansi.tmpdir=/usr/home/username/kol/tmp KoLmafia.jar --CLI

@heeheehee r20703 didn't work. It appears the change is only trapping Exceptions, the failure in this case is an Error, specifically java.lang.ExceptionInInitializerError.

Code:
% /usr/local/bin/java -jar -Djava.awt.headless=true KoLmafia.jar --CLI

KoLmafia v20.7 r20703
Released on July 30, 2020

Currently Running on FreeBSD
Local Directory is /usr/home/username/.kolmafia
Using Java 1.8.0_275

Failed to load native library:jansi-2.3.2-bd4cb74a2fa77e2d-libjansi.so. osinfo: FreeBSD/x86_64
java.lang.UnsatisfiedLinkError: /tmp/jansi-2.3.2-bd4cb74a2fa77e2d-libjansi.so: /tmp/jansi-2.3.2-bd4cb74a2fa77e2d-libjansi.so: mmap of data failed: Permission denied
Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.fusesource.jansi.AnsiConsole.ansiStream(AnsiConsole.java:239)
        at org.fusesource.jansi.AnsiConsole.initStreams(AnsiConsole.java:542)
        at org.fusesource.jansi.AnsiConsole.systemInstall(AnsiConsole.java:496)
        at net.sourceforge.kolmafia.KoLmafiaTUI.initialize(KoLmafiaTUI.java:24)
        at net.sourceforge.kolmafia.KoLmafia.main(KoLmafia.java:389)
Caused by: java.lang.RuntimeException: Unable to load jansi native library
        at org.fusesource.jansi.internal.JansiLoader.initialize(JansiLoader.java:62)
        at org.fusesource.jansi.internal.CLibrary.<clinit>(CLibrary.java:30)
        ... 5 more
Caused by: java.lang.Exception: No native library found for os.name=FreeBSD, os.arch=x86_64, paths=[/org/fusesource/jansi/internal/native/FreeBSD/x86_64:/usr/local/lib]
        at org.fusesource.jansi.internal.JansiLoader.loadJansiNativeLibrary(JansiLoader.java:333)
        at org.fusesource.jansi.internal.JansiLoader.initialize(JansiLoader.java:60)
 

heeheehee

Developer
Staff member
sigh, why does Java need Errors to be separate from Exceptions...

catch (Throwable t) seems like a bad idea, so I guess I'll just catch linkage errors (of which ExceptionInInitializerError is a subclass)...

Try r20704.
 

blueflamepants

New member
@heeheehee r20704 is able to continue when AnsiConsole fails to initialize. Thanks.

That said it seems OpenJDK outputs diagnostic information about the linkage error, but I can live with that. Actually I can live with overriding Jansi's tmpdir, which is a better solution for my purposes.
Code:
 % /usr/local/bin/java -jar -Djava.awt.headless=true KoLmafia.jar --CLI

KoLmafia v20.7 r20704
Released on July 30, 2020

Currently Running on FreeBSD
Local Directory is /usr/home/username/.kolmafia
Using Java 1.8.0_275

Failed to load native library:jansi-2.3.2-a22a79d2926fe1ee-libjansi.so. osinfo: FreeBSD/x86_64
java.lang.UnsatisfiedLinkError: /tmp/jansi-2.3.2-a22a79d2926fe1ee-libjansi.so: /tmp/jansi-2.3.2-a22a79d2926fe1ee-libjansi.so: mmap of data failed: Permission denied

username:
 

Obeliks

Member
Unrelated to the errors, but related to the color terminal, here is a small script to test whether KoLmafia and/or your terminal are outputting the colors you expected:

JavaScript:
const kolmafia = require('kolmafia');

let s = '';

function hex(n) {
  return ('0' + parseInt(n).toString(16)).slice(-2);
}

for (colnum = 0; colnum<77; colnum++) {
    r = 255-(colnum*255/76);
    g = (colnum*510/76);
    b = (colnum*255/76);
    if (g>255) g = 510-g;

    s += '<font color="#' + hex(r) + hex(g) + hex(b) + '">█</font>';
}

kolmafia.printHtml(s);

This should print a nice gradient like so:

1619381674294.png

If it doesn't, try to start KoLmafia with -Djansi.colors=truecolor flag. If that doesn't help, you might need to adjust the settings of your terminal and/or multiplexer.

The following settings help for tmux, put this in ~/.tmux.conf:
Code:
set -g default-terminal "tmux-256color"
set -ga terminal-overrides ",*256col*:Tc"
 

Obeliks

Member
My screenshot is from Windows Terminal as well, albeit with WSL. But the program itself is definitely not limited to 256 color mode.

@philmasterplus, would you mind sharing your js snippet as copiable text, that would be super helpful :)
 

philmasterplus

Active member
JavaScript:
const hex = n => ('0' + parseInt(n).toString(16)).slice(-2); printHtml(new Array(77).fill().map((_, i) => (g = hex(255-(i*255/76)), '<font color="#' + (g > 255 ? 510 - g : g) + hex(i*510/76) + hex(i*255/76) + '">@</font>')).join(''));
 
Top