Skip to content

Commit

Permalink
improve table package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Jul 17, 2024
1 parent 1387f72 commit 0bbbd7c
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ each new build gets tested in multiple steps:
* [re](https://github.com/SimonWaldherr/golibs#re-----) helps you whenever you have to do something multiple times
* [regex](https://github.com/SimonWaldherr/golibs#regex-----) is a layer to speed up your regular expression development
* [rss](https://github.com/SimonWaldherr/golibs#rss-----) is a rss feed parser based on Golangs std xml package
* [semver](https://github.com/SimonWaldherr/golibs#semver-----) is a semantic version parsing/checking package
* [ssl](https://github.com/SimonWaldherr/golibs#ssl-----) generates ssl certificates for https
* [stack](https://github.com/SimonWaldherr/golibs#stack-----) can store your values in stacks and rings
* [structs](https://github.com/SimonWaldherr/golibs#structs-----) use structs like maps
* [table](https://github.com/SimonWaldherr/golibs#table-----) prints structs like ASCII or Markdown tables
* [xmath](https://github.com/SimonWaldherr/golibs#xmath-----) provides a few mathematical functions like Sum, Median, Harmonic-mean, …
* [xtime](https://github.com/SimonWaldherr/golibs#xtime-----) xtime implements a subset of strftime

Expand Down
94 changes: 89 additions & 5 deletions table/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package table

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"
)

// Beispiel-Datenstrukturen
Expand All @@ -26,12 +30,13 @@ func ExampleRenderASCII_noRotation() {

asciiTable, _ := RenderASCII(people, TableOption{Rotate: false})
fmt.Println(asciiTable)

// Output:
// +------+-----+------------------+
// | Name | Age | Email |
// +------+-----+------------------+
// | John | 42 | john@example.com |
// | Jane | 32 | jane@example.com |
// | John | 42 | john@example.com |
// | Jane | 32 | jane@example.com |
// +------+-----+------------------+
}

Expand All @@ -44,12 +49,13 @@ func ExampleRenderASCII_withRotation() {

rotatedASCIITable, _ := RenderASCII(people, TableOption{Rotate: true})
fmt.Println(rotatedASCIITable)

// Output:
// +-------+------------------+------------------+
// | | Row 1 | Row 2 |
// +-------+------------------+------------------+
// | Name | John | Jane |
// | Age | 42 | 32 |
// | Age | 42 | 32 |
// | Email | john@example.com | jane@example.com |
// +-------+------------------+------------------+
}
Expand All @@ -63,9 +69,87 @@ func ExampleRenderMarkdown_noRotation() {

markdownTable, _ := RenderMarkdown(people, TableOption{Rotate: false})
fmt.Println(markdownTable)

// Output:
// | Name | Age | Email |
// |------|-----|------------------|
// | John | 42 | john@example.com |
// | Jane | 32 | jane@example.com |
// | John | 42 | john@example.com |
// | Jane | 32 | jane@example.com |
}

type TestStruct struct {
Name string
Age int
Salary float64
StartDate time.Time
}

func ExampleParseCSVInput() {
csvData := `Name,Age,Salary,StartDate
Alice,30,70000.50,2021-05-01T00:00:00Z
Bob,22,48000.00,2022-07-15T00:00:00Z`
var data []TestStruct
err := ParseCSVInput(strings.NewReader(csvData), &data)
if err != nil {
fmt.Println(err)
}
markdownTable, _ := RenderMarkdown(data, TableOption{Rotate: false})
fmt.Println(markdownTable)

// Output:
// | Name | Age | Salary | StartDate |
// |-------|-----|---------|----------------------|
// | Alice | 30 | 70000.5 | 2021-05-01T00:00:00Z |
// | Bob | 22 | 48000 | 2022-07-15T00:00:00Z |
}

// Header represents a single HTTP header key-value pair
type Header struct {
Key string
Value string
}

func firstN(s string, n int) string {
i := 0
for j := range s {
if i == n {
return s[:j]
}
i++
}
return s
}

func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, Worldn"))
}

// ExampleRenderHTTPResponse demonstrates how to render HTTP response headers
func ExampleRenderHTTPResponse() {
req := httptest.NewRequest("GET", "https://github.com", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()

defer resp.Body.Close()

// Convert headers to a slice of Header structs
var headers []Header
for key, values := range resp.Header {
for _, value := range values {
headers = append(headers, Header{Key: key, Value: firstN(value, 30)})
}
}

// Render the headers as a Markdown table
markdownTable, err := RenderMarkdown(headers, TableOption{Rotate: false})
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(markdownTable)
// Output:
// | Key | Value |
// |--------------|---------------------------|
// | Content-Type | text/plain; charset=utf-8 |
}
Loading

0 comments on commit 0bbbd7c

Please sign in to comment.