package tk
import (
)
type view struct {
prompt ui.Text
rprompt ui.Text
code ui.Text
dot int
errors []error
}
var stylingForPending = ui.Underlined
func ( *codeArea) *view {
:= .CopyState()
, , := patchPending(.Buffer, .Pending)
, := .Highlighter(.Content)
if < {
:= .Partition(, )
:= ui.StyleText([1], stylingForPending)
= ui.Concat([0], , [2])
}
var ui.Text
if !.HideRPrompt {
= .RPrompt()
}
return &view{.Prompt(), , , .Dot, }
}
func ( CodeBuffer, PendingCode) (CodeBuffer, int, int) {
if .From > .To || .From < 0 || .To > len(.Content) {
return , 0, 0
}
if .From == .To && .Content == "" {
return , 0, 0
}
:= .Content[:.From] + .Content + .Content[.To:]
:= 0
switch {
case .Dot < .From:
= .Dot
case .Dot >= .From && .Dot < .To:
= .From + len(.Content)
case .Dot >= .To:
= .Dot - (.To - .From) + len(.Content)
}
return CodeBuffer{Content: , Dot: }, .From, .From + len(.Content)
}
func ( *view, *term.BufferBuilder) {
.EagerWrap = true
.WriteStyled(.prompt)
if len(.Lines) == 1 && .Col*2 < .Width {
.Indent = .Col
}
:= .code.Partition(.dot)
.
WriteStyled([0]).
SetDotHere().
WriteStyled([1])
.EagerWrap = false
.Indent = 0
if := styledWcswidth(.rprompt); > 0 {
:= .Width - .Col -
if >= 1 {
.WriteSpaces()
.WriteStyled(.rprompt)
}
}
if len(.errors) > 0 {
for , := range .errors {
.Newline()
.Write(.Error())
}
}
}
func ( *term.Buffer, int) {
switch {
case len(.Lines) <= :
case .Dot.Line < :
.TrimToLines(0, )
default:
.TrimToLines(.Dot.Line-+1, .Dot.Line+1)
}
}
func ( ui.Text) int {
:= 0
for , := range {
+= wcwidth.Of(.Text)
}
return
}