mirror of
https://github.com/EnumeratedDev/Typer.git
synced 2025-07-01 07:48:20 +00:00
Move and rename drawCurrentBuffer function
This commit is contained in:
parent
cc3b4ecf18
commit
51ec45cd14
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -29,6 +30,52 @@ type Selection struct {
|
||||
var Buffers = make(map[int]*Buffer)
|
||||
var LastBufferId int
|
||||
|
||||
func drawBuffer(window *Window) {
|
||||
buffer := window.CurrentBuffer
|
||||
|
||||
x, y, _, _ := window.GetTextAreaDimensions()
|
||||
|
||||
bufferX, bufferY, _, _ := window.GetTextAreaDimensions()
|
||||
|
||||
for i, r := range buffer.Contents + " " {
|
||||
if x-buffer.OffsetX >= bufferX && y-buffer.OffsetY >= bufferY {
|
||||
// Default style
|
||||
style := tcell.StyleDefault.Background(CurrentStyle.BufferAreaBg).Foreground(CurrentStyle.BufferAreaFg)
|
||||
|
||||
// Change background if under cursor
|
||||
if i == buffer.CursorPos {
|
||||
style = style.Background(CurrentStyle.BufferAreaSel)
|
||||
}
|
||||
|
||||
// Change background if selected
|
||||
if buffer.Selection != nil {
|
||||
if edge1, edge2 := buffer.GetSelectionEdges(); i >= edge1 && i <= edge2 {
|
||||
style = style.Background(CurrentStyle.BufferAreaSel)
|
||||
|
||||
// Show selection on entire tab space
|
||||
if r == '\t' {
|
||||
for j := 0; j < int(Config.TabIndentation); j++ {
|
||||
window.screen.SetContent(x+j-buffer.OffsetX, y-buffer.OffsetY, r, nil, style)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.screen.SetContent(x-buffer.OffsetX, y-buffer.OffsetY, r, nil, style)
|
||||
}
|
||||
|
||||
// Change position for next character
|
||||
if r == '\n' {
|
||||
x = bufferX
|
||||
y++
|
||||
} else if r == '\t' {
|
||||
x += int(Config.TabIndentation)
|
||||
} else {
|
||||
x++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (buffer *Buffer) Load() error {
|
||||
// Do not load if canSave is false or filename is not set
|
||||
if !buffer.canSave || buffer.filename == "" {
|
||||
|
@ -82,52 +82,6 @@ func CreateWindow() (*Window, error) {
|
||||
return &window, nil
|
||||
}
|
||||
|
||||
func (window *Window) drawCurrentBuffer() {
|
||||
buffer := window.CurrentBuffer
|
||||
|
||||
x, y, _, _ := window.GetTextAreaDimensions()
|
||||
|
||||
bufferX, bufferY, _, _ := window.GetTextAreaDimensions()
|
||||
|
||||
for i, r := range buffer.Contents + " " {
|
||||
if x-buffer.OffsetX >= bufferX && y-buffer.OffsetY >= bufferY {
|
||||
// Default style
|
||||
style := tcell.StyleDefault.Background(CurrentStyle.BufferAreaBg).Foreground(CurrentStyle.BufferAreaFg)
|
||||
|
||||
// Change background if under cursor
|
||||
if i == buffer.CursorPos {
|
||||
style = style.Background(CurrentStyle.BufferAreaSel)
|
||||
}
|
||||
|
||||
// Change background if selected
|
||||
if buffer.Selection != nil {
|
||||
if edge1, edge2 := buffer.GetSelectionEdges(); i >= edge1 && i <= edge2 {
|
||||
style = style.Background(CurrentStyle.BufferAreaSel)
|
||||
|
||||
// Show selection on entire tab space
|
||||
if r == '\t' {
|
||||
for j := 0; j < int(Config.TabIndentation); j++ {
|
||||
window.screen.SetContent(x+j-buffer.OffsetX, y-buffer.OffsetY, r, nil, style)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.screen.SetContent(x-buffer.OffsetX, y-buffer.OffsetY, r, nil, style)
|
||||
}
|
||||
|
||||
// Change position for next character
|
||||
if r == '\n' {
|
||||
x = bufferX
|
||||
y++
|
||||
} else if r == '\t' {
|
||||
x += int(Config.TabIndentation)
|
||||
} else {
|
||||
x++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) Draw() {
|
||||
// Clear screen
|
||||
window.screen.Clear()
|
||||
@ -144,7 +98,7 @@ func (window *Window) Draw() {
|
||||
|
||||
// Draw current buffer
|
||||
if window.CurrentBuffer != nil {
|
||||
window.drawCurrentBuffer()
|
||||
drawBuffer(window)
|
||||
}
|
||||
|
||||
// Draw input bar
|
||||
|
Loading…
x
Reference in New Issue
Block a user