Skip to content

Commit

Permalink
FORGE-1445 : Enchanted ls command on Java class to show fields and me…
Browse files Browse the repository at this point in the history
…thods separately
  • Loading branch information
balent committed May 11, 2014
1 parent 21fc5e5 commit 896467e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 19 deletions.
5 changes: 5 additions & 0 deletions shell/addon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>text</artifactId>
<classifier>forge-addon</classifier>
</dependency>
<dependency>
<groupId>org.jboss.forge.addon</groupId>
<artifactId>parser-java</artifactId>
<classifier>forge-addon</classifier>
</dependency>

<!-- Furnace Container -->
<dependency>
Expand Down
6 changes: 6 additions & 0 deletions shell/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.forge.addon</groupId>
<artifactId>parser-java</artifactId>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

import javax.inject.Inject;

import org.jboss.aesh.console.Config;
import org.jboss.aesh.parser.Parser;
import org.jboss.aesh.terminal.TerminalSize;
import org.jboss.forge.addon.parser.java.resources.JavaFieldResource;
import org.jboss.forge.addon.parser.java.resources.JavaMethodResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
Expand Down Expand Up @@ -97,30 +100,101 @@ public Result execute(UIExecutionContext context) throws Exception

private String listMany(Iterable<Resource<?>> resources, Shell shell)
{
if (resources == null) {
return "";
}

TerminalSize terminalSize = shell.getConsole().getShell().getSize();
List<String> display = new ArrayList<>();

List<FileResource> fileResources = new ArrayList<>();
List<JavaFieldResource> fieldResources = new ArrayList<>();
List<JavaMethodResource> methodResources = new ArrayList<>();
List<Resource> otherResources = new ArrayList<>();

for (Resource<?> resource : resources)
{
if (resource instanceof FileResource) {
fileResources.add((FileResource) resource);
} else if (resource instanceof JavaFieldResource) {
fieldResources.add((JavaFieldResource) resource);
} else if (resource instanceof JavaMethodResource) {
methodResources.add((JavaMethodResource) resource);
} else {
otherResources.add(resource);
}
}

StringBuilder sb = new StringBuilder();

if (fileResources.size() > 0) {
sb.append(getFileFormattedList(fileResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (fieldResources.size() > 0) {
sb.append(Config.getLineSeparator());
sb.append(ShellUtil.colorizeLabel("[fields]"));
sb.append(Config.getLineSeparator());
sb.append(getJavaFieldFormattedList(fieldResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (methodResources.size() > 0) {
sb.append(Config.getLineSeparator());
sb.append(ShellUtil.colorizeLabel("[methods]"));
sb.append(Config.getLineSeparator());
sb.append(getJavaMethodFormattedList(methodResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

if (otherResources.size() > 0) {
sb.append(getFormattedList(otherResources, terminalSize.getHeight(), terminalSize.getWidth()));
}

return sb.toString();
}

private String getFileFormattedList(List<FileResource> resources, int termHeight, int termWidth) {

boolean showAll = all.getValue();
if (resources != null)
List<String> display = new ArrayList<>();

for (FileResource resource : resources)
{
for (Resource<?> resource : resources)
if (!showAll && resource.getName().startsWith("."))
{
String name;
if (resource instanceof FileResource)
{
FileResource<?> fileResource = (FileResource<?>) resource;
if (!showAll && fileResource.getName().startsWith("."))
{
continue;
}
name = ShellUtil.colorizeResource(fileResource);
}
else
{
name = resource.getName();
}
display.add(name);
continue;
}
display.add(ShellUtil.colorizeResource(resource));
}
return Parser.formatDisplayList(display, terminalSize.getHeight(), terminalSize.getWidth());

return Parser.formatDisplayList(display, termHeight, termWidth);
}

private String getJavaFieldFormattedList(List<JavaFieldResource> resources, int termHeight, int termWidth) {
List<String> display = new ArrayList<>();

for (JavaFieldResource resource : resources) {
display.add(ShellUtil.colorizeJavaFieldResource(resource));
}

return Parser.formatDisplayList(display, termHeight, termWidth);
}

private String getJavaMethodFormattedList(List<JavaMethodResource> resources, int termHeight, int termWidth) {
List<String> display = new ArrayList<>();

for (JavaMethodResource resource : resources) {
display.add(ShellUtil.colorizeJavaMethodResource(resource));
}

return Parser.formatDisplayList(display, termHeight, termWidth);
}

private String getFormattedList(List<Resource> resources, int termHeight, int termWidth) {
List<String> display = new ArrayList<>();

for (Resource resource : resources) {
display.add(resource.getName());
}

return Parser.formatDisplayList(display, termHeight, termWidth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.jboss.aesh.terminal.Color;
import org.jboss.aesh.terminal.TerminalColor;
import org.jboss.aesh.terminal.TerminalString;
import org.jboss.forge.addon.parser.java.resources.JavaFieldResource;
import org.jboss.forge.addon.parser.java.resources.JavaMethodResource;
import org.jboss.forge.addon.resource.FileResource;

/**
Expand Down Expand Up @@ -55,4 +57,20 @@ else if (resource.isExecutable())
}
return name;
}

public static String colorizeJavaMethodResource(JavaMethodResource resource) {
String name = resource.getName();
String[] splitName = name.split("(?=\\:\\:)"); // split with "::" but preserve delimiter
return splitName[0] + new TerminalString(splitName[1], new TerminalColor(Color.GREEN, Color.DEFAULT)).toString();
}

public static String colorizeJavaFieldResource(JavaFieldResource resource) {
String name = resource.getName();
String[] splitName = name.split("(?=\\:\\:)"); // split with "::" but preserve delimiter
return splitName[0] + new TerminalString(splitName[1], new TerminalColor(Color.GREEN, Color.DEFAULT)).toString();
}

public static String colorizeLabel(String label) {
return new TerminalString(label, new TerminalColor(Color.RED, Color.DEFAULT)).toString();
}
}

0 comments on commit 896467e

Please sign in to comment.