Support a columns count parameter (-c) in directory mode

This commit is contained in:
Stefan Haustein 2016-10-01 13:13:03 +02:00
parent 4990ab5383
commit 2e96aaec28

View file

@ -28,15 +28,17 @@ public class TerminalImageViewer {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
if (args.length == 0) { if (args.length == 0) {
System.out.println( System.out.println(
"Image file name required.\n\n - Use -w and -h to set the maximum width and height in characters" + "Image file name required.\n\n" +
" (defaults: 80, 24).\n - Use -256 for 256 color mode, -grayscale for grayscale and -stdin to" + " - Use -w and -h to set the maximum width and height in characters (defaults: 80, 24).\n" +
" obtain file names from stdin.\n"); " - Use -256 for 256 color mode, -grayscale for grayscale and -stdin to obtain file names from stdin.\n" +
" - When multiple files are supplied, -c sets the number of images per row (default: 4).");
return; return;
} }
int start = 0; int start = 0;
int maxWidth = 80; int maxWidth = 80;
int maxHeight = 24; int maxHeight = 24;
int columns = 4;
boolean stdin = false; boolean stdin = false;
while (start < args.length && args[start].startsWith("-")) { while (start < args.length && args[start].startsWith("-")) {
String option = args[start]; String option = args[start];
@ -44,6 +46,8 @@ public class TerminalImageViewer {
maxWidth = Integer.parseInt(args[++start]); maxWidth = Integer.parseInt(args[++start]);
} else if (option.equals("-h") && args.length > start + 1) { } else if (option.equals("-h") && args.length > start + 1) {
maxHeight = Integer.parseInt(args[++start]); maxHeight = Integer.parseInt(args[++start]);
} else if (option.equals("-c") && args.length > start + 1) {
columns = Integer.parseInt(args[++start]);
} else if (option.equals("-256")) { } else if (option.equals("-256")) {
mode = (mode & ~Ansi.MODE_24BIT) | Ansi.MODE_256; mode = (mode & ~Ansi.MODE_24BIT) | Ansi.MODE_256;
} else if (option.equals("-grayscale")) { } else if (option.equals("-grayscale")) {
@ -71,15 +75,15 @@ public class TerminalImageViewer {
} else { } else {
// Directory-style rendering. // Directory-style rendering.
int index = 0; int index = 0;
int cw = (maxWidth - 2 * 3 * 4) / 16; int cw = (maxWidth - 2 * (columns - 1) * 4) / (4 * columns);
int tw = cw * 4; int tw = cw * 4;
while (index < args.length) { while (index < args.length) {
BufferedImage image = new BufferedImage(tw * 4 + 24, tw, grayscale ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(tw * columns + 24, tw, grayscale ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics(); Graphics2D graphics = image.createGraphics();
int count = 0; int count = 0;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
while (index < args.length && count < 4) { while (index < args.length && count < columns) {
String name = args[index++]; String name = args[index++];
try { try {
BufferedImage original = loadImage(name); BufferedImage original = loadImage(name);