Skip to content

Commit

Permalink
[MNG-6915] Add a helper method to get the terminal width
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 16, 2021
1 parent 81a813b commit f94bb25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Expand Up @@ -201,4 +201,22 @@ public void run()
Runtime.getRuntime().addShutdownHook( shutdownHook );
}
}

/**
* Get the terminal width or -1 if the width cannot be determined.
*
* @return the terminal width
*/
public static int getTerminalWidth()
{
if ( JANSI )
{
int width = AnsiConsole.getTerminalWidth();
return width > 0 ? width : -1;
}
else
{
return -1;
}
}
}
Expand Up @@ -22,9 +22,18 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import org.fusesource.jansi.AnsiColors;
import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.AnsiMode;
import org.fusesource.jansi.AnsiPrintStream;
import org.fusesource.jansi.AnsiType;
import org.fusesource.jansi.io.AnsiOutputStream;
import org.junit.Test;

public class MessageUtilsTest
Expand All @@ -45,4 +54,24 @@ public void testSystem()
System.setOut( currentOut );
}
}

@Test
public void testTerminalWidth()
{
AnsiOutputStream.WidthSupplier width = new AnsiOutputStream.WidthSupplier()
{
@Override
public int getTerminalWidth()
{
return 33;
}
};
AnsiOutputStream aos = new AnsiOutputStream( new ByteArrayOutputStream(), width, AnsiMode.Default,
null, AnsiType.Emulation, AnsiColors.Colors256, StandardCharsets.UTF_8,
null, null, false );
AnsiConsole.systemInstall();
AnsiConsole.out = new AnsiPrintStream( aos, true );
assertEquals( 33, MessageUtils.getTerminalWidth() );
AnsiConsole.systemUninstall();
}
}

0 comments on commit f94bb25

Please sign in to comment.