Skip to content

Commit

Permalink
Merge pull request #70 from gnodet/MSHARED-965-help
Browse files Browse the repository at this point in the history
[MNG-6915] Add a helper method to get the terminal width
  • Loading branch information
elharo authored Apr 16, 2021
2 parents eb4f635 + f94bb25 commit b4577c9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,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;
}
}
}
Original file line number Diff line number Diff line change
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 b4577c9

Please sign in to comment.