Search code examples
javajansi

Jansi keeps incrementing indentation


Increasing indentation for subsequent lines

I can't seem to get rid of previously printed lines. If I do just

AnsiConsole.out.println(ansi);

It repeats the same output every successive output. E.g. A AB ABC

Even if the output is only supposed to be

A B C

My code is:

AnsiConsole.out.println(a);
a.eraseLine(Erase.ALL);
AnsiConsole.out.flush;

If I don't use a.eraseLine(Erase.ALL), it looks like this:

Also, if I don't use Jansi and use plain System.out, it looks like this so I know it's not a matter of code elsewhere.

Solution: Thanks to Betlista's answer. I found out what was wrong. The thing was that my ansi variable was actually called using the Ansi constructor and not the Ansi.ansi() method. That was what was different in my code. Thanks. :)


Solution

  • It seems you are doing something wrong, but there is a lot of info missing in your post, that it is hard to follow...

    ...so I'll write how it works for me:

    I'm using maven with jansi in version 1.11 tested on Windows 7.

    I simply followed the info on https://github.com/fusesource/jansi and my code

    package jansi;
    
    import java.io.IOException;
    
    import org.fusesource.jansi.Ansi;
    import org.fusesource.jansi.Ansi.Color;
    import org.fusesource.jansi.AnsiConsole;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            AnsiConsole.systemInstall();
    
            String[] sa = {"A", "B", "C"};
            Color[] ca = {Color.RED, Color.GREEN, Color.BLUE};
            for (int i = 0; i < sa.length; ++i ) {
                System.out.println(Ansi.ansi().eraseLine().fg(ca[i]).a(sa[i]).reset());
            }
        }
    }
    

    is doing exactly what I expected and this is the result:

    enter image description here