Repository: zyedidia/micro
File: internal/util/util.go (Go)

Code before:
	// CommitHash is the commit this version was built on
	CommitHash = "Unknown"
	// CompileDate is the date this binary was compiled on
	CompileDate = "Unknown"
	// Debug logging
	Debug = "OFF"
	// FakeCursor is used to disable the terminal cursor and have micro
	// draw its own (enabled for windows consoles where the cursor is slow)
	FakeCursor = false

	// Stdout is a buffer that is written to stdout when micro closes
	Stdout *bytes.Buffer
	// Sigterm is a channel where micro exits when written
	Sigterm chan os.Signal

	// To be used for fails on (over-)write with safe writes
	ErrOverwrite = OverwriteError{}
)

// To be used for file writes before umask is applied
const FileMode os.FileMode = 0666

const fileNameLengthLimit int = 255

const backupExtensionShort string = ".backup"
const backupExtensionLong string = ".micro-backup"

const OverwriteFailMsg = `An error occurred while writing to the file:

%s

The file may be corrupted now. The good news is that it has been
successfully backed up. Next time you open this file with Micro,
Micro will ask if you want to recover it from the backup.

The backup path is:

%s`

// OverwriteError is a custom error to add additional information
type OverwriteError struct {
	What       error
	BackupName string
}

func (e OverwriteError) Error() string {
	return fmt.Sprintf(OverwriteFailMsg, e.What, e.BackupName)
}

func (e OverwriteError) Is(target error) bool {
	return target == ErrOverwrite

Review comment (suggestion):
These constants don't seem to be useful at all. Both are used in exactly one place anyway, and their names don't explain what they are for anyway. They don't seem to make the code clearer. I'd opt for keeping using hardcoded strings.

Code after:
	// CommitHash is the commit this version was built on
	CommitHash = "Unknown"
	// CompileDate is the date this binary was compiled on
	CompileDate = "Unknown"
	// Debug logging
	Debug = "OFF"
	// FakeCursor is used to disable the terminal cursor and have micro
	// draw its own (enabled for windows consoles where the cursor is slow)
	FakeCursor = false

	// Stdout is a buffer that is written to stdout when micro closes
	Stdout *bytes.Buffer
	// Sigterm is a channel where micro exits when written
	Sigterm chan os.Signal

	// To be used for fails on (over-)write with safe writes
	ErrOverwrite = OverwriteError{}
)

// To be used for file writes before umask is applied
const FileMode os.FileMode = 0666

const BackupSuffix = ".micro-backup"

const OverwriteFailMsg = `An error occurred while writing to the file:

%s

The file may be corrupted now. The good news is that it has been
successfully backed up. Next time you open this file with Micro,
Micro will ask if you want to recover it from the backup.

The backup path is:

%s`

// OverwriteError is a custom error to add additional information
type OverwriteError struct {
	What       error
	BackupName string
}

func (e OverwriteError) Error() string {
	return fmt.Sprintf(OverwriteFailMsg, e.What, e.BackupName)
}

func (e OverwriteError) Is(target error) bool {
	return target == ErrOverwrite
}

func (e OverwriteError) Unwrap() error {