Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"editGo/editor"
"editGo/ui"
tea "github.com/charmbracelet/bubbletea"
"os"
"os/exec"
"runtime"
"time"
)

Expand All @@ -23,11 +26,9 @@ func NewModel(filePath string) Model {

if filePath != "" {
file, err = data.NewFile(filePath)
} else {
file, err = data.NewEmptyFile()
}
if err != nil {
panic(err)
file, _ = data.NewEmptyFile(filePath)
}

buffer := file.Buffer
Expand Down Expand Up @@ -87,6 +88,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.Cursor.X = 0
case msg.Type == tea.KeyCtrlQ, msg.Type == tea.KeyCtrlC:
m.AutoSaver.Stop()
clearTerminal()
return m, tea.Quit
case msg.Type == tea.KeyUp:
m.Cursor.MoveUp(m.Buffer)
Expand Down Expand Up @@ -119,6 +121,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

func clearTerminal() {
var cmd *exec.Cmd

switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd", "/c", "cls")
default: // Unix-like (Linux, macOS, etc.)
cmd = exec.Command("clear")
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
}

func (m Model) View() string {
return ui.RenderStatusBar(m.File.FilePath, m.Buffer.IsDirty(), m.Cursor.X, m.Cursor.Y) + "\n" +
ui.RenderBuffer(m.Buffer.Lines, m.Cursor.X, m.Cursor.Y) + "\n" +
Expand Down
4 changes: 2 additions & 2 deletions data/fileIO.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func NewFile(filePath string) (*FileManager, error) {
return fm, nil
}

func NewEmptyFile() (*FileManager, error) {
func NewEmptyFile(filePath string) (*FileManager, error) {
buffer := editor.NewTextBuffer()
fm := &FileManager{
Buffer: buffer,
FilePath: "",
FilePath: filePath,
}
return fm, nil
}
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func init() {
func main() {
var model app.Model
if len(os.Args) > 1 {
model = app.NewModel(os.Args[1]) // empty fil

model = app.NewModel(os.Args[1])
} else {
model = app.NewModel("")
}
Expand Down
Loading