Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/reconquest/loreley
Simple and extensible colorizer for programs' output
https://github.com/reconquest/loreley
colorize colors golang logs tabwriter
Last synced: about 2 months ago
JSON representation
Simple and extensible colorizer for programs' output
- Host: GitHub
- URL: https://github.com/reconquest/loreley
- Owner: reconquest
- License: mit
- Created: 2016-06-27T02:42:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-11T07:56:03.000Z (about 3 years ago)
- Last Synced: 2024-06-18T18:30:52.766Z (7 months ago)
- Topics: colorize, colors, golang, logs, tabwriter
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 38
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# loreley
Easy and extensible colorizer for the programs' output.
Basically, loreley turns this:
```
{bold}{fg 15}{bg 27} hello {from "" 29} there {to 16 ""},
```Into this:
![2016-06-27-13t53t45](https://raw.githubusercontent.com/reconquest/loreley/master/demo.png)
# Usage
```go
package mainimport "fmt"
import "github.com/reconquest/loreley"func main() {
text, err := loreley.CompileAndExecuteToString(
`{bold}{fg 15}{bg 27} hello {from "" 29} {.where} {to 16 ""}{reset}`,
nil,
map[string]interface{}{"where": "there"},
)
if err != nil {
fmt.Errorf(`can't compile loreley template: %s`, err)
}fmt.Println(text)
}
```# Colors in `text/tabwriter`
Unfortunately, stdlib tabwriter does not implement proper column width
calculation if you use escape sequences in your data to highlight some
output.So, probably, You will see something like this trying tabwriter:
![tabwriter-before](https://raw.githubusercontent.com/reconquest/loreley/master/tabwriter-before.png)
Using loreley you can achieve exactly what you're expecting to see:
![tabwriter-after](https://raw.githubusercontent.com/reconquest/loreley/master/tabwriter-after.png)
```go
package mainimport (
"bytes"
"fmt"
"strings"
"text/tabwriter""github.com/reconquest/loreley"
)const ()
func main() {
buffer := &bytes.Buffer{}writer := tabwriter.NewWriter(buffer, 2, 4, 2, ' ', tabwriter.FilterHTML)
writer.Write([]byte(strings.Join(
[]string{
"CORES",
"DESCRIPTION\n",
}, "\t",
)))writer.Write([]byte(strings.Join(
[]string{
" 1 3 ",
"test\n",
}, "\t",
)))writer.Flush()
loreley.DelimLeft = "<"
loreley.DelimRight = ">"result, err := loreley.CompileAndExecuteToString(
buffer.String(),
nil,
nil,
)
if err != nil {
panic(err)
}fmt.Print(result)
}
```# Reference
loreley extends Go-lang template system. So, fully syntax is supported with
exception, that `{` and `}` will be used as delimiters.All ``, accepted by loreley, should be the 256-color code.
Available template functions:
* `{bg }` sets background color for the next text;
* `{fg }` sets foreground color for the next text;
* `{nobg}` resets background color to the default;
* `{nofg}` resets foreground color to the default;
* `{bold}` set bold mode on for the next text;
* `{nobold}` set bold mode to off;
* `{reverse}` set reverse mode on for the next text;
* `{noreverse}` set reverse mode off;
* `{underline}` set underline mode on for the next text;
* `{nounderline}` set underline mode off;
* `{reset}` resets all styles to default;
* `{from }` reuse current fg as specified ``'s bg color,
specified `` will be used as fg color and as bg color for the following
text;
* `{to }` reuse current bg as specified ``'s bg color,
specified `` will be used as fg color for the following text;# License
This project is licensed under the terms of the MIT license.