diff options
| author | Ben Culkin <scorpress@gmail.com> | 2023-06-23 19:48:49 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2023-06-23 19:48:49 -0400 |
| commit | 29db46041115065559a0d42f9a76ebb7c19424ea (patch) | |
| tree | 0bb2bd0d2ff6cb56302dba86e6d344653f047298 | |
| parent | 2df02c35b70f7e8077832470de9594b657f1be67 (diff) | |
Misc Updates
8 files changed, 163 insertions, 7 deletions
diff --git a/base/src/examples/java/bjc/utils/examples/cli/package-info.java b/base/src/examples/java/bjc/utils/examples/cli/package-info.java new file mode 100644 index 0000000..29ca4a4 --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/cli/package-info.java @@ -0,0 +1 @@ +package bjc.utils.examples.cli;
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/graph/AlgGraph.java b/base/src/main/java/bjc/utils/graph/AlgGraph.java index 7c9d38d..96a984f 100644 --- a/base/src/main/java/bjc/utils/graph/AlgGraph.java +++ b/base/src/main/java/bjc/utils/graph/AlgGraph.java @@ -1,6 +1,6 @@ package bjc.utils.graph; -import bjc.functypes.Container; +import bjc.typeclasses.Container; /** * A directed algebraic graph diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/MLPromptBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/MLPromptBlockReader.java new file mode 100644 index 0000000..981c5aa --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/blocks/MLPromptBlockReader.java @@ -0,0 +1,102 @@ +package bjc.utils.ioutils.blocks; + +import java.io.*; +import java.util.Scanner; + +/** + * A block reader that uses bash-style multi-line prompts to read blocks. + * + * The format of the blocks returned by this is indented blocks <code> x y z a b + * + * c -- block split here d <code> + * + * @author bjcul + * + */ +public class MLPromptBlockReader implements BlockReader { + private String firstPrompt; + private String secondPrompt; + + private Scanner input; + private Writer output; + + private Block currBlock; + + /* Info about the current block. */ + private int blockNo = 0; + private int lineNo = 1; + + private String nextInitLine; + /** + * Create a new multi-line prompt block reader. + * + * @param firstPrompt The initial prompt. Is treated as a format string, with + * %1 being the block number and %2 being the line number + * @param secondPrompt The secondary prompt. Is also treated as a format string + * with %1 and %2 as firstPrompt, and %3 as the continuation line + * number + * + * @param input The input source + * @param output The output destination + */ + public MLPromptBlockReader(String firstPrompt, String secondPrompt, Reader input, Writer output) { + this.firstPrompt = firstPrompt; + this.secondPrompt = secondPrompt; + this.input = new Scanner(input); + this.output = output; + } + + @Override + public boolean hasNextBlock() { + return input.hasNextLine(); + } + + @Override + public Block getBlock() { + return currBlock; + } + + @Override + public boolean nextBlock() { + StringBuilder blockContents = new StringBuilder(); + + String currLn; + if (nextInitLine == null) { + try { + output.write(String.format(firstPrompt, blockNo, lineNo)); + } catch (IOException ioex) { + throw new RuntimeException(ioex); + } + currLn = input.nextLine(); + } else { + currLn = nextInitLine; + } + + blockContents.append(currLn); + + int startLn = lineNo; + int endLn = lineNo++; + + boolean inBlock = true; + while(input.hasNextLine() && inBlock) { + currLn = input.nextLine(); + + int initCh = currLn.codePointAt(0); + inBlock = Character.isSpaceChar(initCh); + } + + currBlock = new Block(blockNo++, blockContents.toString(), startLn, endLn); + return true; + } + + @Override + public int getBlockCount() { + return blockNo; + } + + @Override + public void close() throws IOException { + input.close(); + } + +} diff --git a/base/src/test/java/bjc/utils/test/ioutils/blocks/MLPromptBlockReaderTest.java b/base/src/test/java/bjc/utils/test/ioutils/blocks/MLPromptBlockReaderTest.java new file mode 100644 index 0000000..599d5a4 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/ioutils/blocks/MLPromptBlockReaderTest.java @@ -0,0 +1,22 @@ +package bjc.utils.test.ioutils.blocks; + +import static org.junit.Assert.*; + +import java.io.StringReader; +import java.io.StringWriter; + +import org.junit.Test; + +import bjc.utils.ioutils.blocks.MLPromptBlockReader; + +public class MLPromptBlockReaderTest { + + @Test + public void test() { + StringReader reader = new StringReader(""); + StringWriter writer = new StringWriter(); + + MLPromptBlockReader blocker = new MLPromptBlockReader("%1:%2> ", "%3... ", reader, writer); + } + +} diff --git a/clformat/data/clformat.sprop b/clformat/data/clformat.sprop index 1d31de6..dd4847b 100644 --- a/clformat/data/clformat.sprop +++ b/clformat/data/clformat.sprop @@ -6,7 +6,7 @@ ## Has two parts ## 1) The optional set of prefix parameters ## 2) The optional modifier -## Captures three things +## Captures three or four things ## 1) The prefix parameters ## 2) The modifiers ## 3) The directive name diff --git a/clformat/readme.md b/clformat/readme.md index da0e085..ffb3f53 100644 --- a/clformat/readme.md +++ b/clformat/readme.md @@ -15,7 +15,7 @@ directive is always started with a ~, and consists of the following parts. ### Prefix parameters Prefix parameters are used to configure options for a directive. These include -things the number of columns to print a field in, or what radix to print a +things like the number of columns to print a field in, or what radix to print a number in. They are separated from each other by commas. A prefix parameter can be one of the following: @@ -102,6 +102,7 @@ implemented, as well as a short description of what that directive does | ( | Case | Perform case-manipulation on text | | \`[ | Inflection | Perform inflection on a format string | | T | Tabulate | Print something in a table-like format | +| \`D | General Decimal | Print numbers using a Java NumberFormat | The following are directives that are not valid to use outside of specific other directives diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java index 61d88bf..7d8a67a 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java @@ -1,7 +1,10 @@ package bjc.utils.ioutils.format; import java.util.*; +import java.util.function.Function; +import bjc.data.Pair; +import bjc.data.TransformIterator; import bjc.esodata.*; import bjc.utils.parserutils.TokenUtils; @@ -28,6 +31,8 @@ public class CLParameters { private Map<String, CLValue> namedParams; private Map<String, Integer> nameIndices; + private Set<String> nonNumberedParams; + /** * Create a new set of blank CL format parameters. */ @@ -69,6 +74,9 @@ public class CLParameters { this.namedParams = namedParams; this.nameIndices = new HashMap<>(); + this.nonNumberedParams = new HashSet<>(); + + nonNumberedParams.addAll(namedParams.keySet()); abbrevWords = new HashSet<>(); nameAbbrevs = new AbbrevMap2(); @@ -135,7 +143,8 @@ public class CLParameters { } nameIndices.put(opt.toUpperCase(), idx); - + nonNumberedParams.remove(opt); + if (doRefresh) refreshAbbrevs(); } @@ -175,7 +184,8 @@ public class CLParameters { */ public static CLParameters fromDirective(String unsplit) { List<String> lParams = new ArrayList<>(); - + Set<String> nonIndexParams = new HashSet<>(); + StringBuilder currParm = new StringBuilder(); char prevChar = ' '; @@ -246,12 +256,16 @@ public class CLParameters { namedParams.put(paramName.toUpperCase(), actVal); if (setIndex) parameters.add(actVal); + else nonIndexParams.add(paramName); } else { parameters.add(parseParam(param)); } } - return new CLParameters(parameters.toArray(new CLValue[0]), namedParams); + CLParameters retVal = new CLParameters(parameters.toArray(new CLValue[0]), namedParams); + retVal.nonNumberedParams = nonIndexParams; + + return retVal; } // Actually parse the value for a parameter @@ -556,4 +570,15 @@ public class CLParameters { return sb.toString(); } + + /** + * Get an iterator over all of the named parameters not bound to an index. + * + * @return The described iterator + */ + public Iterator<Pair<String, CLValue>> getNonNumberedParams() { + return new TransformIterator<>(nonNumberedParams.iterator(), (val) -> { + return Pair.pair(val, namedParams.get(val)); + }); + } } diff --git a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java index 250e52c..94bc5a1 100644 --- a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java +++ b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java @@ -28,7 +28,7 @@ public class CLFormatterTest { } @Test - public void testDecimalPrinting() { + public void testNumberPrinting() { // Test decimal printing assertEquals("5", format("~D", 5)); assertEquals(" 5", format("~3D", 5)); @@ -37,6 +37,11 @@ public class CLFormatterTest { } @Test + public void testDecimalPrinting() { + assertFormat("5.5", "~`D", 5.5); + } + + @Test public void testRadixPrinting() { // Test radix printing assertEquals("1 22", format("~3,,,' ,2:R", 17)); |
