Package org.jline.console.impl
Class ConsoleEngineImpl
- java.lang.Object
-
- org.jline.console.impl.AbstractCommandRegistry
-
- org.jline.console.impl.JlineCommandRegistry
-
- org.jline.console.impl.ConsoleEngineImpl
-
- All Implemented Interfaces:
CommandRegistry,ConsoleEngine
public class ConsoleEngineImpl extends JlineCommandRegistry implements ConsoleEngine
Manage console variables, commands and script execution.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classConsoleEngineImpl.AliasValueCompleterstatic classConsoleEngineImpl.Commandprivate classConsoleEngineImpl.ScriptFile-
Nested classes/interfaces inherited from interface org.jline.console.CommandRegistry
CommandRegistry.CommandSession
-
Nested classes/interfaces inherited from interface org.jline.console.ConsoleEngine
ConsoleEngine.ExecutionResult, ConsoleEngine.WidgetCreator
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.lang.String>aliasesprivate java.nio.file.PathaliasFileprivate static java.lang.StringEND_HELPprivate ScriptEngineengineprivate java.lang.Exceptionexceptionprivate booleanexecutingprivate static intHELP_MAX_SIZEprivate static java.lang.String[]OPTION_HELPprivate static java.lang.StringOPTION_VERBOSEprivate java.util.Map<java.lang.String,java.util.List<java.lang.String>>pipesprivate Printerprinterprivate LineReaderreaderprivate java.lang.StringscriptExtensionprivate static java.lang.StringSLURP_FORMAT_TEXTprivate SystemRegistrysystemRegistryprivate static java.lang.StringVAR_CONSOLE_OPTIONSprivate static java.lang.StringVAR_PATHprivate java.util.function.Supplier<java.nio.file.Path>workDir
-
Constructor Summary
Constructors Constructor Description ConsoleEngineImpl(java.util.Set<ConsoleEngineImpl.Command> commands, ScriptEngine engine, Printer printer, java.util.function.Supplier<java.nio.file.Path> workDir, ConfigurationPath configPath)ConsoleEngineImpl(ScriptEngine engine, Printer printer, java.util.function.Supplier<java.nio.file.Path> workDir, ConfigurationPath configPath)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.Objectaliascmd(CommandInput input)private java.util.List<Completer>aliasCompleter(java.lang.String command)private java.util.Set<java.lang.String>commandAliasNames()private booleanconsoleOption(java.lang.String option)<T> TconsoleOption(java.lang.String option, T defval)Read console option valueprivate java.util.Map<java.lang.String,java.lang.Object>consoleOptions()private java.lang.Objectdel(CommandInput input)private java.lang.Objectdoc(CommandInput input)private java.util.List<Completer>docCompleter(java.lang.String command)private java.util.List<java.lang.String>docs()private voiderror(java.lang.String message)java.lang.Objectexecute(java.io.File script, java.lang.String cmdLine, java.lang.String[] args)Executes either JLine or ScriptEngine script.java.lang.Objectexecute(java.lang.String cmd, java.lang.String line, java.lang.String[] args)Executes command line that does not contain known command by the system registry.booleanexecuteWidget(java.lang.Object function)Execute widget functionjava.lang.StringexpandCommandLine(java.lang.String line)Substitutes command line with system registry invoke method call.private java.lang.StringexpandName(java.lang.String name)java.lang.Object[]expandParameters(java.lang.String[] args)Substituting args references with their values.private java.lang.StringexpandToList(java.lang.String[] args)java.lang.StringexpandToList(java.util.List<java.lang.String> params)Expands parameter list to stringjava.lang.StringgetAlias(java.lang.String name)Returns alias 'name' valuejava.util.List<java.lang.String>getNamedPipes()Returns named pipe namesjava.util.Map<java.lang.String,java.util.List<java.lang.String>>getPipes()Returns defined pipesjava.lang.ObjectgetVariable(java.lang.String name)Get variable valuebooleanhasAlias(java.lang.String name)Returns true if alias 'name' existsbooleanhasVariable(java.lang.String name)Test if variable with name existsjava.lang.Objectinvoke(CommandRegistry.CommandSession session, java.lang.String command, java.lang.Object... args)Execute a command.private booleanisCodeBlock(java.lang.String line)private booleanisCommandLine(java.lang.String line)booleanisExecuting()private booleanisNumber(java.lang.String str)private Parserparser()voidpersist(java.nio.file.Path file, java.lang.Object object)Persist object to fileprivate java.lang.Objectpipe(CommandInput input)ConsoleEngine.ExecutionResultpostProcess(java.lang.Object result)Post processes execution result.private ConsoleEngine.ExecutionResultpostProcess(java.lang.String line, java.lang.Object result)ConsoleEngine.ExecutionResultpostProcess(java.lang.String line, java.lang.Object result, java.lang.String output)Post processes execution result.voidprintln(java.lang.Object object)Print object.private java.lang.Objectprnt(CommandInput input)private java.util.List<Completer>prntCompleter(java.lang.String command)voidpurge()Delete temporary console variablesvoidputVariable(java.lang.String name, java.lang.Object value)Create console variableprivate java.lang.Stringquote(java.lang.String var)private intsaveResult(java.lang.String var, java.lang.Object result)java.util.List<Completer>scriptCompleters()Returns script and variable completersprivate java.util.List<java.lang.String>scriptExtensions()private java.util.Set<java.lang.String>scriptNames()java.util.Map<java.lang.String,java.lang.Boolean>scripts()Returns all scripts found from PATHvoidsetLineReader(LineReader reader)Sets lineReadervoidsetScriptExtension(java.lang.String extension)Sets file name extension used by console scriptsvoidsetSystemRegistry(SystemRegistry systemRegistry)Sets systemRegistryprivate java.lang.Objectshow(CommandInput input)java.lang.Objectslurp(java.nio.file.Path file)Read object from fileprivate java.lang.Objectslurp(java.nio.file.Path file, java.nio.charset.Charset encoding, java.lang.String format)private java.lang.Objectslurpcmd(CommandInput input)private java.util.List<Completer>slurpCompleter(java.lang.String command)private Terminalterminal()voidtrace(java.lang.Object object)private java.lang.Objectunalias(CommandInput input)private java.util.List<Completer>unaliasCompleter(java.lang.String command)private booleanurlExists(java.lang.String weburl)private java.util.List<Completer>variableCompleter(java.lang.String command)private java.util.List<java.lang.String>variableReferences()-
Methods inherited from class org.jline.console.impl.JlineCommandRegistry
commandDescription, commandInfo, commandOptions, compileCommandDescription, compileCommandInfo, compileCommandOptions, defaultCompleter, parseOptions
-
Methods inherited from class org.jline.console.impl.AbstractCommandRegistry
alias, commandAliases, commandNames, compileCompleters, doHelpDesc, getCommandMethods, hasCommand, registerCommands, registerCommands, registeredCommand, rename, saveException
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jline.console.CommandRegistry
commandAliases, commandDescription, commandInfo, commandNames, compileCompleters, hasCommand, name
-
Methods inherited from interface org.jline.console.ConsoleEngine
execute
-
-
-
-
Field Detail
-
VAR_CONSOLE_OPTIONS
private static final java.lang.String VAR_CONSOLE_OPTIONS
- See Also:
- Constant Field Values
-
VAR_PATH
private static final java.lang.String VAR_PATH
- See Also:
- Constant Field Values
-
OPTION_HELP
private static final java.lang.String[] OPTION_HELP
-
OPTION_VERBOSE
private static final java.lang.String OPTION_VERBOSE
- See Also:
- Constant Field Values
-
SLURP_FORMAT_TEXT
private static final java.lang.String SLURP_FORMAT_TEXT
- See Also:
- Constant Field Values
-
END_HELP
private static final java.lang.String END_HELP
- See Also:
- Constant Field Values
-
HELP_MAX_SIZE
private static final int HELP_MAX_SIZE
- See Also:
- Constant Field Values
-
engine
private final ScriptEngine engine
-
exception
private java.lang.Exception exception
-
systemRegistry
private SystemRegistry systemRegistry
-
scriptExtension
private java.lang.String scriptExtension
-
workDir
private final java.util.function.Supplier<java.nio.file.Path> workDir
-
aliases
private final java.util.Map<java.lang.String,java.lang.String> aliases
-
pipes
private final java.util.Map<java.lang.String,java.util.List<java.lang.String>> pipes
-
aliasFile
private java.nio.file.Path aliasFile
-
reader
private LineReader reader
-
executing
private boolean executing
-
printer
private final Printer printer
-
-
Constructor Detail
-
ConsoleEngineImpl
public ConsoleEngineImpl(ScriptEngine engine, Printer printer, java.util.function.Supplier<java.nio.file.Path> workDir, ConfigurationPath configPath) throws java.io.IOException
- Throws:
java.io.IOException
-
ConsoleEngineImpl
public ConsoleEngineImpl(java.util.Set<ConsoleEngineImpl.Command> commands, ScriptEngine engine, Printer printer, java.util.function.Supplier<java.nio.file.Path> workDir, ConfigurationPath configPath) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
setLineReader
public void setLineReader(LineReader reader)
Description copied from interface:ConsoleEngineSets lineReader- Specified by:
setLineReaderin interfaceConsoleEngine- Parameters:
reader- LineReader
-
parser
private Parser parser()
-
terminal
private Terminal terminal()
-
isExecuting
public boolean isExecuting()
- Specified by:
isExecutingin interfaceConsoleEngine- Returns:
- true if consoleEngine is executing script
-
setSystemRegistry
public void setSystemRegistry(SystemRegistry systemRegistry)
Description copied from interface:ConsoleEngineSets systemRegistry- Specified by:
setSystemRegistryin interfaceConsoleEngine- Parameters:
systemRegistry- SystemRegistry
-
setScriptExtension
public void setScriptExtension(java.lang.String extension)
Description copied from interface:ConsoleEngineSets file name extension used by console scripts- Specified by:
setScriptExtensionin interfaceConsoleEngine- Parameters:
extension- console script file extension
-
hasAlias
public boolean hasAlias(java.lang.String name)
Description copied from interface:ConsoleEngineReturns true if alias 'name' exists- Specified by:
hasAliasin interfaceConsoleEngine- Parameters:
name- alias name- Returns:
- true if alias exists
-
getAlias
public java.lang.String getAlias(java.lang.String name)
Description copied from interface:ConsoleEngineReturns alias 'name' value- Specified by:
getAliasin interfaceConsoleEngine- Parameters:
name- alias name- Returns:
- value of alias
-
getPipes
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getPipes()
Description copied from interface:ConsoleEngineReturns defined pipes- Specified by:
getPipesin interfaceConsoleEngine- Returns:
- map of defined pipes
-
getNamedPipes
public java.util.List<java.lang.String> getNamedPipes()
Description copied from interface:ConsoleEngineReturns named pipe names- Specified by:
getNamedPipesin interfaceConsoleEngine- Returns:
- list of named pipe names
-
scriptCompleters
public java.util.List<Completer> scriptCompleters()
Description copied from interface:ConsoleEngineReturns script and variable completers- Specified by:
scriptCompletersin interfaceConsoleEngine- Returns:
- script and variable completers
-
commandAliasNames
private java.util.Set<java.lang.String> commandAliasNames()
-
scriptNames
private java.util.Set<java.lang.String> scriptNames()
-
scripts
public java.util.Map<java.lang.String,java.lang.Boolean> scripts()
Description copied from interface:ConsoleEngineReturns all scripts found from PATH- Specified by:
scriptsin interfaceConsoleEngine- Returns:
- map keys have script file names and value is true if it is console script
-
expandParameters
public java.lang.Object[] expandParameters(java.lang.String[] args) throws java.lang.ExceptionDescription copied from interface:ConsoleEngineSubstituting args references with their values.- Specified by:
expandParametersin interfaceConsoleEngine- Parameters:
args- the arguments to be expanded- Returns:
- expanded arguments
- Throws:
java.lang.Exception- in case of error
-
expandToList
private java.lang.String expandToList(java.lang.String[] args)
-
expandToList
public java.lang.String expandToList(java.util.List<java.lang.String> params)
Description copied from interface:ConsoleEngineExpands parameter list to string- Specified by:
expandToListin interfaceConsoleEngine- Parameters:
params- list of script parameters- Returns:
- expanded parameters list
-
expandName
private java.lang.String expandName(java.lang.String name)
-
isNumber
private boolean isNumber(java.lang.String str)
-
isCodeBlock
private boolean isCodeBlock(java.lang.String line)
-
isCommandLine
private boolean isCommandLine(java.lang.String line)
-
quote
private java.lang.String quote(java.lang.String var)
-
scriptExtensions
private java.util.List<java.lang.String> scriptExtensions()
-
execute
public java.lang.Object execute(java.io.File script, java.lang.String cmdLine, java.lang.String[] args) throws java.lang.ExceptionDescription copied from interface:ConsoleEngineExecutes either JLine or ScriptEngine script.- Specified by:
executein interfaceConsoleEngine- Parameters:
script- script filecmdLine- raw command lineargs- script arguments- Returns:
- script execution result
- Throws:
java.lang.Exception- in case of error
-
expandCommandLine
public java.lang.String expandCommandLine(java.lang.String line)
Description copied from interface:ConsoleEngineSubstitutes command line with system registry invoke method call.- Specified by:
expandCommandLinein interfaceConsoleEngine- Parameters:
line- command line to be expanded- Returns:
- expanded command line
-
execute
public java.lang.Object execute(java.lang.String cmd, java.lang.String line, java.lang.String[] args) throws java.lang.ExceptionDescription copied from interface:ConsoleEngineExecutes command line that does not contain known command by the system registry. If the line is neither JLine or ScriptEngine script it will be evaluated as ScriptEngine statement.- Specified by:
executein interfaceConsoleEngine- Parameters:
cmd- parsed command/script nameline- raw command lineargs- parsed arguments of the command- Returns:
- command line execution result
- Throws:
java.lang.Exception- in case of error
-
purge
public void purge()
Description copied from interface:ConsoleEngineDelete temporary console variables- Specified by:
purgein interfaceConsoleEngine
-
putVariable
public void putVariable(java.lang.String name, java.lang.Object value)Description copied from interface:ConsoleEngineCreate console variable- Specified by:
putVariablein interfaceConsoleEngine- Parameters:
name- name of the variablevalue- value of the variable
-
getVariable
public java.lang.Object getVariable(java.lang.String name)
Description copied from interface:ConsoleEngineGet variable value- Specified by:
getVariablein interfaceConsoleEngine- Parameters:
name- name of the variable- Returns:
- variable value
-
hasVariable
public boolean hasVariable(java.lang.String name)
Description copied from interface:ConsoleEngineTest if variable with name exists- Specified by:
hasVariablein interfaceConsoleEngine- Parameters:
name- name of the variable- Returns:
- true if variable with name exists
-
executeWidget
public boolean executeWidget(java.lang.Object function)
Description copied from interface:ConsoleEngineExecute widget function- Specified by:
executeWidgetin interfaceConsoleEngine- Parameters:
function- to execute- Returns:
- true on success
-
consoleOptions
private java.util.Map<java.lang.String,java.lang.Object> consoleOptions()
-
consoleOption
public <T> T consoleOption(java.lang.String option, T defval)Description copied from interface:ConsoleEngineRead console option value- Specified by:
consoleOptionin interfaceConsoleEngine- Type Parameters:
T- option type- Parameters:
option- option namedefval- default value- Returns:
- option value
-
consoleOption
private boolean consoleOption(java.lang.String option)
-
postProcess
public ConsoleEngine.ExecutionResult postProcess(java.lang.String line, java.lang.Object result, java.lang.String output)
Description copied from interface:ConsoleEnginePost processes execution result. If result is to be assigned to the console variable then method will return null.- Specified by:
postProcessin interfaceConsoleEngine- Parameters:
line- command lineresult- command result to processoutput- command redirected output- Returns:
- processed result
-
postProcess
private ConsoleEngine.ExecutionResult postProcess(java.lang.String line, java.lang.Object result)
-
postProcess
public ConsoleEngine.ExecutionResult postProcess(java.lang.Object result)
Description copied from interface:ConsoleEnginePost processes execution result.- Specified by:
postProcessin interfaceConsoleEngine- Parameters:
result- command result to process- Returns:
- processed result
-
saveResult
private int saveResult(java.lang.String var, java.lang.Object result)
-
invoke
public java.lang.Object invoke(CommandRegistry.CommandSession session, java.lang.String command, java.lang.Object... args) throws java.lang.Exception
Description copied from interface:CommandRegistryExecute a command.- Specified by:
invokein interfaceCommandRegistry- Overrides:
invokein classAbstractCommandRegistry- Parameters:
session- the data of the current command sessioncommand- the name of the commandargs- arguments of the command- Returns:
- result of the command execution
- Throws:
java.lang.Exception- in case of error
-
trace
public void trace(java.lang.Object object)
- Specified by:
tracein interfaceConsoleEngine- Parameters:
object- object to print
-
error
private void error(java.lang.String message)
-
println
public void println(java.lang.Object object)
Description copied from interface:ConsoleEnginePrint object.- Specified by:
printlnin interfaceConsoleEngine- Parameters:
object- object to print
-
show
private java.lang.Object show(CommandInput input)
-
del
private java.lang.Object del(CommandInput input)
-
prnt
private java.lang.Object prnt(CommandInput input)
-
slurpcmd
private java.lang.Object slurpcmd(CommandInput input)
-
persist
public void persist(java.nio.file.Path file, java.lang.Object object)Description copied from interface:ConsoleEnginePersist object to file- Specified by:
persistin interfaceConsoleEngine- Parameters:
file- file where object should be writtenobject- object to persist
-
slurp
public java.lang.Object slurp(java.nio.file.Path file) throws java.io.IOExceptionDescription copied from interface:ConsoleEngineRead object from file- Specified by:
slurpin interfaceConsoleEngine- Parameters:
file- file from where object should be read- Returns:
- object
- Throws:
java.io.IOException- in case of error
-
slurp
private java.lang.Object slurp(java.nio.file.Path file, java.nio.charset.Charset encoding, java.lang.String format) throws java.io.IOException- Throws:
java.io.IOException
-
aliascmd
private java.lang.Object aliascmd(CommandInput input)
-
unalias
private java.lang.Object unalias(CommandInput input)
-
pipe
private java.lang.Object pipe(CommandInput input)
-
doc
private java.lang.Object doc(CommandInput input)
-
urlExists
private boolean urlExists(java.lang.String weburl)
-
slurpCompleter
private java.util.List<Completer> slurpCompleter(java.lang.String command)
-
variableCompleter
private java.util.List<Completer> variableCompleter(java.lang.String command)
-
variableReferences
private java.util.List<java.lang.String> variableReferences()
-
prntCompleter
private java.util.List<Completer> prntCompleter(java.lang.String command)
-
aliasCompleter
private java.util.List<Completer> aliasCompleter(java.lang.String command)
-
unaliasCompleter
private java.util.List<Completer> unaliasCompleter(java.lang.String command)
-
docs
private java.util.List<java.lang.String> docs()
-
docCompleter
private java.util.List<Completer> docCompleter(java.lang.String command)
-
-