<commit_before>// This is free and unencumbered software released into the public
// domain.  For more information, see <http://unlicense.org> or the
// accompanying UNLICENSE file.

package commands

import (
	"fmt"
	"os"
	"strings"

	"github.com/nelsam/gxui"
	"github.com/nelsam/vidar/editor"
)

type CurrentFileFinder interface {
	CurrentEditor() *editor.CodeEditor
	CurrentFile() string
}

type SaveCurrent struct {
	statusKeeper

	filepath string
}

func NewSave(theme gxui.Theme) *SaveCurrent {
	return &SaveCurrent{
		statusKeeper: statusKeeper{theme: theme},
	}
}

func (s *SaveCurrent) Name() string {
	return "save-current-file"
}

func (s *SaveCurrent) Exec(target interface{}) (executed, consume bool) {
	finder, ok := target.(CurrentFileFinder)
	if !ok {
		return false, false
	}
	editor := finder.CurrentEditor()
	if editor == nil {
		return true, true
	}
	s.filepath = finder.CurrentFile()
	if !editor.LastKnownMTime().IsZero() {
		finfo, err := os.Stat(s.filepath)
		if err != nil {
			s.err = fmt.Sprintf("Could not stat file %s: %s", s.filepath, err)
			return true, false
		}
		if finfo.ModTime().After(editor.LastKnownMTime()) {
			// TODO: prompt for override
			s.err = fmt.Sprintf("File %s changed on disk.  Cowardly refusing to overwrite.", s.filepath)
			return true, false
		}
	}
	f, err := os.Create(s.filepath)
	if err != nil {
		s.err = fmt.Sprintf("Could not open %s for writing: %s", s.filepath, err)
		return true, false
	}
	defer f.Close()
	if !strings.HasSuffix(editor.Text(), "\n") {
		editor.SetText(editor.Text() + "\n")
	}
	if _, err := f.WriteString(editor.Text()); err != nil {
		s.err = fmt.Sprintf("Could not write to file %s: %s", s.filepath, err)
		return true, false
	}
	editor.FlushedChanges()
	return true, true
}<commit_msg>Add back save success message<commit_after>// This is free and unencumbered software released into the public
// domain.  For more information, see <http://unlicense.org> or the
// accompanying UNLICENSE file.

package commands

import (
	"fmt"
	"os"
	"strings"

	"github.com/nelsam/gxui"
	"github.com/nelsam/vidar/editor"
)

type CurrentFileFinder interface {
	CurrentEditor() *editor.CodeEditor
	CurrentFile() string
}

type SaveCurrent struct {
	statusKeeper

	filepath string
}

func NewSave(theme gxui.Theme) *SaveCurrent {
	return &SaveCurrent{
		statusKeeper: statusKeeper{theme: theme},
	}
}

func (s *SaveCurrent) Name() string {
	return "save-current-file"
}

func (s *SaveCurrent) Exec(target interface{}) (executed, consume bool) {
	finder, ok := target.(CurrentFileFinder)
	if !ok {
		return false, false
	}
	editor := finder.CurrentEditor()
	if editor == nil {
		return true, true
	}
	s.filepath = finder.CurrentFile()
	if !editor.LastKnownMTime().IsZero() {
		finfo, err := os.Stat(s.filepath)
		if err != nil {
			s.err = fmt.Sprintf("Could not stat file %s: %s", s.filepath, err)
			return true, false
		}
		if finfo.ModTime().After(editor.LastKnownMTime()) {
			// TODO: prompt for override
			s.err = fmt.Sprintf("File %s changed on disk.  Cowardly refusing to overwrite.", s.filepath)
			return true, false
		}
	}
	f, err := os.Create(s.filepath)
	if err != nil {
		s.err = fmt.Sprintf("Could not open %s for writing: %s", s.filepath, err)
		return true, false
	}
	defer f.Close()
	if !strings.HasSuffix(editor.Text(), "\n") {
		editor.SetText(editor.Text() + "\n")
	}
	if _, err := f.WriteString(editor.Text()); err != nil {
		s.err = fmt.Sprintf("Could not write to file %s: %s", s.filepath, err)
		return true, false
	}
	s.info = fmt.Sprintf("Successfully saved %s", s.filepath)
	editor.FlushedChanges()
	return true, true
}
{'fraction_non_alphanumeric': 0.14258082343188727, 'fraction_numerical': 0.0, 'mean_word_length': 3.8721399730820996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5956032', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1270, 'n_words': 355}