From 1d6ee1439c98da584f6e52171fea94a1e8d5d4c3 Mon Sep 17 00:00:00 2001 From: Satyam Shree Date: Tue, 15 Jul 2025 12:39:22 +0530 Subject: [PATCH] Issue#9: add a build step --- cmd/app/app.go | 23 ++++++++++++++++++++--- data/fileIO.go | 4 ++-- main.go | 3 +-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/app/app.go b/cmd/app/app.go index 8521fcf..aae24fd 100644 --- a/cmd/app/app.go +++ b/cmd/app/app.go @@ -5,6 +5,9 @@ import ( "editGo/editor" "editGo/ui" tea "github.com/charmbracelet/bubbletea" + "os" + "os/exec" + "runtime" "time" ) @@ -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 @@ -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) @@ -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" + diff --git a/data/fileIO.go b/data/fileIO.go index 4aacba9..0252b38 100644 --- a/data/fileIO.go +++ b/data/fileIO.go @@ -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 } diff --git a/main.go b/main.go index e7316a9..d788762 100644 --- a/main.go +++ b/main.go @@ -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("") }