Initial Commit

This commit is contained in:
EnumDev 2025-06-03 16:59:39 +03:00
commit 6b5e6e8996
12 changed files with 602 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Build directory
/build/
# IDE files
.idea

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 EnumDev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
# Installation paths
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
SYSCONFDIR := $(PREFIX)/etc
# Compilers and tools
GO ?= $(shell which go)
build:
mkdir -p build
cd src/; $(GO) build -ldflags "-w" -o ../build/typer
install: build/typer
# Create directories
install -dm755 $(DESTDIR)$(BINDIR)
# Install files
install -Dm755 build/typer $(DESTDIR)$(BINDIR)/typer
uninstall:
rm $(DESTDIR)$(BINDIR)/typer
clean:
rm -r build/
.PHONY: build

87
src/buffer.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
type Buffer struct {
Id int
Name string
Contents string
LoadFunc func(buffer *Buffer) error
SaveFunc func(buffer *Buffer) error
}
var Buffers = make(map[int]*Buffer)
var LastBufferId int
func CreateFileBuffer(filename string) (*Buffer, error) {
// Replace tilde with home directory
if filename != "~" && strings.HasPrefix(filename, "~/") {
homedir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
filename = filepath.Join(homedir, filename[2:])
}
stat, err := os.Stat(filename)
if err != nil {
return nil, err
}
if !stat.Mode().IsRegular() {
return nil, fmt.Errorf("%s is not a regular file", filename)
}
buffer := Buffer{
Id: LastBufferId + 1,
Name: filename,
Contents: "",
LoadFunc: func(buffer *Buffer) error {
content, err := os.ReadFile(filename)
if err != nil {
return err
}
buffer.Contents = string(content)
return nil
},
SaveFunc: func(buffer *Buffer) error {
err := os.WriteFile(filename, []byte(buffer.Contents), 0644)
if err != nil {
return err
}
return nil
},
}
err = buffer.LoadFunc(&buffer)
if err != nil {
return nil, err
}
Buffers[buffer.Id] = &buffer
return &buffer, nil
}
func CreateBuffer(bufferName string) *Buffer {
buffer := Buffer{
Id: LastBufferId + 1,
Name: bufferName,
Contents: "\n",
LoadFunc: func(buffer *Buffer) error { return nil },
SaveFunc: func(buffer *Buffer) error { return nil },
}
Buffers[buffer.Id] = &buffer
return &buffer
}

13
src/go.mod Normal file
View File

@ -0,0 +1,13 @@
module Typer
go 1.24
require (
github.com/gdamore/encoding v1.0.1 // indirect
github.com/gdamore/tcell v1.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
)

59
src/go.sum Normal file
View File

@ -0,0 +1,59 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU=
github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

26
src/line_index.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"github.com/gdamore/tcell"
"strconv"
"strings"
)
func drawLineIndex(window *Window) {
screen := window.screen
buffer := window.textArea.CurrentBuffer
lineIndexStyle := tcell.StyleDefault.Foreground(tcell.ColorDimGray).Background(tcell.Color236)
_, sizeY := screen.Size()
y := 0
if window.ShowTopMenu {
y = 1
}
for lineIndex := 1; lineIndex <= strings.Count(buffer.Contents, "\n") && lineIndex < sizeY; lineIndex++ {
drawText(screen, 0, y, 3, y, lineIndexStyle, strconv.Itoa(lineIndex)+". ")
y++
}
}

16
src/main.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"log"
)
func main() {
window, err := CreateWindow(nil)
if err != nil {
log.Fatalf("Failed to create window: %v", err)
}
for window.screen != nil {
window.Draw()
}
}

53
src/message_bar.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"github.com/gdamore/tcell"
"time"
)
type TyperMessage struct {
timestamp int64
message string
}
var messageLog = make([]TyperMessage, 0)
func PrintMessage(window *Window, message string) {
messageLog = append(messageLog, TyperMessage{timestamp: time.Now().UnixMilli(), message: message})
err := window.screen.PostEvent(tcell.NewEventInterrupt(nil))
if err != nil {
return
}
go func() {
time.Sleep(5 * time.Second)
err := window.screen.PostEvent(tcell.NewEventInterrupt(nil))
if err != nil {
return
}
}()
}
func drawMessageBar(window *Window) {
screen := window.screen
messageBarStyle := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)
sizeX, sizeY := screen.Size()
messageToPrint := ""
if len(messageLog) > 0 && time.Since(time.UnixMilli(messageLog[len(messageLog)-1].timestamp)).Seconds() < 5 {
messageToPrint = messageLog[len(messageLog)-1].message
}
for x := 0; x < sizeX; x++ {
char := ' '
if x < len(messageToPrint) {
char = int32(messageToPrint[x])
}
screen.SetContent(x, sizeY-1, char, nil, messageBarStyle)
}
}

46
src/top_menu.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"github.com/gdamore/tcell"
)
type TopMenuButton struct {
Name string
}
var TopMenuButtons = make([]TopMenuButton, 0)
func initTopMenu() {
// Buttons
fileButton := TopMenuButton{
Name: "File",
}
EditButton := TopMenuButton{
Name: "Edit",
}
Buffers := TopMenuButton{
Name: "Buffers",
}
// Append buttons
TopMenuButtons = append(TopMenuButtons, fileButton, EditButton, Buffers)
}
func drawTopMenu(window *Window) {
screen := window.screen
topMenuStyle := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)
sizeX, _ := screen.Size()
for x := 0; x < sizeX; x++ {
screen.SetContent(x, 0, ' ', nil, topMenuStyle)
}
currentX := 1
for _, button := range TopMenuButtons {
drawText(screen, currentX, 0, currentX+len(button.Name), 0, topMenuStyle, button.Name)
currentX += len(button.Name) + 1
}
}

