...
 
Commits (3)
......@@ -383,11 +383,15 @@ be entered instead of expressions:
copy Copies the current paper to copy, cp, y
the clipboard by default. Accepts
a type and a range.
new Creates a new paper. new, :new
next Switches to the next paper in next, right,
the list, if there is any. :bnext, :bn
note Allows to edit the current note. note
Accepts the operation and parameters.
open Opens a paper from the given path. open, :e
The path to open the files is expected
as parameter to this command. If
......@@ -434,17 +438,17 @@ provided only the set parts of the given lines will be copied.
The types available are:
Part
Part Description Aliases
-----------------------------------------------------------------------
expression The expression of the selected lines. exp
expression The expression of the selected lines. expression, exp
id The ID of the selected lines.
id The ID of the selected lines. id
line The whole lines.
line The whole lines. line
paper The whole paper.
paper The whole paper. paper
result The result of the selected lines. res
result The result of the selected lines. result, res
The range can either be nothing (for all), a single ID or index (starting at 1),
a comma separated list of ID's or indexes or a range specified with two dots.
......@@ -454,6 +458,36 @@ a comma separated list of ID's or indexes or a range specified with two dots.
copy expression #3,abc,#7
copy result 3..abc
#### note
The note command accepts additional parameters, namely the action and
the associated parameters.
note action [index] line
The actions are:
Action Description Aliases
-----------------------------------------------------------------------
add Adds the given value as separate line add, append
to the notes.
clear Clears the whole note. clear, clr, cls,
reset
delete Deletes the line with the given 1-based delete, del,
index. remove, rem
insert Inserts the line at the given 1-based insert, ins
index.
Some examples.
note add New line.
note insert 1 Inserted before the first line.
note delete 3
note clear
### Options
There are various options available to change how jMathPaper behaves, they can
......
......@@ -210,6 +210,20 @@ public abstract class AbstractPapersUi implements Ui {
new_();
break;
case NOTE:
if (parameters != null && parameters.length > 0) {
NoteAction noteAction = NoteAction.getNoteAction(parameters[0]);
if (noteAction == null) {
noteAction = NoteAction.ADD;
}
editNote(noteAction, Arrays.copyOfRange(parameters, 1, parameters.length));
} else {
throw new CommandExecutionException("No parameters given: add/clear/delete/insert parameters");
}
break;
case OPEN:
for (String parameter : parameters) {
open(Paths.get(parameter));
......@@ -620,13 +634,20 @@ public abstract class AbstractPapersUi implements Ui {
return paper;
}
/**
* Invoked whenever the current {@link Paper} has been added, meaning that
* is just has been added to this UI.
* <p>
* Overriding classes can safely assume that there is a current
* {@link Paper}.
*/
protected void currentPaperHasBeenAdded() {
// For extending classes.
}
/**
* Invoked whenever the current {@link Paper} has been modified,meaning that
* one or more additional expressions has been added.
* Invoked whenever the current {@link Paper} has been modified, meaning
* that one or more additional expressions has been added.
* <p>
* Overriding classes can safely assume that there is a current
* {@link Paper}.
......@@ -668,6 +689,61 @@ public abstract class AbstractPapersUi implements Ui {
// For extending classes.
}
/**
* Edits the note of the current {@link Paper}.
*
* @param noteAction The {@link NoteAction} to perform on the current note.
* @param parameters The parameters to use.
*/
protected void editNote(NoteAction noteAction, String[] parameters) {
checkCurrentPaper();
switch (noteAction) {
case ADD:
paper.setNotes(paper.getNotes() + String.join(" ", parameters) + "\n");
break;
case CLEAR:
paper.setNotes("");
break;
case DELETE:
case INSERT:
if (parameters.length <= 0) {
throw new IllegalArgumentException("Expected an index, got nothing.");
}
int index = -1;
try {
index = Integer.parseInt(parameters[0]) - 1;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Expected an index, got: " + parameters[0], e);
}
List<String> noteLines = new ArrayList<>(Arrays.asList(paper.getNotes().split("\\n")));
if (index < 0 || index >= noteLines.size()) {
throw new IllegalArgumentException("Given index is out of range.");
}
switch (noteAction) {
case DELETE:
noteLines.remove(index);
break;
case INSERT:
noteLines.add(index, String.join(" ", Arrays.copyOfRange(parameters, 1, parameters.length)));
break;
}
paper.setNotes(String.join("\n", noteLines) + "\n");
break;
}
currentPaperHasBeenModified();
}
/**
* A helper method which gets the appropriate Enum value from the given
* class and name.
......
......@@ -47,6 +47,9 @@ public enum Command {
/** Switches to the next paper, if there is any. */
NEXT("next", "right", ":bnext", ":bn"),
/** Edits the note. */
NOTE("note"),
/** Opens the given paper. */
OPEN("open", ":e"),
......
/*
* Copyright 2018, Robert 'Bobby' Zenz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.bonsaimind.jmathpaper.core.ui;
/**
* Defines an action being taken on a note.
*/
public enum NoteAction {
/** Adds a line to the end of the note. */
ADD("add", "append"),
/** Clears the whole note. */
CLEAR("clear", "clr", "cls", "reset"),
/** Deletes the line with the given index. */
DELETE("delete", "del", "remove", "rem"),
/** Inserts a line at the given index. */
INSERT("insert", "ins");
private String[] aliases = null;
private NoteAction(String... aliases) {
this.aliases = aliases;
}
public static NoteAction getNoteAction(String name) {
if (name == null || name.length() == 0) {
return null;
}
for (NoteAction paperPart : values()) {
for (String alias : paperPart.aliases) {
if (name.equalsIgnoreCase(alias)) {
return paperPart;
}
}
}
return null;
}
}
......@@ -24,19 +24,19 @@ package org.bonsaimind.jmathpaper.core.ui;
*/
public enum PaperPart {
/** The expression of a line/expression. */
EXPRESSION("exp"),
EXPRESSION("expression", "exp"),
/** The ID of a line/expression. */
ID(),
ID("id"),
/** The whole line-expression. */
LINE(),
LINE("line"),
/** The whole paper. */
PAPER(),
PAPER("paper"),
/** The result of a line/expression. */
RESULT("res");
RESULT("result", "res");
private String[] aliases = null;
......@@ -50,10 +50,6 @@ public enum PaperPart {
}
for (PaperPart paperPart : values()) {
if (paperPart.name().equalsIgnoreCase(name)) {
return paperPart;
}
for (String alias : paperPart.aliases) {
if (name.equalsIgnoreCase(alias)) {
return paperPart;
......
......@@ -135,6 +135,24 @@ public class TestAbstractPapersUi extends AbstractPapersUi {
assertClipboard("1+1\n2+2\n3+3\n4+4\n5+5");
}
@Test
public void testCommandNote() throws CommandExecutionException, InvalidExpressionException {
process("note add 1");
process("note add 2");
process("note add 3");
process("note add 4");
assertNote("1\n2\n3\n4\n");
process("note insert 1 First");
assertNote("First\n1\n2\n3\n4\n");
process("note delete 3");
assertNote("First\n1\n3\n4\n");
process("note clear");
assertNote("");
}
@Test
public void testCommandQuit() throws CommandExecutionException, InvalidExpressionException {
process("quit");
......@@ -272,6 +290,10 @@ public class TestAbstractPapersUi extends AbstractPapersUi {
}
}
private final void assertNote(String expected) {
Assert.assertEquals(expected, paper.getNotes());
}
private final void assertSplitParameters(String[] expected, String input) {
assertLists(expected, splitParameters(input));
}
......