Skip to content

Commit

Permalink
Merge pull request #19 from nao1215/add-markdown-format
Browse files Browse the repository at this point in the history
Add featuer thar print date by markdown table format
  • Loading branch information
nao1215 authored Nov 13, 2022
2 parents 9db79a7 + 4d9c6fd commit 2ea9e0b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ $ sqly --sql "SELECT * FROM user" testdata/user.csv --output=test.csv
-h, --help print help message
-j, --json change output format to json (default: table)
-l, --ltsv change output format to ltsv (default: table)
-m, --markdown change output format to markdown (default: table)
-o, --output string destination path for SQL results specified in --sql option
-s, --sql string sql query you want to execute
-t, --tsv change output format to tsv (default: table)
Expand All @@ -126,7 +127,7 @@ $ sqly --sql "SELECT * FROM user" testdata/user.csv --output=test.csv
- [x] print ltsv format
- [ ] dump ltsv file
- [ ] import swagger
- [ ] print markdown format
- [x] print markdown format
- [ ] ignore csv header option
- [x] The file type is determined by the file extension. This specification is to reduce the number of options.
- [x] change input position (left arrow, right arrow, delete char)
Expand Down Expand Up @@ -155,4 +156,3 @@ If you would like to send comments such as "find a bug" or "request for addition

# LICENSE
The sqly project is licensed under the terms of [MIT LICENSE](./LICENSE).

12 changes: 8 additions & 4 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ type Arg struct {
}

type outputFlag struct {
csv bool
tsv bool
ltsv bool
json bool
csv bool
tsv bool
ltsv bool
json bool
markdown bool
}

// NewArg return *Arg that is assigned the result of parsing os.Args.
Expand All @@ -61,6 +62,7 @@ func NewArg() (*Arg, error) {
pflag.BoolVarP(&outputFlag.tsv, "tsv", "t", false, "change output format to tsv (default: table)")
pflag.BoolVarP(&outputFlag.ltsv, "ltsv", "l", false, "change output format to ltsv (default: table)")
pflag.BoolVarP(&outputFlag.json, "json", "j", false, "change output format to json (default: table)")
pflag.BoolVarP(&outputFlag.markdown, "markdown", "m", false, "change output format to markdown table(default: table)")
pflag.BoolVarP(&arg.HelpFlag, "help", "h", false, "print help message")
pflag.BoolVarP(&arg.VersionFlag, "version", "v", false, "print help message")
pflag.Parse()
Expand All @@ -85,6 +87,8 @@ func newOutput(filePath string, of *outputFlag) *Output {
mode = model.PrintModeLTSV
} else if of.json {
mode = model.PrintModeJSON
} else if of.markdown {
mode = model.PrintModeMarkdownTable
}
return &Output{
FilePath: filePath,
Expand Down
23 changes: 22 additions & 1 deletion domain/model/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type PrintMode uint
const (
// PrintModeTable print data in table format
PrintModeTable PrintMode = iota
// PrintModeMarkdownTable print data in markdown table format
PrintModeMarkdownTable
// PrintModeCSV print data in csv format
PrintModeCSV
// PrintModeTSV print data in tsv format
Expand All @@ -30,6 +32,8 @@ func (p PrintMode) String() string {
switch p {
case PrintModeTable:
return "table"
case PrintModeMarkdownTable:
return "markdown"
case PrintModeCSV:
return "csv"
case PrintModeTSV:
Expand Down Expand Up @@ -104,6 +108,8 @@ func (t *Table) Print(out *os.File, mode PrintMode) {
switch mode {
case PrintModeTable:
t.printTable(out)
case PrintModeMarkdownTable:
t.printMarkdownTable(out)
case PrintModeCSV:
t.printCSV(out)
case PrintModeTSV:
Expand All @@ -130,6 +136,21 @@ func (t *Table) printTable(out *os.File) {
table.Render()
}

// printMarkdownTable print all record with header; output format is markdown
func (t *Table) printMarkdownTable(out *os.File) {
table := tablewriter.NewWriter(out)
table.SetHeader(t.Header)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")

for _, v := range t.Records {
table.Append(v)
}
table.Render()
}

// printCSV print all record with header; output format is csv
func (t *Table) printCSV(out *os.File) {
fmt.Fprintln(out, strings.Join(t.Header, ","))
Expand All @@ -146,7 +167,7 @@ func (t *Table) printTSV(out *os.File) {
}
}

// Print print all record with header; output format is tsv
// Print print all record with header; output format is ltsv
func (t *Table) printLTSV(out *os.File) {
for _, v := range t.Records {
r := []string{}
Expand Down
4 changes: 4 additions & 0 deletions shell/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (c CommandList) modeCommand(s *Shell, argv []string) error {
fmt.Fprintf(Stdout, " .mode OUTPUT_MODE ※ current mode=%s\n", s.argument.Output.Mode.String())
fmt.Fprintln(Stdout, "[Output mode list]")
fmt.Fprintln(Stdout, " table")
fmt.Fprintln(Stdout, " markdown")
fmt.Fprintln(Stdout, " csv")
fmt.Fprintln(Stdout, " tsv")
fmt.Fprintln(Stdout, " ltsv")
Expand All @@ -29,6 +30,9 @@ func (c CommandList) modeCommand(s *Shell, argv []string) error {
case model.PrintModeTable.String():
fmt.Printf("Change output mode from %s to table\n", s.argument.Output.Mode.String())
s.argument.Output.Mode = model.PrintModeTable
case model.PrintModeMarkdownTable.String():
fmt.Printf("Change output mode from %s to markdown table\n", s.argument.Output.Mode.String())
s.argument.Output.Mode = model.PrintModeMarkdownTable
case model.PrintModeCSV.String():
fmt.Printf("Change output mode from %s to csv\n", s.argument.Output.Mode.String())
s.argument.Output.Mode = model.PrintModeCSV
Expand Down

0 comments on commit 2ea9e0b

Please sign in to comment.