diff --git a/api/interfaces/cli.go b/api/interfaces/cli.go index cdc5765..9fb0217 100644 --- a/api/interfaces/cli.go +++ b/api/interfaces/cli.go @@ -1,10 +1,12 @@ package interfaces import ( + "bufio" "flag" "fmt" "io" "os" + "os/user" prompt "github.com/c-bata/go-prompt" "github.com/pkg/errors" @@ -39,7 +41,21 @@ func (c *CLI) Run(args []string) int { return ExitCodeParseError } - p, err := c.preparePrompt(conf) + histories := []string{} + f, err := loadHistoryFile(conf) + if err != nil { + // NOTE: Continue processing even if an error occurred at open a file + fmt.Fprintf(c.ErrStream, "failed to a history file open: %v\n", err) + } else { + // TODO: Read lines and set to histories + s := bufio.NewScanner(f) + for s.Scan() { + histories = append(histories, s.Text()) + } + defer f.Close() + } + + p, err := c.preparePrompt(conf, f, histories) if err != nil { fmt.Fprintf(c.ErrStream, "failed to initialized prompt: %v\n", err) return ExitCodeError @@ -72,7 +88,7 @@ func usage(w io.Writer) { flag.CommandLine.PrintDefaults() } -func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) { +func (c *CLI) preparePrompt(conf *config.Config, writer io.Writer, histories []string) (*prompt.Prompt, error) { repository, err := bigtable.NewBigtableRepository(conf.Project, conf.Instance) if err != nil { return nil, errors.Wrapf(err, "failed to initialized bigtable repository:%v", err) @@ -81,8 +97,10 @@ func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) { rowsInteractor := application.NewRowsInteractor(repository) executor := Executor{ - outStream: c.OutStream, - errStream: c.ErrStream, + outStream: c.OutStream, + errStream: c.ErrStream, + history: writer, + rowsInteractor: rowsInteractor, tableInteractor: tableInteractor, } @@ -93,7 +111,17 @@ func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) { return prompt.New( executor.Do, completer.Do, - // TODO: Add histories from the history file. - // prompt.OptionHistory(), + prompt.OptionHistory(histories), + prompt.OptionPreviewSuggestionTextColor(prompt.Blue), + prompt.OptionSelectedSuggestionBGColor(prompt.LightGray), + prompt.OptionSuggestionBGColor(prompt.DarkGray), ), nil } + +func loadHistoryFile(conf *config.Config) (*os.File, error) { + u, err := user.Current() + if err != nil { + return nil, err + } + return os.OpenFile(u.HomeDir+"/.btcli_history", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) +} diff --git a/api/interfaces/executor.go b/api/interfaces/executor.go index 94f2773..5c757ab 100644 --- a/api/interfaces/executor.go +++ b/api/interfaces/executor.go @@ -29,6 +29,7 @@ func init() { type Executor struct { outStream io.Writer errStream io.Writer + history io.Writer tableInteractor *application.TableInteractor rowsInteractor *application.RowsInteractor @@ -47,7 +48,9 @@ func (e *Executor) Do(s string) { for _, c := range commands { if cmd == c.Name { - // TODO: Add command histories to history file + if e.history != nil { + fmt.Fprintln(e.history, strings.Join(args, " ")) + } // TODO: extract args[0] c.Runner(ctx, e, args...)