github.com/jmigpin/editor@v1.6.0/util/iout/iorw/rwedit/ctx.go (about) 1 package rwedit 2 3 import ( 4 "fmt" 5 "image" 6 7 "github.com/jmigpin/editor/util/iout" 8 "github.com/jmigpin/editor/util/iout/iorw" 9 "github.com/jmigpin/editor/util/uiutil/event" 10 ) 11 12 //godebug:annotatefile 13 14 type Ctx struct { 15 RW iorw.ReadWriterAt 16 C Cursor 17 Fns CtxFns 18 } 19 20 func NewCtx() *Ctx { 21 ctx := &Ctx{C: &SimpleCursor{}, Fns: EmptyCtxFns()} 22 return ctx 23 } 24 25 func (ctx *Ctx) CursorSelectionLinesIndexes() (int, int, bool, error) { 26 a, b, ok := ctx.C.SelectionIndexes() 27 if !ok { 28 a = ctx.C.Index() 29 b = a 30 } 31 rd := ctx.LocalReader2(a, b) 32 return iorw.LinesIndexes(rd, a, b) 33 } 34 35 func (ctx *Ctx) Selection() ([]byte, bool) { 36 a, b, ok := ctx.C.SelectionIndexes() 37 if !ok { 38 return nil, false 39 } 40 w, err := ctx.RW.ReadFastAt(a, b-a) 41 if err != nil { 42 return nil, false 43 } 44 return iout.CopyBytes(w), true 45 } 46 47 func (ctx *Ctx) LocalReader(i int) iorw.ReaderAt { 48 return ctx.LocalReader2(i, i) 49 } 50 func (ctx *Ctx) LocalReader2(min, max int) iorw.ReaderAt { 51 pad := 2500 52 return iorw.NewLimitedReaderAtPad(ctx.RW, min, max, pad) 53 } 54 55 //---------- 56 57 type CtxFns struct { 58 Error func(error) 59 60 GetPoint func(int) image.Point 61 GetIndex func(image.Point) int 62 LineHeight func() int 63 LineCommentStr func() string 64 MakeIndexVisible func(int) 65 PageUp func(up bool) 66 ScrollUp func(up bool) 67 68 SetClipboardData func(event.ClipboardIndex, string) 69 GetClipboardData func(event.ClipboardIndex, func(string, error)) // setter should wrap fn to run on ui goroutine 70 71 Undo func() error 72 Redo func() error 73 } 74 75 func EmptyCtxFns() CtxFns { 76 u := CtxFns{} 77 78 u.Error = func(err error) { fmt.Println(err) } 79 80 u.GetPoint = func(int) image.Point { return image.ZP } 81 u.GetIndex = func(image.Point) int { return 0 } 82 u.LineHeight = func() int { return 0 } 83 u.LineCommentStr = func() string { return "" } 84 u.MakeIndexVisible = func(int) {} 85 u.PageUp = func(bool) {} 86 u.ScrollUp = func(bool) {} 87 88 u.SetClipboardData = func(event.ClipboardIndex, string) {} 89 u.GetClipboardData = func(event.ClipboardIndex, func(string, error)) {} 90 91 u.Undo = func() error { return nil } 92 u.Redo = func() error { return nil } 93 94 return u 95 }