// https://github.com/mattj23/InteractiveReadLine
using InteractiveReadLine.Formatting;
using InteractiveReadLine.KeyBehaviors;
using InteractiveReadLine.Tokenizing;
#nullable disable
namespace InteractiveReadLine
{
///
/// The main configuration object for the ReadLineHandler, instances of this class carry all of the objects
/// which customize and control every aspect of the ReadLineHandler's behavior. Many standard behaviors are
/// provided through the CommonFormatters/CommonKeyBehaviors/CommonLexers tools, but custom behaviors can
/// be attached to this object instead.
///
public class ReadLineConfig
{
private ReadLineConfig()
{
this.KeyBehaviors = [];
}
///
/// Gets an Action which can be used to update the history. This is automatically set when the
///
///
public Action UpdateHistory { get; private set; }
///
/// Gets a list which contains the history of entered text, used for any behaviors which interact
/// with the entered history.
///
public IReadOnlyList History { get; private set; }
///
/// Gets a providing function used to format the line to display based on a tokenization of the
/// readline content just before display. Requires a Lexer to work.
///
public Func FormatterFromTokens { get; private set; }
///
/// Gets a format providing method which should format the line based on the raw LineState
///
public Func FormatterFromLine { get; private set; }
///
/// Gets a dictionary which maps key press information to key behavior methods
///
public Dictionary> KeyBehaviors { get; }
///
/// Gets the default key behavior, which is applied if no other key behavior is first located by
/// the KeyBehaviors dictionary
///
public Action DefaultKeyBehavior { get; private set; }
///
/// Gets the lexer for the readline handler to use, which tokenizes a LineState object.
/// A non-null lexer is a critical component of auto-completion and certain token-based key behaviors
///
public Func Lexer { get; private set; }
///
/// Gets the auto-completion provider, which is a method that takes a TokenizedLine object
/// and returns a list of suggestions for the token under the cursor. The TokenizedLine can
/// also be modified by the auto-completion method.
///
public Func AutoCompletion { get; private set; }
///
/// Gets whether the configuration is capable of auto-completion, which requires both a Lexer
/// and an auto-completion handler.
///
public bool CanAutoComplete => this.Lexer != null && this.AutoCompletion != null;
///
/// Sets the auto-completion handler for the configuration
///
public ReadLineConfig SetAutoCompletion(Func handler)
{
this.AutoCompletion = handler;
return this;
}
///
/// Sets the lexer for the configuration
///
public ReadLineConfig SetLexer(Func lexer)
{
this.Lexer = lexer;
return this;
}
///
/// Sets the formatter for the configuration using a formatter based on a tokenized line. This will
/// require that a lexer be provided for the configuration.
///
///
///
public ReadLineConfig SetFormatter(Func formatter)
{
this.FormatterFromLine = null;
this.FormatterFromTokens = formatter;
return this;
}
///
/// Sets the formatter for the configuration using a formatter which only needs a simple LineState.
///
///
///
public ReadLineConfig SetFormatter(Func formatter)
{
this.FormatterFromLine = formatter;
this.FormatterFromTokens = null;
return this;
}
///
/// Sets the collection of previously entered lines to be used as the command history. Use this setter
/// if you do not want the history to be automatically updated. Also, make sure to set some key behavior
/// which will make use of the history.
///
///
///
public ReadLineConfig SetHistorySource(IReadOnlyList history)
{
this.History = history;
return this;
}
///
/// Sets the collection of previously entered lines to be used as the command history. This setter will
/// cause the history list to be automatically updated. Also, make sure to set some key behavior which
/// will make use of the history.
///
///
///
public ReadLineConfig SetUpdatingHistorySource(List history)
{
this.SetHistoryUpdateAction(history.Add);
return this.SetHistorySource(history);
}
///
/// Sets the action delegate which will get call when the readline is finalized and the history would otherwise
/// update itself. Use this to provide a custom update action.
///
///
///
public ReadLineConfig SetHistoryUpdateAction(Action action)
{
this.UpdateHistory = action;
return this;
}
///
/// Set the default KeyBehavior which is called when no other special action for a key has been registered
/// in the configuration. Typically this will be used to insert the key character into the input line.
///
///
///
public ReadLineConfig SetDefaultKeyBehavior(Action defaultBehavior)
{
this.DefaultKeyBehavior = defaultBehavior;
return this;
}
///
/// Gets an empty configuration with absolutely nothing set
///
public static ReadLineConfig Empty => new ReadLineConfig();
///
/// Gets a configuration that has the standard key behaviors added to it
///
public static ReadLineConfig Basic => new ReadLineConfig().AddStandardKeys();
}
}