From fa5cad1e80bd6179d214894a8917282d1d2fb4af Mon Sep 17 00:00:00 2001 From: Mithrandie Date: Fri, 25 Feb 2022 17:58:18 +0900 Subject: [PATCH] Update README. --- .travis.yml | 3 +- README.md | 381 ++------------------------------------------- color/README.md | 81 ++++++++++ csv/README.md | 67 ++++++++ fixedlen/README.md | 65 ++++++++ json/README.md | 39 +++++ jsonl/README.md | 41 +++++ ltsv/README.md | 66 ++++++++ table/README.md | 64 ++++++++ 9 files changed, 439 insertions(+), 368 deletions(-) create mode 100644 color/README.md create mode 100644 csv/README.md create mode 100644 fixedlen/README.md create mode 100644 json/README.md create mode 100644 jsonl/README.md create mode 100644 ltsv/README.md create mode 100644 table/README.md diff --git a/.travis.yml b/.travis.yml index b7f0f66..ce4ca3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,5 @@ sudo: false go: - 1.x - - 1.14.x - - 1.15.x - 1.16.x + - 1.17.x diff --git a/README.md b/README.md index a8196f7..d3bb17d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # go-text -Package text and the sub-packages are Go libraries to operate text. +The package text and the sub-packages are Go libraries to operate text. [![Build Status](https://travis-ci.org/mithrandie/go-text.svg?branch=master)](https://travis-ci.org/mithrandie/go-text) [![License: MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg)](https://opensource.org/licenses/MIT) @@ -11,374 +11,23 @@ Package text and the sub-packages are Go libraries to operate text. - Shift-JIS ## Sub Packages +_color_ +: This package provides support for coloring text with ANSI escape sequences. -### color -Supports ANSI escape sequences. +_csv_ +: This package provides support for reading and writing CSV format. -```go -package main +_fixedlen_ +: This package provides support for reading and writing Fixed-Length format. -import ( - "encoding/json" - "fmt" - - "github.com/mithrandie/go-text/color" -) +_json_ +: This package provides support for encoding and decoding JSON format. -const ( - BlueColor = "blue" - YellowColor = "yellow" -) +_jsonl_ +: This package provides support for reading JSON Lines format. -type Config struct { - Palette color.PaletteConfig `json:"palette"` -} +_ltsv_ +: This package provides support for reading and writing LTSV format. -var jsonConfig = ` -{ - "palette": { - "effectors": { - "color1": { - "effects": [ - "Bold" - ], - "foreground": "Blue", - "background": null - }, - "color2": { - "effects": [], - "foreground": "Magenta", - "background": null - } - } - } -} -` - -func main() { - message := "message" - - // Use JSON Configuration - conf := &Config{} - if err := json.Unmarshal([]byte(jsonConfig), conf); err != nil { - panic(err) - } - - palette, err := color.GeneratePalette(conf.Palette) - if err != nil { - panic(err) - } - - fmt.Println(palette.Render("color1", message)) - - // Use Effector - e := color.NewEffector() - e.SetFGColor(color.Red) - e.SetEffect(color.Bold, color.Italic) - - fmt.Println(e.Render(message)) - - // Use Palette that bundles multiple effectors. - blue := color.NewEffector() - blue.SetFGColor(color.Blue) - yellow := color.NewEffector() - yellow.SetFGColor(color.Blue) - - palette.SetEffector(BlueColor, blue) - palette.SetEffector(YellowColor, yellow) - - fmt.Println(palette.Render(BlueColor, message)) -} -``` - -### csv -Supports reading and writing CSV Format. - - -```go -package main - -import ( - "os" - - "github.com/mithrandie/go-text" - "github.com/mithrandie/go-text/csv" -) - -func main() { - fp, err := os.Open("example.csv") - if err != nil { - panic("file open error") - } - defer func() { - if err = fp.Close(); err != nil { - panic(err.Error()) - } - }() - - r, _ := csv.NewReader(fp, text.UTF8) - r.Delimiter = ',' - r.WithoutNull = true - recordSet, err := r.ReadAll() - if err != nil { - panic("csv read error") - } - - lineBreak := r.DetectedLineBreak - - wfp, err := os.Create("example_new.csv") - if err != nil { - panic("file open error") - } - defer func() { - if err = wfp.Close(); err != nil { - panic(err.Error()) - } - }() - - w, err := csv.NewWriter(wfp, lineBreak, text.SJIS) - if err != nil { - panic(err.Error()) - } - w.Delimiter = ',' - - for _, record := range recordSet { - r := make([]csv.Field, 0, len(record)) - for _, field := range record { - r = append(r, csv.NewField(string(field), false)) - } - if err := w.Write(r); err != nil { - panic("write error") - } - } - if err = w.Flush(); err != nil { - panic(err) - } -} -``` - -### fixedlen -Supports reading and writing Fixed-Length Format. - -```go -package main - -import ( - "os" - - "github.com/mithrandie/go-text" - "github.com/mithrandie/go-text/fixedlen" -) - -func main() { - fp, err := os.Open("example.txt") - if err != nil { - panic("file open error") - } - defer func() { - if err = fp.Close(); err != nil { - panic(err.Error()) - } - }() - - r, _ := fixedlen.NewReader(fp, []int{5, 10, 45, 60}, text.UTF8) - r.WithoutNull = true - recordSet, err := r.ReadAll() - if err != nil { - panic("fixed-length read error") - } - - lineBreak := r.DetectedLineBreak - - wfp, err := os.Create("example_new.txt") - if err != nil { - panic("file open error") - } - defer func() { - if err = wfp.Close(); err != nil { - panic(err.Error()) - } - }() - - w, err := fixedlen.NewWriter(wfp, []int{5, 10, 45, 60}, lineBreak, text.SJIS) - if err != nil { - panic(err.Error) - } - - for _, record := range recordSet { - r := make([]fixedlen.Field, 0, len(record)) - for _, field := range record { - r = append(r, fixedlen.NewField(string(field), text.NotAligned)) - } - if err = w.Write(r); err != nil { - panic(err.Error()) - } - } - if err = w.Flush(); err != nil { - panic(err.Error()) - } -} -``` - -### json -Supports reading and writing JSON Format. - -```go -package main - -import ( - "fmt" - "io/ioutil" - - "github.com/mithrandie/go-text" - "github.com/mithrandie/go-text/json" -) - -func main() { - data, err := ioutil.ReadFile("example.json") - if err != nil { - panic("file open error") - } - - d := json.NewDecoder() - structure, escapeType, err := d.Decode(string(data)) - if err != nil { - panic("json decode error") - } - - e := json.NewEncoder() - e.EscapeType = escapeType - e.LineBreak = text.LF - e.PrettyPrint = true - e.Palette = json.NewJsonPalette() - - encoded := e.Encode(structure) - fmt.Println(encoded) -} -``` - -### ltsv -Supports reading and writing LTSV Format. - -```go -package main - -import ( - "os" - - "github.com/mithrandie/go-text" - "github.com/mithrandie/go-text/ltsv" -) - -func main() { - fp, err := os.Open("example.txt") - if err != nil { - panic("file open error") - } - defer func() { - if err = fp.Close(); err != nil { - panic(err.Error()) - } - }() - - r, _ := ltsv.NewReader(fp, text.UTF8) - r.WithoutNull = true - recordSet, err := r.ReadAll() - if err != nil { - panic("ltsv read error") - } - - header := r.Header.Fields() - lineBreak := r.DetectedLineBreak - - wfp, err := os.Create("example_new.ltsv") - if err != nil { - panic("file open error") - } - defer func() { - if err = wfp.Close(); err != nil { - panic(err.Error()) - } - }() - - w, err := ltsv.NewWriter(wfp, header, lineBreak, text.UTF8) - if err != nil { - panic("ltsv writer generation error") - } - - for _, record := range recordSet { - r := make([]string, 0, len(record)) - for _, field := range record { - r = append(r, string(field)) - } - if err = w.Write(r); err != nil { - panic(err.Error()) - } - } - if err = w.Flush(); err != nil { - panic(err.Error()) - } -} -``` - -### table -Supports writing text tables. - -```go -package main - -import ( - "fmt" - - "github.com/mithrandie/go-text" - "github.com/mithrandie/go-text/table" -) - -func main() { - header := []table.Field{ - table.NewField("c1", text.Centering), - table.NewField("c2", text.Centering), - table.NewField("c3", text.Centering), - } - - recordSet := [][]table.Field{ - { - table.NewField("1", text.RightAligned), - table.NewField("abc", text.LeftAligned), - table.NewField("true", text.NotAligned), - }, - { - table.NewField("2", text.RightAligned), - table.NewField("def", text.LeftAligned), - table.NewField("true", text.NotAligned), - }, - { - table.NewField("3", text.RightAligned), - table.NewField("ghi", text.LeftAligned), - table.NewField("true", text.NotAligned), - }, - } - - alignments := []text.FieldAlignment{ - text.RightAligned, - text.LeftAligned, - text.NotAligned, - } - - e := table.NewEncoder(table.GFMTable, len(recordSet)) - e.LineBreak = text.LF - e.EastAsianEncoding = true - e.CountDiacriticalSign = false - e.WithoutHeader = false - - - e.SetHeader(header) - for _, record := range recordSet { - e.AppendRecord(record) - } - e.SetFieldAlignments(alignments) - - encoded, _ := e.Encode() - fmt.Println(encoded) -} -``` \ No newline at end of file +_table_ +: This package provides support for writing text tables. diff --git a/color/README.md b/color/README.md new file mode 100644 index 0000000..cbbaa09 --- /dev/null +++ b/color/README.md @@ -0,0 +1,81 @@ +# color + +This package provides support for coloring text with ANSI escape sequences. + +## Examples + +```go +package main + +import ( + "encoding/json" + "fmt" + + "github.com/mithrandie/go-text/color" +) + +const ( + BlueColor = "blue" + YellowColor = "yellow" +) + +type Config struct { + Palette color.PaletteConfig `json:"palette"` +} + +var jsonConfig = ` +{ + "palette": { + "effectors": { + "color1": { + "effects": [ + "Bold" + ], + "foreground": "Blue", + "background": null + }, + "color2": { + "effects": [], + "foreground": "Magenta", + "background": null + } + } + } +} +` + +func main() { + message := "message" + + // Use JSON Configuration + conf := &Config{} + if err := json.Unmarshal([]byte(jsonConfig), conf); err != nil { + panic(err) + } + + palette, err := color.GeneratePalette(conf.Palette) + if err != nil { + panic(err) + } + + fmt.Println(palette.Render("color1", message)) + + // Use Effector + e := color.NewEffector() + e.SetFGColor(color.Red) + e.SetEffect(color.Bold, color.Italic) + + fmt.Println(e.Render(message)) + + // Use Palette that bundles multiple effectors. + blue := color.NewEffector() + blue.SetFGColor(color.Blue) + yellow := color.NewEffector() + yellow.SetFGColor(color.Blue) + + palette.SetEffector(BlueColor, blue) + palette.SetEffector(YellowColor, yellow) + + fmt.Println(palette.Render(BlueColor, message)) +} +``` diff --git a/csv/README.md b/csv/README.md new file mode 100644 index 0000000..4272eba --- /dev/null +++ b/csv/README.md @@ -0,0 +1,67 @@ +# csv + +This package provides support for reading and writing CSV format. + +## Examples + +```go +package main + +import ( + "os" + + "github.com/mithrandie/go-text" + "github.com/mithrandie/go-text/csv" +) + +func main() { + fp, err := os.Open("example.csv") + if err != nil { + panic("file open error") + } + defer func() { + if err = fp.Close(); err != nil { + panic(err.Error()) + } + }() + + r, _ := csv.NewReader(fp, text.UTF8) + r.Delimiter = ',' + r.WithoutNull = true + recordSet, err := r.ReadAll() + if err != nil { + panic("csv read error") + } + + lineBreak := r.DetectedLineBreak + + wfp, err := os.Create("example_new.csv") + if err != nil { + panic("file open error") + } + defer func() { + if err = wfp.Close(); err != nil { + panic(err.Error()) + } + }() + + w, err := csv.NewWriter(wfp, lineBreak, text.SJIS) + if err != nil { + panic(err.Error()) + } + w.Delimiter = ',' + + for _, record := range recordSet { + r := make([]csv.Field, 0, len(record)) + for _, field := range record { + r = append(r, csv.NewField(string(field), false)) + } + if err := w.Write(r); err != nil { + panic("write error") + } + } + if err = w.Flush(); err != nil { + panic(err) + } +} +``` diff --git a/fixedlen/README.md b/fixedlen/README.md new file mode 100644 index 0000000..2216010 --- /dev/null +++ b/fixedlen/README.md @@ -0,0 +1,65 @@ +# fixedlen + +This package provides support for reading and writing Fixed-Length format. + +## Examples + +```go +package main + +import ( + "os" + + "github.com/mithrandie/go-text" + "github.com/mithrandie/go-text/fixedlen" +) + +func main() { + fp, err := os.Open("example.txt") + if err != nil { + panic("file open error") + } + defer func() { + if err = fp.Close(); err != nil { + panic(err.Error()) + } + }() + + r, _ := fixedlen.NewReader(fp, []int{5, 10, 45, 60}, text.UTF8) + r.WithoutNull = true + recordSet, err := r.ReadAll() + if err != nil { + panic("fixed-length read error") + } + + lineBreak := r.DetectedLineBreak + + wfp, err := os.Create("example_new.txt") + if err != nil { + panic("file open error") + } + defer func() { + if err = wfp.Close(); err != nil { + panic(err.Error()) + } + }() + + w, err := fixedlen.NewWriter(wfp, []int{5, 10, 45, 60}, lineBreak, text.SJIS) + if err != nil { + panic(err.Error) + } + + for _, record := range recordSet { + r := make([]fixedlen.Field, 0, len(record)) + for _, field := range record { + r = append(r, fixedlen.NewField(string(field), text.NotAligned)) + } + if err = w.Write(r); err != nil { + panic(err.Error()) + } + } + if err = w.Flush(); err != nil { + panic(err.Error()) + } +} +``` diff --git a/json/README.md b/json/README.md new file mode 100644 index 0000000..b66b45b --- /dev/null +++ b/json/README.md @@ -0,0 +1,39 @@ +# json + +This package provides support for encoding and decoding JSON format. + +## Examples + +```go +package main + +import ( + "fmt" + "io/ioutil" + + "github.com/mithrandie/go-text" + "github.com/mithrandie/go-text/json" +) + +func main() { + data, err := ioutil.ReadFile("example.json") + if err != nil { + panic("file open error") + } + + d := json.NewDecoder() + structure, escapeType, err := d.Decode(string(data)) + if err != nil { + panic("json decode error") + } + + e := json.NewEncoder() + e.EscapeType = escapeType + e.LineBreak = text.LF + e.PrettyPrint = true + e.Palette = json.NewJsonPalette() + + encoded := e.Encode(structure) + fmt.Println(encoded) +} +``` diff --git a/jsonl/README.md b/jsonl/README.md new file mode 100644 index 0000000..506e101 --- /dev/null +++ b/jsonl/README.md @@ -0,0 +1,41 @@ +# jsonl + +This package provides support for reading JSON Lines format. + +## Examples + +```go +package main + +import ( + "fmt" + "os" + + "github.com/mithrandie/go-text/json" + "github.com/mithrandie/go-text/jsonl" +) + +func main() { + fp, err := os.Open("example.jsonl") + if err != nil { + panic("file open error") + } + defer func() { + if err = fp.Close(); err != nil { + panic(err.Error()) + } + }() + + r := jsonl.NewReader(fp) + structures, escapeType, err := r.ReadAll() + + fmt.Printf("Escape Type: %v\n", escapeType) + + e := json.NewEncoder() + e.EscapeType = escapeType + + for _, st := range structures { + fmt.Println(e.Encode(st)) + } +} +``` \ No newline at end of file diff --git a/ltsv/README.md b/ltsv/README.md new file mode 100644 index 0000000..06264fa --- /dev/null +++ b/ltsv/README.md @@ -0,0 +1,66 @@ +# ltsv + +This package provides support for reading and writing LTSV format. + +## Examples + +```go +package main + +import ( + "os" + + "github.com/mithrandie/go-text" + "github.com/mithrandie/go-text/ltsv" +) + +func main() { + fp, err := os.Open("example.txt") + if err != nil { + panic("file open error") + } + defer func() { + if err = fp.Close(); err != nil { + panic(err.Error()) + } + }() + + r, _ := ltsv.NewReader(fp, text.UTF8) + r.WithoutNull = true + recordSet, err := r.ReadAll() + if err != nil { + panic("ltsv read error") + } + + header := r.Header.Fields() + lineBreak := r.DetectedLineBreak + + wfp, err := os.Create("example_new.ltsv") + if err != nil { + panic("file open error") + } + defer func() { + if err = wfp.Close(); err != nil { + panic(err.Error()) + } + }() + + w, err := ltsv.NewWriter(wfp, header, lineBreak, text.UTF8) + if err != nil { + panic("ltsv writer generation error") + } + + for _, record := range recordSet { + r := make([]string, 0, len(record)) + for _, field := range record { + r = append(r, string(field)) + } + if err = w.Write(r); err != nil { + panic(err.Error()) + } + } + if err = w.Flush(); err != nil { + panic(err.Error()) + } +} +``` diff --git a/table/README.md b/table/README.md new file mode 100644 index 0000000..3aed1a4 --- /dev/null +++ b/table/README.md @@ -0,0 +1,64 @@ +# table + +This package provides support for writing text tables. + +## Examples + +```go +package main + +import ( + "fmt" + + "github.com/mithrandie/go-text" + "github.com/mithrandie/go-text/table" +) + +func main() { + header := []table.Field{ + table.NewField("c1", text.Centering), + table.NewField("c2", text.Centering), + table.NewField("c3", text.Centering), + } + + recordSet := [][]table.Field{ + { + table.NewField("1", text.RightAligned), + table.NewField("abc", text.LeftAligned), + table.NewField("true", text.NotAligned), + }, + { + table.NewField("2", text.RightAligned), + table.NewField("def", text.LeftAligned), + table.NewField("true", text.NotAligned), + }, + { + table.NewField("3", text.RightAligned), + table.NewField("ghi", text.LeftAligned), + table.NewField("true", text.NotAligned), + }, + } + + alignments := []text.FieldAlignment{ + text.RightAligned, + text.LeftAligned, + text.NotAligned, + } + + e := table.NewEncoder(table.GFMTable, len(recordSet)) + e.LineBreak = text.LF + e.EastAsianEncoding = true + e.CountDiacriticalSign = false + e.WithoutHeader = false + + + e.SetHeader(header) + for _, record := range recordSet { + e.AppendRecord(record) + } + e.SetFieldAlignments(alignments) + + encoded, _ := e.Encode() + fmt.Println(encoded) +} +```