Skip to content

Commit 9660c64

Browse files
authored
Merge pull request #12 from nao1215/add-header-command
Add header command
2 parents 3839b4c + b14d2a3 commit 9660c64

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ If the --sql option is not specified, the sqly shell is started. sqly command ar
4343
The command beginning with a dot is the sqly helper command; I plan to add more features in the future to make the sqly shell run more comfortably.
4444
```
4545
$ sqly
46-
sqly v0.0.7 (work in progress)
46+
sqly v0.2.1 (work in progress)
4747
4848
enter "SQL query" or "sqly command that beginning with a dot".
4949
.help print usage, .exit exit sqly.
5050
51-
sqly> .help
51+
sqly> .help
5252
.dump: dump db table to csv file
5353
.exit: exit sqly
54+
.header: print table header
5455
.help: print help message
5556
.import: import csv file(s)
5657
.tables: print tables

shell/command.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewCommands() CommandList {
2727
c := CommandList{}
2828
c[".dump"] = command{execute: c.dumpCommand, name: ".dump", description: "dump db table to csv file"}
2929
c[".exit"] = command{execute: c.exitCommand, name: ".exit", description: "exit sqly"}
30+
c[".header"] = command{execute: c.headerCommand, name: ".header", description: "print table header"}
3031
c[".help"] = command{execute: c.helpCommand, name: ".help", description: "print help message"}
3132
c[".import"] = command{execute: c.importCommand, name: ".import", description: "import csv file(s)"}
3233
c[".tables"] = command{execute: c.tablesCommand, name: ".tables", description: "print tables"}

shell/header.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package shell
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/nao1215/sqly/domain/model"
8+
"github.com/olekukonko/tablewriter"
9+
)
10+
11+
// headerCommand print table header.
12+
func (c CommandList) headerCommand(s *Shell, argv []string) error {
13+
if len(argv) == 0 {
14+
fmt.Fprintln(Stdout, "[Usage]")
15+
fmt.Fprintln(Stdout, " .header TABLE_NAME")
16+
return nil
17+
}
18+
19+
table, err := s.sqlite3Interactor.List(s.Ctx, argv[0])
20+
if err != nil {
21+
return err
22+
}
23+
table.Name = argv[0]
24+
printHeader(os.Stdout, table)
25+
return nil
26+
}
27+
28+
// printHeader print header
29+
func printHeader(out *os.File, t *model.Table) {
30+
table := tablewriter.NewWriter(out)
31+
table.SetHeader([]string{t.Name})
32+
table.SetAutoWrapText(false)
33+
table.SetAutoFormatHeaders(false)
34+
35+
for _, v := range t.Header {
36+
table.Append([]string{v})
37+
}
38+
table.Render()
39+
}

shell/shell.go

+14
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ func (s *Shell) completer(d prompt.Document) []prompt.Suggest {
187187
Text: v.Name,
188188
Description: v.Name + " table",
189189
})
190+
191+
table, err := s.sqlite3Interactor.List(s.Ctx, v.Name)
192+
if err != nil {
193+
// TODO: error logging
194+
continue
195+
}
196+
table.Name = v.Name
197+
198+
for _, h := range table.Header {
199+
suggest = append(suggest, prompt.Suggest{
200+
Text: h,
201+
Description: "header in " + table.Name + " table",
202+
})
203+
}
190204
}
191205
return prompt.FilterHasPrefix(suggest, d.GetWordBeforeCursor(), true)
192206
}

0 commit comments

Comments
 (0)