Skip to content

Commit

Permalink
iterate on JLine dependencies and prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
dfa1 committed Dec 21, 2023
1 parent 8d4ed2a commit f2f359a
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 2 deletions.
5 changes: 5 additions & 0 deletions main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>hosh</groupId>
<artifactId>hosh-runtime</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>jline-reader</artifactId>
<version>${version.jline}</version>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-style</artifactId>
<version>${version.jline}</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package hosh.runtime.prompt;

import hosh.doc.Todo;
import hosh.spi.Ansi;
import hosh.spi.State;

Expand All @@ -31,6 +32,7 @@

public class DefaultPromptProvider implements PromptProvider {

@Todo(description = "use StyledPrompt class")
@Override
public String provide(State state) {
StringWriter sw = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package hosh.runtime.prompt;

import hosh.doc.Todo;
import hosh.spi.Ansi;
import hosh.spi.LoggerFactory;
import hosh.spi.State;
Expand Down Expand Up @@ -61,6 +62,7 @@ public void setGitExternal(GitExternal gitExternal) {
this.gitExternal = gitExternal;
}

@Todo(description = "use StyledPrompt class")
@Override
public String provide(State state) {
String currentBranch = getCurrentBranch(state);
Expand Down
39 changes: 39 additions & 0 deletions runtime/src/main/java/hosh/runtime/prompt/StyledPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.runtime.prompt;

import hosh.doc.Experimental;
import org.jline.style.StyleExpression;
import org.jline.utils.AttributedString;

@Experimental(description = "next gen prompt")
public class StyledPrompt {

public String render(String template) {
StyleExpression sut = new StyleExpression();
AttributedString result = sut.evaluate(template);
return result.toAnsi();
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.runtime.prompt;

import hosh.spi.State;
Expand Down
68 changes: 68 additions & 0 deletions runtime/src/test/java/hosh/runtime/prompt/StyledPromptTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.runtime.prompt;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class StyledPromptTest {

@Test
void noStyle() {
// Given
StyledPrompt sut = new StyledPrompt();

// When
String result = sut.render("hosh> ");

// Then
assertThat(result).isEqualTo("hosh> ");
}

@Test
void oneStyle() {
// Given
StyledPrompt sut = new StyledPrompt();

// When
String result = sut.render("@{fg:green user}> ");

// Then
assertThat(result).isEqualTo("\u001B[32muser\u001B[0m> ");
}

@Test
void twoStyles() {
// Given
StyledPrompt sut = new StyledPrompt();

// When
String result = sut.render("@{fg:green user}@@{fg:red server}> ");

// Then
assertThat(result).isEqualTo("\u001B[32muser\u001B[0m@\u001B[31mserver\u001B[0m> ");
}

}
3 changes: 1 addition & 2 deletions spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
<scope>runtime</scope>
<artifactId>jline-style</artifactId>
</dependency>
<!-- test classpath -->
<dependency>
Expand Down
1 change: 1 addition & 0 deletions spi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

requires transitive org.jline.reader;
requires transitive org.jline.terminal;
requires transitive org.jline.style;

exports hosh.spi;
exports hosh.doc;
Expand Down

0 comments on commit f2f359a

Please sign in to comment.