github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readline/instance.go (about)

     1  package readline
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"sync"
     7  )
     8  
     9  var ForceCrLf = true
    10  
    11  type HintCacheFuncT func(prefix string, items []string) []string
    12  type PreviewFuncT func(ctx context.Context, line []rune, item string, incImages bool, size *PreviewSizeT, callback PreviewFuncCallbackT)
    13  type PreviewFuncCallbackT func(lines []string, pos int, err error)
    14  
    15  type TabCompleterReturnT struct {
    16  	Prefix       string
    17  	Suggestions  []string
    18  	Descriptions map[string]string
    19  	DisplayType  TabDisplayType
    20  	HintCache    HintCacheFuncT
    21  	Preview      PreviewFuncT
    22  }
    23  
    24  // Instance is used to encapsulate the parameter group and run time of any given
    25  // readline instance so that you can reuse the readline API for multiple entry
    26  // captures without having to repeatedly unload configuration.
    27  type Instance struct {
    28  	fdMutex sync.Mutex
    29  	//fdMutex debug.BadMutex
    30  
    31  	Active        bool
    32  	closeSigwinch func()
    33  
    34  	// PasswordMask is what character to hide password entry behind.
    35  	// Once enabled, set to 0 (zero) to disable the mask again.
    36  	PasswordMask rune
    37  
    38  	// SyntaxHighlight is a helper function to provide syntax highlighting.
    39  	// Once enabled, set to nil to disable again.
    40  	SyntaxHighlighter func([]rune) string
    41  
    42  	// History is an interface for querying the readline history.
    43  	// This is exposed as an interface to allow you the flexibility to define how
    44  	// you want your history managed (eg file on disk, database, cloud, or even
    45  	// no history at all). By default it uses a dummy interface that only stores
    46  	// historic items in memory.
    47  	History History
    48  
    49  	// HistoryAutoWrite defines whether items automatically get written to
    50  	// history.
    51  	// Enabled by default. Set to false to disable.
    52  	HistoryAutoWrite bool
    53  
    54  	// TabCompleter is a function that offers completion suggestions.
    55  	TabCompleter      func([]rune, int, DelayedTabContext) *TabCompleterReturnT
    56  	delayedTabContext DelayedTabContext
    57  
    58  	tcr *TabCompleterReturnT
    59  
    60  	MinTabItemLength int
    61  	MaxTabItemLength int
    62  
    63  	// MaxTabCompletionRows is the maximum number of rows to display in the tab
    64  	// completion grid.
    65  	MaxTabCompleterRows int
    66  
    67  	// SyntaxCompletion is used to autocomplete code syntax (like braces and
    68  	// quotation marks). If you want to complete words or phrases then you might
    69  	// be better off using the TabCompletion function.
    70  	// SyntaxCompletion takes the line ([]rune), change (string) and cursor
    71  	// position, and returns the new line and cursor position.
    72  	SyntaxCompleter func([]rune, string, int) ([]rune, int)
    73  
    74  	// DelayedSyntaxWorker allows for syntax highlighting happen to the line
    75  	// after the line has been drawn.
    76  	DelayedSyntaxWorker func([]rune) []rune
    77  	delayedSyntaxCount  int32
    78  
    79  	// HintText is a helper function which displays hint text the prompt.
    80  	// HintText takes the line input from the prompt and the cursor position.
    81  	// It returns the hint text to display.
    82  	HintText func([]rune, int) []rune
    83  
    84  	// HintColor any ANSI escape codes you wish to use for hint formatting. By
    85  	// default this will just be blue.
    86  	HintFormatting string
    87  
    88  	// AutocompleteHistory is another customization allowing for alternative
    89  	// results when [ctrl]+[r]
    90  	AutocompleteHistory func(string) ([]string, map[string]string)
    91  
    92  	// TempDirectory is the path to write temporary files when editing a line in
    93  	// $EDITOR. This will default to os.TempDir()
    94  	TempDirectory string
    95  
    96  	// GetMultiLine is a callback to your host program. Since multiline support
    97  	// is handled by the application rather than readline itself, this callback
    98  	// is required when calling $EDITOR. However if this function is not set
    99  	// then readline will just use the current line.
   100  	GetMultiLine func([]rune) []rune
   101  
   102  	MaxCacheSize int
   103  	cacheHint    cacheSliceRune
   104  	cacheSyntax  cacheString
   105  	//cacheSyntaxHighlight cacheString
   106  	//cacheSyntaxDelayed   cacheSliceRune
   107  
   108  	// readline operating parameters
   109  	prompt        string //  = ">>> "
   110  	promptLen     int    //= 4
   111  	line          *UnicodeT
   112  	lineChange    string // cache what had changed from previous line
   113  	termWidth     int
   114  	multiline     []byte
   115  	multiSplit    []string
   116  	skipStdinRead bool
   117  
   118  	// history
   119  	lineBuf *UnicodeT
   120  	histPos int
   121  
   122  	// hint text
   123  	hintY    int
   124  	hintText []rune
   125  
   126  	ScreenRefresh func()
   127  
   128  	PreviewInit   func()
   129  	previewMode   previewModeT
   130  	previewRef    previewRefT
   131  	previewItem   string
   132  	previewCache  *previewCacheT
   133  	PreviewImages bool
   134  	previewCancel context.CancelFunc
   135  	PreviewLine   PreviewFuncT
   136  
   137  	// tab completion
   138  	modeTabCompletion bool
   139  	tabMutex          sync.Mutex
   140  	tcPrefix          string
   141  	tcSuggestions     []string
   142  	tcDescriptions    map[string]string
   143  	tcDisplayType     TabDisplayType
   144  	tcOffset          int
   145  	tcPosX            int
   146  	tcPosY            int
   147  	tcMaxX            int
   148  	tcMaxY            int
   149  	tcUsedY           int
   150  	tcMaxLength       int
   151  
   152  	// tab find
   153  	modeTabFind   bool
   154  	rFindSearch   []rune // searching message
   155  	rFindCancel   []rune // search cancelled message
   156  	tfLine        []rune
   157  	tfSuggestions []string
   158  	modeAutoFind  bool // for when invoked via ^R or ^F outside of [tab]
   159  
   160  	// vim
   161  	modeViMode       viMode //= vimInsert
   162  	viIteration      string
   163  	viUndoHistory    []*UnicodeT
   164  	viUndoSkipAppend bool
   165  	viYankBuffer     string
   166  
   167  	// event
   168  	evtKeyPress map[string]func(string, []rune, int) *EventReturn
   169  
   170  	//ForceCrLf          bool
   171  	EnableGetCursorPos bool
   172  }
   173  
   174  // NewInstance is used to create a readline instance and initialise it with sane
   175  // defaults.
   176  func NewInstance() *Instance {
   177  	rl := new(Instance)
   178  
   179  	rl.line = new(UnicodeT)
   180  	rl.lineBuf = new(UnicodeT)
   181  	rl.History = new(ExampleHistory)
   182  	rl.HistoryAutoWrite = true
   183  	rl.MaxTabCompleterRows = 4
   184  	rl.prompt = ">>> "
   185  	rl.promptLen = 4
   186  	rl.HintFormatting = seqFgBlue
   187  	rl.evtKeyPress = make(map[string]func(string, []rune, int) *EventReturn)
   188  
   189  	rl.TempDirectory = os.TempDir()
   190  
   191  	rl.MaxCacheSize = 256
   192  	rl.cacheHint.Init(rl)
   193  	rl.cacheSyntax.Init(rl)
   194  
   195  	//rl.ForceCrLf = true
   196  
   197  	return rl
   198  }