19
src/utils.go Normal file
View File

@ -0,0 +1,19 @@
package main
import "github.com/gdamore/tcell"
func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
row := y1
col := x1
for _, r := range []rune(text) {
s.SetContent(col, row, r, nil, style)
col++
if col >= x2 {
row++
col = x1
}
if row > y2 {
break
}
}
}

232
src/window.go Normal file
View File

@ -0,0 +1,232 @@
package main
import (
"github.com/gdamore/tcell"
"log"
)
type Window struct {
ShowTopMenu bool
ShowLineIndex bool
textArea TextArea
screen tcell.Screen
}
type TextArea struct {
CursorPos int
CurrentBuffer *Buffer
}
func CreateWindow(initialBuffer *Buffer) (*Window, error) {
window := Window{
ShowTopMenu: true,
ShowLineIndex: true,
textArea: TextArea{
CursorPos: 0,
CurrentBuffer: initialBuffer,
},
screen: nil,
}
// Create empty buffer if nil
if window.textArea.CurrentBuffer == nil {
window.textArea.CurrentBuffer = CreateBuffer("New File")
}
// Create tcell screen
screen, err := tcell.NewScreen()
if err != nil {
log.Fatalf("Failed to initialize tcell: %s", err)
}
if err := screen.Init(); err != nil {
log.Fatalf("Failed to initialize screen: %s", err)
}
// Set screen style
screen.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.Color234))
// Set window screen field
window.screen = screen
// Initialize top menu
initTopMenu()
return &window, nil
}
func (window *Window) drawCurrentBuffer() {
buffer := window.textArea.CurrentBuffer
x, y := 0, 0
if window.ShowTopMenu {
y++
}
if window.ShowLineIndex {
x += 3
}
width, _ := window.screen.Size()
for _, r := range []rune(buffer.Contents) {
if x >= width || r == '\n' {
x = 0
if window.ShowLineIndex {
x += 3
}
y++
}
window.screen.SetContent(x, y, r, nil, tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.Color234))
if r != '\n' {
x++
}
}
}
func (window *Window) Draw() {
// Clear screen
window.screen.Clear()
// Draw top menu
if window.ShowTopMenu {
drawTopMenu(window)
}
// Draw line index
if window.ShowLineIndex {
drawLineIndex(window)
}
// Draw current buffer
if window.textArea.CurrentBuffer != nil {
window.drawCurrentBuffer()
}
// Draw message bar
drawMessageBar(window)
// Draw cursor
window.screen.ShowCursor(window.GetAbsoluteCursorPos())
// Update screen
window.screen.Show()
// Poll event
ev := window.screen.PollEvent()
// Process event
switch ev := ev.(type) {
case *tcell.EventResize:
window.screen.Sync()
case *tcell.EventKey:
// Navigation Keys
if ev.Key() == tcell.KeyRight {
window.SetCursorPos(window.textArea.CursorPos + 1)
} else if ev.Key() == tcell.KeyLeft {
window.SetCursorPos(window.textArea.CursorPos - 1)
} else if ev.Key() == tcell.KeyUp {
x, y := window.GetCursorPos2D()
window.SetCursorPos2D(x, y-1)
} else if ev.Key() == tcell.KeyDown {
x, y := window.GetCursorPos2D()
window.SetCursorPos2D(x, y+1)
}
// Exit key
if ev.Key() == tcell.KeyCtrlC {
window.Close()
}
}
}
func (window *Window) Close() {
window.screen.Fini()
window.screen = nil
}
func (window *Window) GetTextAreaDimensions() (int, int, int, int) {
x1, y1 := 0, 0
x2, y2 := window.screen.Size()
if window.ShowTopMenu {
y1++
}
if window.ShowLineIndex {
x1 += 3
}
return x1, y1, x2, y2
}
func (window *Window) GetAbsoluteCursorPos() (int, int) {
cursorX, cursorY := window.GetCursorPos2D()
x1, y1, _, _ := window.GetTextAreaDimensions()
cursorX += x1
cursorY += y1
return cursorX, cursorY
}
func (window *Window) GetCursorPos2D() (int, int) {
cursorX := 0
cursorY := 0
for i := 0; i < window.textArea.CursorPos; i++ {
char := window.textArea.CurrentBuffer.Contents[i]
if char == '\n' {
cursorY++
cursorX = 0
} else {
cursorX++
}
}
return cursorX, cursorY
}
func (window *Window) SetCursorPos(position int) {
window.textArea.CursorPos = position
if window.textArea.CursorPos < 0 {
window.textArea.CursorPos = 0
}
if window.textArea.CursorPos > len(window.textArea.CurrentBuffer.Contents)-1 {
window.textArea.CursorPos = len(window.textArea.CurrentBuffer.Contents) - 1
}
}
func (window *Window) SetCursorPos2D(x, y int) {
x = max(x, 0)
y = max(y, 0)
lines := make([]struct {
charIndex int
str string
}, 0)
var str string
for i, char := range window.textArea.CurrentBuffer.Contents {
str += string(char)
if char == '\n' {
lines = append(lines, struct {
charIndex int
str string
}{charIndex: i - len(str) + 1, str: str})
str = ""
}
}
y = min(y, len(lines)-1)
x = min(x, len(lines[y].str)-1)
window.SetCursorPos(lines[y].charIndex + x)
}