Skip to content

[MNG-6915] Add a helper method to get the terminal width #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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;
}
}
}
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();
}
}