-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// This is free and unencumbered software released into the public | ||
// domain. For more information, see <http://unlicense.org> or the | ||
// accompanying UNLICENSE file. | ||
|
||
package command | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/nelsam/gxui" | ||
"github.com/nelsam/gxui/themes/basic" | ||
"github.com/nelsam/vidar/command/focus" | ||
"github.com/nelsam/vidar/command/fs" | ||
"github.com/nelsam/vidar/commander/bind" | ||
"github.com/nelsam/vidar/plugin/status" | ||
) | ||
|
||
type Binder interface { | ||
Pop() []bind.Bindable | ||
Execute(bind.Bindable) | ||
} | ||
|
||
type FileCopySaver struct { | ||
status.General | ||
|
||
driver gxui.Driver | ||
theme *basic.Theme | ||
|
||
file *fs.Locator | ||
input <-chan gxui.Focusable | ||
|
||
focuser Focuser | ||
closer CurrentEditorCloser | ||
binder Binder | ||
} | ||
|
||
func NewSaveAs(driver gxui.Driver, theme *basic.Theme) *FileCopySaver { | ||
o := &FileCopySaver{ | ||
driver: driver, | ||
theme: theme, | ||
} | ||
o.file = fs.NewLocator(driver, theme, fs.All) | ||
return o | ||
} | ||
|
||
func (s *FileCopySaver) Name() string { | ||
return "save-as" | ||
} | ||
|
||
func (s *FileCopySaver) Menu() string { | ||
return "File" | ||
} | ||
|
||
func (s *FileCopySaver) Defaults() []fmt.Stringer { | ||
return []fmt.Stringer{gxui.KeyboardEvent{ | ||
Modifier: gxui.ModControl | gxui.ModShift, | ||
Key: gxui.KeyC, | ||
}} | ||
} | ||
|
||
func (f *FileCopySaver) Start(control gxui.Control) gxui.Control { | ||
f.file.LoadDir(control) | ||
input := make(chan gxui.Focusable, 1) | ||
f.input = input | ||
input <- f.file | ||
close(input) | ||
return nil | ||
} | ||
|
||
func (f *FileCopySaver) Next() gxui.Focusable { | ||
return <-f.input | ||
} | ||
|
||
func (f *FileCopySaver) Reset() { | ||
f.focuser = nil | ||
f.closer = nil | ||
f.binder = nil | ||
} | ||
|
||
func (f *FileCopySaver) Store(elem interface{}) bind.Status { | ||
switch src := elem.(type) { | ||
case Focuser: | ||
f.focuser = src | ||
case Binder: | ||
f.binder = src | ||
case CurrentEditorCloser: | ||
f.closer = src | ||
} | ||
|
||
if f.focuser == nil || f.binder == nil || f.closer == nil { | ||
return bind.Waiting | ||
} | ||
return bind.Executing | ||
} | ||
|
||
func (f *FileCopySaver) Exec() error { | ||
filepath := f.file.Path() | ||
if filepath == "" { | ||
return errors.New("command.FileOpener: No file path provided") | ||
} | ||
out, err := os.Create(filepath) | ||
if err != nil { | ||
return errors.New(fmt.Sprintf("Could not open %s for writing: %s", filepath, err)) | ||
} | ||
if _, err := out.WriteString(f.closer.CurrentEditor().Text()); err != nil { | ||
return errors.New(fmt.Sprintf("Could not write to file %s: %s", filepath, err)) | ||
} | ||
out.Close() | ||
f.closer.CloseCurrentEditor() | ||
f.binder.Pop() // should there be check on nil? | ||
f.binder.Execute(f.focuser.For(focus.Path(filepath))) | ||
return nil | ||
} |