olekukonko / tablewriter

ASCII table in golang
MIT License
4.29k stars 367 forks source link

[Question] Merging rows and cells #224

Closed bentcoder closed 11 months ago

bentcoder commented 12 months ago

Hi,

I am considering to switch to this library but want to confirm if I can achieve output shown below which is what I am able to do with the library I am currently using. I am not sure how I should modify my code to achieve it.

Thanks

Trying to achieve this table

+---+--------+--------------------------+-----------------+
| # | METHOD | PATH                     | MIDDLEWARES     |
+---+--------+--------------------------+-----------------+
| 1 | GET    | /api/info                | 1               |
|   |        |                          | panic.Recovery  |
+---+--------+--------------------------+-----------------+
| 2 | POST   | /api/v1/posts            | 2               |
|   |        |                          | request.Context |
|   |        |                          | panic.Recovery  |
+---+--------+--------------------------+-----------------+
| 3 | POST   | /api/v1/users            | 2               |
|   |        |                          | request.Context |
|   |        |                          | panic.Recovery  |
+---+--------+--------------------------+-----------------+
| 4 | GET    | /api/v1/users/{id}       | 2               |
|   |        |                          | request.Context |
|   |        |                          | panic.Recovery  |
+---+--------+--------------------------+-----------------+
| 5 | DELETE | /api/v1/users/{id}       | 3               |
|   |        |                          | request.Context |
|   |        |                          | panic.Recovery  |
|   |        |                          | audit.Delete    |
+---+--------+--------------------------+-----------------+
| 6 | GET    | /api/v1/users/{id}/posts | 2               |
|   |        |                          | request.Context |
|   |        |                          | panic.Recovery  |
+---+--------+--------------------------+-----------------+

My dummy code as starter but obviously both data won't work.

package table

import (
    "os"

    "github.com/olekukonko/tablewriter"
)

func Generate() error {
    data := [][]string{
        {"1", "GET", "/api/info", "***"},
        {"2", "POST", "/api/v1/posts", "***"},
        {"3", "POST", "/api/v1/users", "***"},
        {"4", "GET", "/api/v1/users/{id}", "***"},
        {"4", "DELETE", "/api/v1/users/{id}", "***"},
        {"5", "GET", "/api/v1/users/{id}/posts", "***"},
    }

    // data := [][]string{
    //  {"1", "GET", "/api/info", "1"},
    //  {"2", "POST", "/api/v1/posts", "2"},
    //  {"", "", "", "panic.Recovery"},
    //  {"", "", "", "request.Context"},
    //  {"3", "POST", "/api/v1/users", "2"},
    //  {"", "", "", "panic.Recovery"},
    //  {"", "", "", "request.Context"},
    //  {"4", "GET", "/api/v1/users/{id}", "2"},
    //  {"", "", "", "panic.Recovery"},
    //  {"", "", "", "request.Context"},
    //  {"4", "DELETE", "/api/v1/users/{id}", "3"},
    //  {"", "", "", "panic.Recovery"},
    //  {"", "", "", "request.Context"},
    //  {"", "", "", "audit.Delete"},
    //  {"5", "GET", "/api/v1/users/{id}/posts", "1"},
    //  {"", "", "", "panic.Recovery"},
    // }

    table := tablewriter.NewWriter(os.Stdout)
    table.SetHeader([]string{"#", "METHOD", "PATH", "MIDDLEWARE"})
    table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
    table.SetRowLine(true)
    table.AppendBulk(data)
    table.Render()

    // TODO: TBD
    return nil
}
bentcoder commented 11 months ago

Never mind, won't use this library.