@@ -17,6 +17,7 @@ import (
1717 "fmt"
1818 "os"
1919 "path/filepath"
20+ "runtime"
2021 "strings"
2122
2223 "github.com/atotto/clipboard"
@@ -289,11 +290,20 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
289290 a .editorPane .focused = true
290291 a .aiPane .focused = false
291292 } else {
292- // No file open, prompt for new file
293- a .pendingCodeInsert = selectedCode
294- a .showFilePrompt = true
295- a .filePromptBuffer = ""
296- a .statusMessage = "Enter filename to insert code"
293+ // No file open — load code into editor as unsaved buffer
294+ suggestedName := a .aiPane .GetSuggestedFilename ()
295+ a .editorPane .SetContentUnsaved (selectedCode , suggestedName )
296+
297+ // Switch to editor pane
298+ a .activePane = types .EditorPaneType
299+ a .editorPane .focused = true
300+ a .aiPane .focused = false
301+
302+ if suggestedName != "" {
303+ a .statusMessage = "Code loaded (suggested name: " + suggestedName + ") — Ctrl+S to save"
304+ } else {
305+ a .statusMessage = "Code loaded — Ctrl+S to save with a filename"
306+ }
297307 }
298308 } else {
299309 a .statusMessage = "No code block selected"
@@ -718,11 +728,33 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
718728 case "ctrl+s" :
719729 // Save file in editor
720730 if a .activePane == types .EditorPaneType {
721- err := a .editorPane .SaveFile ()
722- if err != nil {
723- a .statusMessage = "Error saving: " + err .Error ()
731+ if a .editorPane .currentFile == nil && a .editorPane .GetContent () != "" {
732+ // No file yet — check for AI-suggested name
733+ suggested := a .editorPane .GetSuggestedName ()
734+ if suggested != "" {
735+ // Use the suggested name directly
736+ filePath := suggested
737+ err := a .fileManager .CreateFile (filePath , a .editorPane .GetContent ())
738+ if err != nil {
739+ a .statusMessage = "Error creating file: " + err .Error ()
740+ } else {
741+ a .editorPane .LoadFile (filePath )
742+ a .statusMessage = "Saved as " + filePath
743+ }
744+ } else {
745+ // No suggestion — prompt for filename
746+ a .pendingCodeInsert = a .editorPane .GetContent ()
747+ a .showFilePrompt = true
748+ a .filePromptBuffer = ""
749+ a .statusMessage = "Enter filename to save"
750+ }
724751 } else {
725- a .statusMessage = "File saved"
752+ err := a .editorPane .SaveFile ()
753+ if err != nil {
754+ a .statusMessage = "Error saving: " + err .Error ()
755+ } else {
756+ a .statusMessage = "File saved"
757+ }
726758 }
727759 }
728760 return a , nil
@@ -738,10 +770,56 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
738770 return a , nil
739771
740772 case "ctrl+r" :
741- // Execute script (placeholder for now)
742- // TODO: Implement script execution
743- a .statusMessage = "Script execution not yet implemented"
744- return a , nil
773+ // Execute current script from editor
774+ if a .editorPane .currentFile == nil {
775+ a .statusMessage = "No file open to run"
776+ return a , nil
777+ }
778+
779+ filePath := a .editorPane .currentFile .Filepath
780+ fileType := a .editorPane .currentFile .FileType
781+
782+ // Auto-save before running
783+ if a .editorPane .HasUnsavedChanges () {
784+ if err := a .editorPane .SaveFile (); err != nil {
785+ a .statusMessage = "Save failed: " + err .Error ()
786+ return a , nil
787+ }
788+ }
789+
790+ // Build the execution command based on file type
791+ var runCmd string
792+ switch fileType {
793+ case "bash" :
794+ runCmd = "bash " + filePath
795+ case "powershell" :
796+ if runtime .GOOS == "windows" {
797+ runCmd = "powershell -NoProfile -File " + filePath
798+ } else {
799+ runCmd = "pwsh -NoProfile -File " + filePath
800+ }
801+ case "python" :
802+ runCmd = "python3 " + filePath
803+ default :
804+ // Default: try to run as shell script
805+ if runtime .GOOS == "windows" {
806+ runCmd = "powershell -NoProfile -File " + filePath
807+ } else {
808+ runCmd = "sh " + filePath
809+ }
810+ }
811+
812+ // Switch focus to AI pane and run
813+ a .activePane = types .AIPaneType
814+ a .editorPane .focused = false
815+ a .aiPane .focused = true
816+
817+ fileName := filepath .Base (filePath )
818+ a .statusMessage = "Running " + fileName + "..."
819+
820+ cmd := a .aiPane .RunScript (runCmd , fileName )
821+ cmds = append (cmds , cmd )
822+ return a , tea .Batch (cmds ... )
745823
746824 case "ctrl+enter" :
747825 // Send AI message with context from editor
0 commit comments