1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
# Contributing to PTerm
> This document explains how to participate in the development of PTerm.\
If your goal is to report a bug instead of programming PTerm, you can do so [here](https://github.com/pterm/pterm/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).
## Best practise
We enforce some best practises, especially made for PTerm, to provide a clean and consistent user experience.
### Styles
Styles should always be consumed as pointers. That way, the user can change the style of printers globally.
## Creating a new printer
> In this chapter we will show you how to create a new printer.
### `TextPrinter` Template
```go
package pterm
type TemplatePrinter struct{
// TODO: Add printer settings here
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func (p TemplatePrinter) Sprint(a ...interface{}) string {
panic("write printer code here")
}
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p TemplatePrinter) Sprintln(a ...interface{}) string {
return Sprintln(p.Sprint(a...))
}
// Sprintf formats according to a format specifier and returns the resulting string.
func (p TemplatePrinter) Sprintf(format string, a ...interface{}) string {
return p.Sprint(Sprintf(format, a...))
}
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func (p TemplatePrinter) Print(a ...interface{}) *TextPrinter {
Print(p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func (p TemplatePrinter) Println(a ...interface{}) *TextPrinter {
Println(p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (p TemplatePrinter) Printf(format string, a ...interface{}) *TextPrinter {
Print(p.Sprintf(format, a...))
tp := TextPrinter(p)
return &tp
}
```
### `RenderablePrinter` Template
```go
package pterm
type TemplatePrinter struct{
// TODO: Add printer settings here
}
// Srender renders the Template as a string.
func (p TemplatePrinter) Srender() (string, error) {
var ret string
return ret, nil
}
// Render prints the Template to the terminal.
func (p TemplatePrinter) Render() error {
s, err := p.Srender()
if err != nil {
return err
}
Println(s)
return nil
}
```
### `LivePrinter` Template
```go
// Start the TemplatePrinter.
package pterm
import "github.com/pterm/pterm"
type TemplatePrinter struct{
}
func (s TemplatePrinter) Start(text...interface{}) (*TemplatePrinter, error) { // TODO: Replace Template with actual printer.
// TODO: start logic
return &s, nil
}
// Stop terminates the TemplatePrinter immediately.
// The TemplatePrinter will not resolve into anything.
func (s *TemplatePrinter) Stop() error {
// TODO: stop logic
return nil
}
// GenericStart runs Start, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Start instead of this in your program.
func (s *TemplatePrinter) GenericStart() (*LivePrinter, error) {
_, err := s.Start()
lp := LivePrinter(s)
return &lp, err
}
// GenericStop runs Stop, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Stop instead of this in your program.
func (s *TemplatePrinter) GenericStop() (*LivePrinter, error) {
err := s.Stop()
lp := LivePrinter(s)
return &lp, err
}
```
## Writing Tests
> Each method of PTerm must be tested.
### Required tests for every printer
#### Nil Check
> This ensures that a printer without set values will not produce errors.
```go
func TestTemplatePrinterNilPrint(t *testing.T) { // TODO: Replace "Template" with actual printer name.
p := TemplatePrinter{} // TODO: Replace "Template" with actual printer name.
p.Println("Hello, World!")
}
```
#### `WithXxx()` Methods
> Each method, which starts with `With` can be tested by checking if it actually creates a new printer and sets the value.
Example from `SectionPrinter`:
```go
func TestSectionPrinter_WithStyle(t *testing.T) {
p := SectionPrinter{}
s := NewStyle(FgRed, BgRed, Bold)
p2 := p.WithStyle(s)
assert.Equal(t, s, p2.Style)
assert.Empty(t, p.Style)
}
func TestSectionPrinter_WithTopPadding(t *testing.T) {
p := SectionPrinter{}
p2 := p.WithTopPadding(1337)
assert.Equal(t, 1337, p2.TopPadding)
assert.Empty(t, p.TopPadding)
}
```
### `TextPrinter` Tests Template
```go
func TestTemplatePrinterPrintMethods(t *testing.T) { // TODO: Replace "Template" with actual printer name.
p := DefaultTemplate // TODO: Replace "Template" with actual printer name.
t.Run("Print", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Print(a)
})
})
t.Run("Printf", func(t *testing.T) {
testPrintfContains(t, func(w io.Writer, format string, a interface{}) {
p.Printf(format, a)
})
})
t.Run("Println", func(t *testing.T) {
testPrintlnContains(t, func(w io.Writer, a interface{}) {
p.Println(a)
})
})
t.Run("Sprint", func(t *testing.T) {
testSprintContains(t, func(a interface{}) string {
return p.Sprint(a)
})
})
t.Run("Sprintf", func(t *testing.T) {
testSprintfContains(t, func(format string, a interface{}) string {
return p.Sprintf(format, a)
})
})
t.Run("Sprintln", func(t *testing.T) {
testSprintlnContains(t, func(a interface{}) string {
return p.Sprintln(a)
})
})
}
```
|