Skip to content

Commit

Permalink
Add save as command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kvaz1r committed Jan 19, 2020
1 parent 0470b7f commit 4503ab2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Bindables(cmdr command.Commander, driver gxui.Driver, theme *basic.Theme) [
&caret.Mover{},
&scroll.Scroller{},
focus.NewLocation(driver),
FileHook{Theme: theme},
FileHook{Theme: theme, Driver: driver},
EditHook{Theme: theme, Driver: driver},
ViewHook{},
NavHook{Commander: cmdr},
Expand Down
5 changes: 4 additions & 1 deletion command/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package command

import (
"github.com/nelsam/gxui"
"github.com/nelsam/gxui/themes/basic"
"github.com/nelsam/vidar/commander/bind"
)

type FileHook struct {
Theme *basic.Theme
Theme *basic.Theme
Driver gxui.Driver
}

func (h FileHook) Name() string {
Expand All @@ -24,6 +26,7 @@ func (h FileHook) OpName() string {
func (h FileHook) FileBindables(string) []bind.Bindable {
return []bind.Bindable{
NewSave(h.Theme),
NewSaveAs(h.Driver, h.Theme),
NewSaveAll(h.Theme),
NewCloseTab(),
&EditorRedraw{},
Expand Down
115 changes: 115 additions & 0 deletions command/save_as.go
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
}

0 comments on commit 4503ab2

Please sign in to comment.