Skip to content

Basic UI components #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<jline.version>3.21.0</jline.version>
<jcommander.version>1.81</jcommander.version>
<antlr-st4.version>4.3.1</antlr-st4.version>
<jimfs.version>1.2</jimfs.version>
</properties>

<modules>
Expand Down Expand Up @@ -110,6 +111,11 @@
<artifactId>ST4</artifactId>
<version>${antlr-st4.version}</version>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
11 changes: 10 additions & 1 deletion spring-shell-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.component;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

import org.springframework.shell.component.context.ComponentContext;
import org.springframework.shell.component.support.AbstractSelectorComponent;
import org.springframework.shell.component.support.Enableable;
import org.springframework.shell.component.support.Itemable;
import org.springframework.shell.component.support.Matchable;
import org.springframework.shell.component.support.Nameable;
import org.springframework.shell.component.support.AbstractSelectorComponent.SelectorComponentContext;
import org.springframework.shell.component.MultiItemSelector.MultiItemSelectorContext;

/**
* Component able to pick multiple items.
*
* @author Janne Valkealahti
*/
public class MultiItemSelector<T, I extends Nameable & Matchable & Enableable & Itemable<T>>
extends AbstractSelectorComponent<T, MultiItemSelectorContext<T, I>, I> {

private MultiItemSelectorContext<T, I> currentContext;

public MultiItemSelector(Terminal terminal, List<I> items, String name, Comparator<I> comparator) {
super(terminal, name, items, false, comparator);
setRenderer(new DefaultRenderer());
setTemplateLocation("classpath:org/springframework/shell/component/multi-item-selector-default.stg");
}

@Override
protected MultiItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) {
if (context != null && currentContext == context) {
return currentContext;
}
currentContext = MultiItemSelectorContext.empty(getItemMapper());
currentContext.setName(name);
if (currentContext.getItems() == null) {
currentContext.setItems(getItems());
}
context.stream().forEach(e -> {
currentContext.put(e.getKey(), e.getValue());
});
return currentContext;
}

@Override
protected MultiItemSelectorContext<T, I> runInternal(MultiItemSelectorContext<T, I> context) {
super.runInternal(context);
loop(context);
return context;
}

/**
* Context {@link MultiItemSelector}.
*/
public interface MultiItemSelectorContext<T, I extends Nameable & Matchable & Itemable<T>>
extends SelectorComponentContext<T, I, MultiItemSelectorContext<T, I>> {

/**
* Gets a values.
*
* @return a values
*/
List<String> getValues();

/**
* Creates an empty {@link MultiItemSelectorContext}.
*
* @return empty context
*/
static <T, I extends Nameable & Matchable & Itemable<T>> MultiItemSelectorContext<T, I> empty() {
return new DefaultMultiItemSelectorContext<>();
}

/**
* Creates an {@link MultiItemSelectorContext}.
*
* @return context
*/
static <T, I extends Nameable & Matchable & Itemable<T>> MultiItemSelectorContext<T, I> empty(Function<T, String> itemMapper) {
return new DefaultMultiItemSelectorContext<>(itemMapper);
}
}

private static class DefaultMultiItemSelectorContext<T, I extends Nameable & Matchable & Itemable<T>> extends
BaseSelectorComponentContext<T, I, MultiItemSelectorContext<T, I>> implements MultiItemSelectorContext<T, I> {

private Function<T, String> itemMapper = item -> item.toString();

DefaultMultiItemSelectorContext() {
}

DefaultMultiItemSelectorContext(Function<T, String> itemMapper) {
this.itemMapper = itemMapper;
}

@Override
public List<String> getValues() {
if (getResultItems() == null) {
return Collections.emptyList();
}
return getResultItems().stream()
.map(i -> i.getItem())
.map(i -> itemMapper.apply(i))
.collect(Collectors.toList());
}

@Override
public Map<String, Object> toTemplateModel() {
Map<String, Object> attributes = super.toTemplateModel();
attributes.put("values", getValues());
List<Map<String, Object>> rows = getItemStateView().stream()
.map(is -> {
Map<String, Object> map = new HashMap<>();
map.put("name", is.getName());
map.put("selected", is.isSelected());
map.put("onrow", getCursorRow().intValue() == is.getIndex());
map.put("enabled", is.isEnabled());
return map;
})
.collect(Collectors.toList());
attributes.put("rows", rows);
// finally wrap it into 'model' as that's what
// we expect in stg template.
Map<String, Object> model = new HashMap<>();
model.put("model", attributes);
return model;
}
}

