| 1 | package be.nikiroo.utils.main; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | import java.util.Scanner; |
| 6 | |
| 7 | import be.nikiroo.utils.StringUtils; |
| 8 | import be.nikiroo.utils.StringUtils.Alignment; |
| 9 | |
| 10 | /** |
| 11 | * Text justification (left, right, center, justify). |
| 12 | * |
| 13 | * @author niki |
| 14 | */ |
| 15 | public class justify { |
| 16 | /** |
| 17 | * Syntax: $0 ([left|right|center|justify]) (max width) |
| 18 | * <p> |
| 19 | * <ul> |
| 20 | * <li>mode: left, right, center or full justification (defaults to left)</li> |
| 21 | * <li>max width: the maximum width of a line, or "" for "no maximum" |
| 22 | * (defaults to "no maximum")</li> |
| 23 | * </ul> |
| 24 | * |
| 25 | * @param args |
| 26 | */ |
| 27 | public static void main(String[] args) { |
| 28 | int width = -1; |
| 29 | StringUtils.Alignment align = Alignment.LEFT; |
| 30 | |
| 31 | if (args.length >= 1) { |
| 32 | align = Alignment.valueOf(args[0].toUpperCase()); |
| 33 | } |
| 34 | if (args.length >= 2) { |
| 35 | width = Integer.parseInt(args[1]); |
| 36 | } |
| 37 | |
| 38 | Scanner scan = new Scanner(System.in); |
| 39 | scan.useDelimiter("\r\n|[\r\n]"); |
| 40 | try { |
| 41 | List<String> lines = new ArrayList<String>(); |
| 42 | while (scan.hasNext()) { |
| 43 | lines.add(scan.next()); |
| 44 | } |
| 45 | |
| 46 | for (String line : StringUtils.justifyText(lines, width, align)) { |
| 47 | System.out.println(line); |
| 48 | } |
| 49 | } finally { |
| 50 | scan.close(); |
| 51 | } |
| 52 | } |
| 53 | } |