Skip to content

Commit

Permalink
Rename OSCommand to Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed May 3, 2020
1 parent 7fd50e6 commit 78b915d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
4 changes: 2 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/sirupsen/logrus"

"github.com/nakabonne/golintui/pkg/config"
"github.com/nakabonne/golintui/pkg/editor"
"github.com/nakabonne/golintui/pkg/golangcilint"
"github.com/nakabonne/golintui/pkg/gui"
"github.com/nakabonne/golintui/pkg/oscommand"
)

type App struct {
Expand All @@ -25,7 +25,7 @@ type App struct {
func New(conf *config.Config) (*App, error) {
logger := newLogger(conf)
runner := golangcilint.NewRunner(conf.Executable, []string{}, logger)
command := oscommand.NewOSCommand(conf.OpenCommandEnv, logger)
command := editor.NewEditor(conf.OpenCommandEnv, logger)
return &App{
closers: []io.Closer{},
Config: conf,
Expand Down
51 changes: 18 additions & 33 deletions pkg/oscommand/oscommand.go → pkg/editor/editor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oscommand
package editor

import (
"fmt"
Expand All @@ -9,34 +9,32 @@ import (
"github.com/sirupsen/logrus"
)

type OSCommand struct {
OpenCommandEnv string
Logger *logrus.Entry
type Editor struct {
OpenCommandEnvKey string
Logger *logrus.Entry
}

func NewOSCommand(openCommandEnv string, logger *logrus.Entry) *OSCommand {
return &OSCommand{OpenCommandEnv: openCommandEnv, Logger: logger}
func NewEditor(openCommandEnv string, logger *logrus.Entry) *Editor {
return &Editor{OpenCommandEnvKey: openCommandEnv, Logger: logger}
}

// OpenFileAtLineColumn opens a file at a specific line and column.
func (o *OSCommand) OpenFileAtLineColumn(filename string, line, column int) error {
command := specifyLineColumn(o.openCommand(), filename, line, column)
//_, err := o.runCommand(command[0], command[1:]...)
return o.runSubprocess(command[0], command[1:]...)
//return err
func (e *Editor) OpenFileAtLineColumn(filename string, line, column int) error {
command := specifyLineColumn(e.openCommand(), filename, line, column)
return e.run(command[0], command[1:]...)
}

// openCommand returns an executable editor command.
// Falling back to environment variable for golintui, EDITOR then vi.
func (o *OSCommand) openCommand() string {
executable := os.Getenv(o.OpenCommandEnv)
func (e *Editor) openCommand() string {
executable := os.Getenv(e.OpenCommandEnvKey)
if executable == "" {
executable = os.Getenv("EDITOR")
}
if executable == "" {
vi, err := exec.LookPath("vi")
if err != nil {
o.Logger.Error("failed to get path to vi", err)
e.Logger.Error("failed to get path to vi", err)
}
executable = vi
}
Expand Down Expand Up @@ -75,34 +73,21 @@ func specifyLineColumn(command, filename string, line, column int) []string {
return res
}

func (o *OSCommand) runCommand(executable string, args ...string) ([]byte, error) {
cmd := exec.Command(executable, args...)
return cmd.CombinedOutput()
}

func (o *OSCommand) runSubprocess(executable string, args ...string) error {
func (e *Editor) run(executable string, args ...string) error {
cmd := exec.Command(executable, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()

//fmt.Fprintf(os.Stdout, "\n%s\n\n", utils.ColoredString("+ "+strings.Join(cmd.Args, " "), color.FgBlue))

o.Logger.Info("now start running!")
e.Logger.Debug("now start running editor")
if err := cmd.Run(); err != nil {
// not handling the error explicitly because usually we're going to see it in the output anyway
o.Logger.Error(err)
e.Logger.Error(err)
}
o.Logger.Info("now finish!")

/* cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
cmd.Stdin = nil
cmd = nil
e.Logger.Debug("finish to edit")

fmt.Fprintf(os.Stdout, "\n%s", utils.ColoredString("Press Enter", color.FgGreen))
fmt.Scanln() // wait for enter press
*/
// fmt.Fprintf(os.Stdout, "\n%s", utils.ColoredString("Press Enter", color.FgGreen))
// fmt.Scanln() // wait for enter press
return nil
}
14 changes: 7 additions & 7 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/rivo/tview"

"github.com/nakabonne/golintui/pkg/editor"
"github.com/nakabonne/golintui/pkg/golangcilint"
"github.com/nakabonne/golintui/pkg/gui/item"
"github.com/nakabonne/golintui/pkg/oscommand"
)

// Gui wraps the tview application which handles rendering and events.
Expand All @@ -20,12 +20,12 @@ type Gui struct {
resultsItem *item.Results
infoItem *item.Info

runner *golangcilint.Runner
osCommand *oscommand.OSCommand
logger *logrus.Entry
runner *golangcilint.Runner
editor *editor.Editor
logger *logrus.Entry
}

func New(logger *logrus.Entry, runner *golangcilint.Runner, command *oscommand.OSCommand) *Gui {
func New(logger *logrus.Entry, runner *golangcilint.Runner, command *editor.Editor) *Gui {
return &Gui{
application: tview.NewApplication(),
lintersItem: item.NewLinters(),
Expand All @@ -34,7 +34,7 @@ func New(logger *logrus.Entry, runner *golangcilint.Runner, command *oscommand.O
infoItem: item.NewInfo(runner.GetVersion()), // TODO: Run GetVersion() concurrency
runner: runner,
logger: logger,
osCommand: command,
editor: command,
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func (g *Gui) switchPanel(p tview.Primitive) {
// openFile temporarily suspends this application and open file with the editor as a sub process.
func (g *Gui) openFile(filepath string, line, colmun int) error {
g.application.Suspend(func() {
if err := g.osCommand.OpenFileAtLineColumn(filepath, line, colmun); err != nil {
if err := g.editor.OpenFileAtLineColumn(filepath, line, colmun); err != nil {
g.logger.Error(err)
}
})
Expand Down

0 comments on commit 78b915d

Please sign in to comment.