Module atom: correction + ajout de méthodes pour Buffer et State; Module scanner: ajout d’une fonction pour scanner les champs de manière brute

This commit is contained in:
Benjamin VAUDOUR 2024-03-02 14:26:17 +01:00
parent 974a160bfb
commit 3307dec97c
4 changed files with 208 additions and 31 deletions

View file

@ -305,3 +305,11 @@ func (b *Buffer) Clone() *Buffer {
return &cb
}
func (b *Buffer) SetCursor(cursor int) (ok bool) {
if ok = cursor > 0 && cursor <= b.Len() && cursor != b.cursor; ok {
b.cursor = cursor
}
return
}

View file

@ -45,7 +45,7 @@ func CheckUnicode(str string) (err option.Option[error]) {
if i == len(runes)-1 || runes[i+1] != '[' {
return option.Some(ErrInvalidUnicode)
}
} else if unicode.Is(unicode.C, r) {
} else if unicode.Is(unicode.C, r) && r != '\n' {
return option.Some(ErrInvalidUnicode)
}
}

View file

@ -130,6 +130,7 @@ func (st *State) Print(str string, cursor int) (err option.Option[error]) {
px, py, n := CursorOffset(str, cursor+VisibleWidth(st.prompt), st.dim.Width())
RestoreCursorPosition()
ClearEndOfScreen()
if n > 0 {
NewLines(n)
@ -137,7 +138,6 @@ func (st *State) Print(str string, cursor int) (err option.Option[error]) {
SaveCursorPosition()
}
ClearEndOfScreen()
fmt.Print(str)
RestoreCursorPosition()
@ -295,11 +295,25 @@ func (st *State) BufferLen() int {
return st.buffer.Len()
}
// SavedBuffer retouren la représentation du buffer sauvegardé, si celui-ci existe.
func (st *State) SavedBuffer() (out option.Option[string]) {
if buf, ok := st.savedBuffer.Get(); ok {
out = option.Some(buf.String())
}
return
}
// Width retourne la largeur du terminal.
func (st *State) Width() int {
return st.dim.Width()
}
// Height retourn la hauteur du terminal.
func (st *State) Height() int {
return st.dim.Height()
}
// ToggleReplace se place en mode remplacement si on était on mode insertion
// et vice-versa.
func (st *State) ToggleReplace() {
@ -355,6 +369,11 @@ func (st *State) NextWord() bool {
return st.buffer.NextWord()
}
// SetCursor déplace le curseur à la position donnée
func (st *State) SetCursor(cursor int) bool {
return st.buffer.SetCursor(cursor)
}
func (st *State) rem(cb func() option.Option[string]) (ok bool) {
var yank string
if yank, ok = cb().Get(); ok {
@ -480,6 +499,79 @@ func (st *State) HistoryNext() (ok bool) {
return
}
// GetProposal retourne une suggestion de saisie pour la saisie en cours
// basée sur lhistorique.
func (st *State) GetProposal(insensitive ...bool) (proposal string) {
candidates := console.HistoryFilterPrefix(st.history, st.buffer.String(), insensitive...)
if l := len(candidates); l > 0 {
proposal = candidates[l-1]
}
return
}
// SearchMotiveNext remonte dans lhistorique de recherche.
// Lhistorique de recherche est initialisée avec le motif du buffer.
func (st *State) SearchMotiveNext(insensitive ...bool) (ok bool) {
var hs console.History
if hs, ok = st.historySearch.Get(); !ok {
motive := st.buffer.String()
hs = NewHistory(false)
candidates := console.HistoryFilterMotive(st.history, motive, insensitive...)
for _, h := range candidates {
hs.Append(h)
}
hs.SetCursor(-1)
st.historySearch = option.Some(hs)
}
if ok = hs.Next(); ok {
st.UnfocusYank()
st.UnfocusHistory()
st.SaveBuffer()
var h string
if h, ok = console.FocusedElement(hs).Get(); ok {
st.SetBuffer(h)
}
}
return
}
// SearchMotivePrev redescend dans lhistorique de recherche.
// Lhistorique de recherche est initialisée avec le motif du buffer.
func (st *State) SearchMotivePrev(insensitive ...bool) (ok bool) {
var hs console.History
if hs, ok = st.historySearch.Get(); !ok {
prefix := st.buffer.String()
hs = NewHistory(false)
candidates := console.HistoryFilterMotive(st.history, prefix, insensitive...)
for _, h := range candidates {
hs.Append(h)
}
st.historySearch = option.Some(hs)
}
if ok = hs.Prev(); ok {
st.UnfocusYank()
st.UnfocusHistory()
st.SaveBuffer()
var h string
if h, ok = console.FocusedElement(hs).Get(); ok {
st.SetBuffer(h)
}
}
return
}
// SearchHistoryNext remonte dans lhistorique de recherche.
// Lhistorique de recherche est initialisée avec le préfixe du buffer.
func (st *State) SearchHistoryNext(insensitive ...bool) (ok bool) {
@ -553,3 +645,8 @@ func (st *State) Cancel() (ok bool) {
return
}
// GetPrompt retourne le prompt configuré.
func (st *State) GetPrompt() string {
return st.prompt
}