org.codehaus.groovy.tools.shell.commands.HistoryCommand.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all Show documentation
Show all versions of groovy-all Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2007 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
*
* http://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.codehaus.groovy.tools.shell.commands
import org.codehaus.groovy.tools.shell.ComplexCommandSupport
import org.codehaus.groovy.tools.shell.Shell
import org.codehaus.groovy.tools.shell.util.SimpleCompletor
/**
* The 'history' command.
*
* @version $Id: HistoryCommand.groovy 8422 2007-10-07 07:32:07Z user57 $
* @author Jason Dillon
*/
class HistoryCommand
extends ComplexCommandSupport
{
HistoryCommand(final Shell shell) {
super(shell, 'history', '\\H')
this.functions = [ 'show', 'clear', 'flush', 'recall' ]
this.defaultFunction = 'show'
}
protected List createCompletors() {
def loader = {
def list = []
functions.each { list << it }
return list
}
return [
new SimpleCompletor(loader),
null
]
}
Object execute(List args) {
if (!history) {
fail("Shell does not appear to be interactive; Can not query history")
}
super.execute(args)
// Don't return anything
return null
}
def do_show = {
history.historyList.eachWithIndex { item, i ->
i = i.toString().padLeft(3, ' ')
io.out.println(" @|bold $i| $item")
}
}
def do_clear = {
history.clear()
if (io.verbose) {
io.out.println('History cleared')
}
}
def do_flush = {
history.flushBuffer()
if (io.verbose) {
io.out.println('History flushed')
}
}
def do_recall = { args ->
def line
if (args.size() != 1) {
fail("History recall requires a single history identifer")
}
def id = args[0]
//
// FIXME: This won't work as desired because the history shifts when we run recall and could internally shift more from alias redirection
//
try {
id = Integer.parseInt(id)
line = history.historyList[id]
}
catch (Exception e) {
fail("Invalid history identifier: $id", e)
}
log.debug("Recalling history item #$id: $line")
return shell.execute(line)
}
}