private class DefaultRenderer implements Function<MultiItemSelectorContext<T, I>, List<AttributedString>> {

@Override
public List<AttributedString> apply(MultiItemSelectorContext<T, I> context) {
return renderTemplateResource(context.toTemplateModel());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

import org.springframework.shell.component.PathInput.PathInputContext;
import org.springframework.shell.component.context.ComponentContext;
import org.springframework.shell.component.support.AbstractTextComponent;
import org.springframework.shell.component.support.AbstractTextComponent.TextComponentContext;
import org.springframework.shell.component.support.AbstractTextComponent.TextComponentContext.MessageLevel;
import org.springframework.util.StringUtils;;

/**
* Component for a simple path input.
*
* @author Janne Valkealahti
*/
public class PathInput extends AbstractTextComponent<Path, PathInputContext> {

private PathInputContext currentContext;
private Function<String, Path> pathProvider = (path) -> Paths.get(path);

public PathInput(Terminal terminal) {
this(terminal, null);
}

public PathInput(Terminal terminal, String name) {
this(terminal, name, null);
}

public PathInput(Terminal terminal, String name, Function<PathInputContext, List<AttributedString>> renderer) {
super(terminal, name, null);
setRenderer(renderer != null ? renderer : new DefaultRenderer());
setTemplateLocation("classpath:org/springframework/shell/component/path-input-default.stg");
}

@Override
protected PathInputContext getThisContext(ComponentContext<?> context) {
if (context != null && currentContext == context) {
return currentContext;
}
currentContext = PathInputContext.empty();
currentContext.setName(getName());
context.stream().forEach(e -> {
currentContext.put(e.getKey(), e.getValue());
});
return currentContext;
}

@Override
protected boolean read(BindingReader bindingReader, KeyMap<String> keyMap, PathInputContext context) {
String operation = bindingReader.readBinding(keyMap);
String input;
switch (operation) {
case OPERATION_CHAR:
String lastBinding = bindingReader.getLastBinding();
input = context.getInput();
if (input == null) {
input = lastBinding;
}
else {
input = input + lastBinding;
}
context.setInput(input);
checkPath(input, context);
break;
case OPERATION_BACKSPACE:
input = context.getInput();
if (StringUtils.hasLength(input)) {
input = input.length() > 1 ? input.substring(0, input.length() - 1) : null;
}
context.setInput(input);
checkPath(input, context);
break;
case OPERATION_EXIT:
if (StringUtils.hasText(context.getInput())) {
context.setResultValue(Paths.get(context.getInput()));
}
return true;
default:
break;
}
return false;
}

/**
* Sets a path provider.
*
* @param pathProvider the path provider
*/
public void setPathProvider(Function<String, Path> pathProvider) {
this.pathProvider = pathProvider;
}

/**
* Resolves a {@link Path} from a given raw {@code path}.
*
* @param path the raw path
* @return a resolved path
*/
protected Path resolvePath(String path) {
return this.pathProvider.apply(path);
}

private void checkPath(String path, PathInputContext context) {
if (!StringUtils.hasText(path)) {
context.setMessage(null);
return;
}
Path p = resolvePath(path);
boolean isDirectory = Files.isDirectory(p);
if (isDirectory) {
context.setMessage("Directory exists", MessageLevel.ERROR);
}
else {
context.setMessage("Path ok", MessageLevel.INFO);
}
}

public interface PathInputContext extends TextComponentContext<Path, PathInputContext> {

/**
* Gets an empty {@link PathInputContext}.
*
* @return empty path input context
*/
public static PathInputContext empty() {
return new DefaultPathInputContext();
}
}

private static class DefaultPathInputContext extends BaseTextComponentContext<Path, PathInputContext>
implements PathInputContext {

@Override
public Map<String, Object> toTemplateModel() {
Map<String, Object> attributes = super.toTemplateModel();
Map<String, Object> model = new HashMap<>();
model.put("model", attributes);
return model;
}
}

private class DefaultRenderer implements Function<PathInputContext, List<AttributedString>> {

@Override
public List<AttributedString> apply(PathInputContext context) {
return renderTemplateResource(context.toTemplateModel());
}
}
}
Loading