-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1387f72
commit 0bbbd7c
Showing
3 changed files
with
289 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ package table | |
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Beispiel-Datenstrukturen | ||
|
@@ -26,12 +30,13 @@ func ExampleRenderASCII_noRotation() { | |
|
||
asciiTable, _ := RenderASCII(people, TableOption{Rotate: false}) | ||
fmt.Println(asciiTable) | ||
|
||
// Output: | ||
// +------+-----+------------------+ | ||
// | Name | Age | Email | | ||
// +------+-----+------------------+ | ||
// | John | 42 | [email protected] | | ||
// | Jane | 32 | [email protected] | | ||
// | John | 42 | [email protected] | | ||
// | Jane | 32 | [email protected] | | ||
// +------+-----+------------------+ | ||
} | ||
|
||
|
@@ -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 | [email protected] | [email protected] | | ||
// +-------+------------------+------------------+ | ||
} | ||
|
@@ -63,9 +69,87 @@ func ExampleRenderMarkdown_noRotation() { | |
|
||
markdownTable, _ := RenderMarkdown(people, TableOption{Rotate: false}) | ||
fmt.Println(markdownTable) | ||
|
||
// Output: | ||
// | Name | Age | Email | | ||
// |------|-----|------------------| | ||
// | John | 42 | [email protected] | | ||
// | Jane | 32 | [email protected] | | ||
// | John | 42 | [email protected] | | ||
// | Jane | 32 | [email protected] | | ||
} | ||
|
||
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 | | ||
} |
Oops, something went wrong.