Compare commits

..

No commits in common. "2d49f84d6f2cc3bdeaa5b10f3a8837ddc5860727" and "cc3b4ecf188b53b0c297e48eb7a4d89b79742dcf" have entirely different histories.

10 changed files with 99 additions and 155 deletions

View File

@ -1,20 +0,0 @@
# Typer Text Editor
### A simple and easy to use text editor written in Go
| Default Style | Classic Style |
|:----------------------------------------------------------------:|:----------------------------------------------------------------:|
| ![Example of the Typer's default style](media/default-style.png) | ![Example of the Typer's classic style](media/classic-style.png) |
### Installation
#### From source:
- Download `go` from your package manager or from the go website
- Downlaod `which` from your package manager
- Download `make` from your package manager
- Run the following command to compile Typer
```shell
make
```
- Run the following command **with superuser privileges** to install Typer to your system
```shell
make install SYSCONFDIR=/etc
```

View File

@ -22,7 +22,7 @@ keybindings:
command: "prev-buffer"
- keybinding: "PgDn"
cursor_modes: ["buffer"]
command: "next-buffer"
command: "prev-buffer"
- keybinding: "Ctrl-N"
cursor_modes: ["buffer"]
command: "new-buffer"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

View File

@ -2,13 +2,13 @@ package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"os"
"path/filepath"
"strings"
)
type Buffer struct {
Id int
Name string
Contents string
@ -26,71 +26,8 @@ type Selection struct {
selectionEnd int
}
var Buffers = make([]*Buffer, 0)
func GetBufferByName(name string) *Buffer {
for _, buffer := range Buffers {
if buffer.Name == name {
return buffer
}
}
return nil
}
func GetBufferByFilename(filename string) *Buffer {
for _, buffer := range Buffers {
if buffer.filename == filename {
return buffer
}
}
return nil
}
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++
}
}
}
var Buffers = make(map[int]*Buffer)
var LastBufferId int
func (buffer *Buffer) Load() error {
// Do not load if canSave is false or filename is not set
@ -220,15 +157,8 @@ func CreateFileBuffer(filename string, openNonExistentFile bool) (*Buffer, error
}
}
if GetBufferByName(filename) != nil {
return nil, fmt.Errorf("a buffer with the name (%s) is already open", filename)
}
if GetBufferByFilename(abs) != nil {
return nil, fmt.Errorf("%s is already open in another buffer", filename)
}
buffer := Buffer{
Id: LastBufferId + 1,
Name: filename,
Contents: "",
CursorPos: 0,
@ -245,13 +175,15 @@ func CreateFileBuffer(filename string, openNonExistentFile bool) (*Buffer, error
}
}
Buffers = append(Buffers, &buffer)
Buffers[buffer.Id] = &buffer
LastBufferId++
return &buffer, nil
}
func CreateBuffer(bufferName string) (*Buffer, error) {
func CreateBuffer(bufferName string) *Buffer {
buffer := Buffer{
Id: LastBufferId + 1,
Name: bufferName,
Contents: "",
CursorPos: 0,
@ -259,11 +191,8 @@ func CreateBuffer(bufferName string) (*Buffer, error) {
filename: "",
}
if GetBufferByName(bufferName) != nil {
return nil, fmt.Errorf("a buffer with the name (%s) is already open", bufferName)
}
Buffers[buffer.Id] = &buffer
LastBufferId++
Buffers = append(Buffers, &buffer)
return &buffer, nil
return &buffer
}

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"log"
"maps"
"slices"
"strconv"
"strings"
)
@ -140,15 +140,15 @@ func initCommands() {
return
}
index := slices.Index(Buffers, window.CurrentBuffer)
buffers := slices.Collect(maps.Values(Buffers))
index := slices.Index(buffers, window.CurrentBuffer)
index--
if index < 0 {
index = 0
}
window.CurrentBuffer = Buffers[index]
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
window.CurrentBuffer = buffers[index]
},
}
@ -159,50 +159,44 @@ func initCommands() {
return
}
index := slices.Index(Buffers, window.CurrentBuffer)
buffers := slices.Collect(maps.Values(Buffers))
index := slices.Index(buffers, window.CurrentBuffer)
index++
if index >= len(Buffers) {
index = len(Buffers) - 1
if index >= len(buffers) {
index = len(buffers) - 1
}
window.CurrentBuffer = Buffers[index]
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
window.CurrentBuffer = buffers[index]
},
}
newBufferCmd := Command{
cmd: "new-buffer",
run: func(window *Window, args ...string) {
for i := 1; true; i++ {
buffer, err := CreateBuffer("New Buffer " + strconv.Itoa(i))
if err == nil {
window.CurrentBuffer = buffer
break
number := 1
for _, buffer := range Buffers {
if strings.HasPrefix(buffer.Name, "New File ") {
number++
}
}
buffer := CreateBuffer(fmt.Sprintf("New File %d", number))
window.CurrentBuffer = buffer
window.CursorMode = CursorModeBuffer
PrintMessage(window, fmt.Sprintf("New buffer created with the name '%s'.", window.CurrentBuffer.Name))
},
}
closeBufferCmd := Command{
cmd: "close-buffer",
run: func(window *Window, args ...string) {
bufferIndex := slices.Index(Buffers, window.CurrentBuffer)
Buffers = DeleteFromSlice(Buffers, bufferIndex)
if len(Buffers) == 0 {
delete(Buffers, window.CurrentBuffer.Id)
buffersSlice := slices.Collect(maps.Values(Buffers))
if len(buffersSlice) == 0 {
window.Close()
return
}
if bufferIndex >= len(Buffers) {
window.CurrentBuffer = Buffers[bufferIndex-1]
} else {
window.CurrentBuffer = Buffers[bufferIndex]
}
window.CurrentBuffer = buffersSlice[0]
window.CursorMode = CursorModeBuffer
PrintMessage(window, "Buffer closed.")
},
}

View File

@ -24,16 +24,16 @@ func main() {
}
if len(os.Args) > 1 {
for i, file := range os.Args[1:] {
for _, file := range os.Args[1:] {
b, err := CreateFileBuffer(file, true)
if err != nil {
PrintMessage(window, "Could not open file: "+file)
continue
}
if i == 0 {
if window.CurrentBuffer.Name == "New File 1" {
delete(Buffers, window.CurrentBuffer.Id)
window.CurrentBuffer = b
Buffers = Buffers[1:]
}
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gdamore/tcell/v2"
"path/filepath"
"slices"
"strconv"
"strings"
)
@ -81,17 +82,27 @@ func initTopMenu() {
}
buffersSlice := make([]string, 0)
for i, buffer := range Buffers {
for _, buffer := range Buffers {
if window.CurrentBuffer == buffer {
buffersSlice = append(buffersSlice, fmt.Sprintf("[%d] * %s", i+1, buffer.Name))
buffersSlice = append(buffersSlice, fmt.Sprintf("[%d] * %s", buffer.Id, buffer.Name))
} else {
buffersSlice = append(buffersSlice, fmt.Sprintf("[%d] %s", i+1, buffer.Name))
buffersSlice = append(buffersSlice, fmt.Sprintf("[%d] %s", buffer.Id, buffer.Name))
}
}
slices.Sort(buffersSlice)
d := CreateDropdownMenu(buffersSlice, 0, y, 0, func(i int) {
window.CurrentBuffer = Buffers[i]
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
start := strings.Index(buffersSlice[i], "[")
end := strings.Index(buffersSlice[i], "]")
id, err := strconv.Atoi(buffersSlice[i][start+1 : end])
if err != nil {
PrintMessage(window, fmt.Sprintf("Cannot convert buffer id '%s' to int", buffersSlice[i][start:end]))
return
}
window.CurrentBuffer = Buffers[id]
ClearDropdowns()
window.CursorMode = CursorModeBuffer
})

View File

@ -53,15 +53,3 @@ func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
drawText(s, x1+1, y1+1, x2-1, y2-1, style, " ")
}
func DeleteFromSlice[T any](slice []T, i int) []T {
if i >= len(slice) {
return slice
} else if i < 0 {
return slice
} else if i == len(slice)-1 {
return slice[:len(slice)-1]
} else {
return append(slice[:i], slice[i+1:]...)
}
}

View File

@ -4,7 +4,6 @@ import (
"github.com/gdamore/tcell/v2"
"log"
"slices"
"strconv"
"strings"
"time"
"unicode"
@ -53,11 +52,8 @@ func CreateWindow() (*Window, error) {
}
// Create empty buffer if nil
for i := 1; window.CurrentBuffer == nil; i++ {
buffer, err := CreateBuffer("New Buffer " + strconv.Itoa(i))
if err == nil {
window.CurrentBuffer = buffer
}
if window.CurrentBuffer == nil {
window.CurrentBuffer = CreateBuffer("New File 1")
}
// Create tcell screen
@ -86,6 +82,52 @@ 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()
@ -102,7 +144,7 @@ func (window *Window) Draw() {
// Draw current buffer
if window.CurrentBuffer != nil {
drawBuffer(window)
window.drawCurrentBuffer()
}
// Draw input bar