package runner class RunnerBehaviour( val form : RunnerForm ) : TextBehaviour("Runner"), Promptable { constructor() : this( RunnerForm() ) val textInput = StringFeature( form.commandString.value, this:>runString ).apply { columns = -1 } val back = Action( "Back", this:>back ) .icon( "back" ).shortcut( Key.UP.alt() ) val forward = SplitMenu( "Forward", this:>forward ) .onDemand( this:>buildHistory ) .icon( "forward" ).shortcut( Key.DOWN.alt() ) val duplicate = Action( "Duplicate Tab", this:>duplicate ) .icon( "add" ) var historyIndex = -1 val history = listOf() init { readOnly = true disableHistory() if ( form.commandString.value.isBlank() ) { append( "Type any command, then ENTER to run." ) } else { run() } } override meth attached( container : Container ) { super.attached( container ) focusArea( 1 ) } meth runString( text : String ) { form.commandString.value = text run() } meth run() { if (invokeRunnerPrefix( form.commandString.value ) ) return message( "" ) val cmd = form.commandString.value addToHistory(cmd) title = "$ " + if (cmd.size() < 15) cmd else form.commandString.value.replaceAll( "\\s.*", "" ) if (form.clear.value) { clear() } else { append( "\n${form.commandString.value}\n\n" ) } $( ${cmd} ) .dir( form.folder.value ) .outToTextBehaviour( this, "Runner", this:>onFinished ) } meth addToHistory( cmd : String ) { if (historyIndex < 0 || history[historyIndex] != cmd) { historyIndex = history.size() history.add( cmd ) } while (history.size() > Runner.maxHistory) { history.remove(0) } } meth duplicate() { openBehaviour( RunnerBehaviour( form.copy() ) ) } meth onFinished( result : Boolean ) { moveToEnd() focus() } meth back() { if (historyIndex > 0) { historyIndex -- textInput.text = history[historyIndex] form.commandString.value = textInput.text focus() } else { message( "Cannot go further back" ) } } meth forward() { if (historyIndex < history.size()-1) { historyIndex ++ textInput.text = history[historyIndex] form.commandString.value = textInput.text focus() } else { message( "Cannot go further forward" ) } } meth buildHistory() : List { val result = listOf() var index = 0 for ( cmd in history ) { result.add( Action( "$cmd", this:>chooseFromHistory.curry( index ) ) ) index ++ } return result } meth chooseFromHistory( index : int ) { historyIndex = index textInput.text = history[index] form.commandString.value = textInput.text textInput.focus() } override meth toolbar() = listOf ( textInput, separator, back, forward, separator, form.clear, duplicate ) override meth focus() { textInput.focus() } override meth promptForm() = form }