limit history commands size

This commit is contained in:
Jarred Li
2012-04-14 11:43:01 +08:00
parent 5dca1a102a
commit f12f986e1f
2 changed files with 62 additions and 47 deletions

View File

@@ -50,11 +50,10 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.14</version> <version>1.2.14</version>
</dependency> </dependency>
<!-- consider removing dependency on commons-io -->
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>1.4</version> <version>2.3</version>
</dependency> </dependency>
<!-- External modules --> <!-- External modules -->

View File

@@ -27,6 +27,7 @@ import jline.ANSIBuffer.ANSICodes;
import jline.ConsoleReader; import jline.ConsoleReader;
import jline.WindowsTerminal; import jline.WindowsTerminal;
import org.apache.commons.io.input.ReversedLinesFileReader;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@@ -40,7 +41,6 @@ import org.springframework.roo.shell.event.ShellStatus.Status;
import org.springframework.roo.shell.event.ShellStatusListener; import org.springframework.roo.shell.event.ShellStatusListener;
import org.springframework.roo.support.util.Assert; import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.ClassUtils; import org.springframework.roo.support.util.ClassUtils;
import org.springframework.roo.support.util.FileCopyUtils;
import org.springframework.roo.support.util.IOUtils; import org.springframework.roo.support.util.IOUtils;
import org.springframework.roo.support.util.OsUtils; import org.springframework.roo.support.util.OsUtils;
import org.springframework.roo.support.util.StringUtils; import org.springframework.roo.support.util.StringUtils;
@@ -63,8 +63,7 @@ import org.springframework.shell.plugin.PromptProvider;
* @author Jarred Li * @author Jarred Li
* @since 1.0 * @since 1.0
*/ */
public abstract class JLineShell extends AbstractShell public abstract class JLineShell extends AbstractShell implements CommandMarker, Shell, Runnable {
implements CommandMarker, Shell, Runnable {
// Constants // Constants
private static final String ANSI_CONSOLE_CLASSNAME = "org.fusesource.jansi.AnsiConsole"; private static final String ANSI_CONSOLE_CLASSNAME = "org.fusesource.jansi.AnsiConsole";
@@ -134,21 +133,10 @@ public abstract class JLineShell extends AbstractShell
openFileLogIfPossible(); openFileLogIfPossible();
// Try to build previous command history from the project's log // Try to build previous command history from the project's log
try {
String logFileContents = FileCopyUtils.copyToString(new File(this.historyFileName)); String[] filteredLogEntries = filterLogEntry();
String[] logEntries = logFileContents.split(StringUtils.LINE_SEPARATOR); for (String logEntry : filteredLogEntries) {
// LIFO reader.getHistory().addToHistory(logEntry);
int size = 0;
for (String logEntry : logEntries) {
if (!logEntry.startsWith("//")) {
reader.getHistory().addToHistory(logEntry);
size++;
if(size > historySize){
break;
}
}
}
} catch (IOException ignored) {
} }
flashMessageRenderer(); flashMessageRenderer();
@@ -190,8 +178,37 @@ public abstract class JLineShell extends AbstractShell
} }
public void printBannerAndWelcome(){ /**
if(printBanner){ * read history commands from history log. the history size if determined by --histsize options.
*
* @return history commands
*/
private String[] filterLogEntry() {
ArrayList<String> entries = new ArrayList<String>();
try {
ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(this.historyFileName));
int size = 0;
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("//")) {
size++;
if (size > historySize) {
break;
}
else {
entries.add(line);
}
}
}
} catch (IOException e) {
logger.warning("read history file failed");
}
Collections.reverse(entries);
return entries.toArray(new String[0]);
}
public void printBannerAndWelcome() {
if (printBanner) {
logger.info(this.version); logger.info(this.version);
logger.info(getWelcomeMessage()); logger.info(getWelcomeMessage());
} }
@@ -546,7 +563,7 @@ public abstract class JLineShell extends AbstractShell
this.applicatonContext = applicationContext; this.applicatonContext = applicationContext;
} }
public void costomizePlugin(){ public void costomizePlugin() {
this.historyFileName = getHistoryFileName(); this.historyFileName = getHistoryFileName();
this.promptText = getPromptText(); this.promptText = getPromptText();
this.version = getBannerText()[0]; this.version = getBannerText()[0];
@@ -569,7 +586,7 @@ public abstract class JLineShell extends AbstractShell
* *
* @return prompt text * @return prompt text
*/ */
private String getPromptText(){ private String getPromptText() {
return getHighestPriorityProvider(PromptProvider.class).getPromptText(); return getHighestPriorityProvider(PromptProvider.class).getPromptText();
} }
@@ -579,7 +596,7 @@ public abstract class JLineShell extends AbstractShell
* @return BannerText[0]: Banner * @return BannerText[0]: Banner
* BannerText[1]: Welcome Message. * BannerText[1]: Welcome Message.
*/ */
private String[] getBannerText(){ private String[] getBannerText() {
String[] bannerText = new String[2]; String[] bannerText = new String[2];
BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); BannerProvider provider = getHighestPriorityProvider(BannerProvider.class);
bannerText[0] = provider.getBanner(); bannerText[0] = provider.getBanner();
@@ -588,9 +605,8 @@ public abstract class JLineShell extends AbstractShell
} }
private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t){ private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t) {
Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors( Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicatonContext, t);
this.applicatonContext, t);
List<T> sortedProviders = new ArrayList<T>(providers.values()); List<T> sortedProviders = new ArrayList<T>(providers.values());
Collections.sort(sortedProviders, annocationOrderComparator); Collections.sort(sortedProviders, annocationOrderComparator);
T highestPriorityProvider = sortedProviders.get(0); T highestPriorityProvider = sortedProviders.get(0);
@@ -602,7 +618,7 @@ public abstract class JLineShell extends AbstractShell
* *
*/ */
@Override @Override
public String version(String text){ public String version(String text) {
return this.version; return this.version;
} }
@@ -611,7 +627,7 @@ public abstract class JLineShell extends AbstractShell
* *
* @return welcome message * @return welcome message
*/ */
public String getWelcomeMessage(){ public String getWelcomeMessage() {
return this.welcomeMessage; return this.welcomeMessage;
} }