Skip to content

Commit

Permalink
[barcode] New package with methods to generate colored representation…
Browse files Browse the repository at this point in the history
… of unique data
  • Loading branch information
andyone committed Sep 28, 2023
1 parent 823d5b4 commit e97a9c9
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.78.0

* `[barcode]` New package with methods to generate colored representation of unique data

### 12.77.1

* `[options]` Fixed bug with `Split` result for empty options
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ go get -u github.com/essentialkaos/ek/v12
* [`directio`](https://kaos.sh/g/ek.v12/directio) — Package provides methods for reading/writing files with direct io
* [`fmtc`](https://kaos.sh/g/ek.v12/fmtc) — Package provides methods similar to fmt for colored output
* [`fmtutil`](https://kaos.sh/g/ek.v12/fmtutil) — Package provides methods for output formatting
* [`fmtutil/table`](https://kaos.sh/g/ek.v12/fmtutil/table) — Package contains methods and structs for rendering data in tabular format
* [`fmtutil/barcode`](https://kaos.sh/g/ek.v12/fmtutil/barcode) — Package provides methods to generate colored representation of unique data
* [`fmtutil/panel`](https://kaos.sh/g/ek.v12/fmtutil/panel) — Package contains methods for rendering panels with text
* [`fmtutil/table`](https://kaos.sh/g/ek.v12/fmtutil/table) — Package contains methods and structs for rendering data in tabular format
* [`fsutil`](https://kaos.sh/g/ek.v12/fsutil) — Package provides methods for working with files on POSIX compatible systems (BSD/Linux/macOS)
* [`hash`](https://kaos.sh/g/ek.v12/hash) — Package hash contains different hash algorithms and utilities
* [`httputil`](https://kaos.sh/g/ek.v12/httputil) — Package provides methods for working with HTTP request/responses
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.77.1"
const VERSION = "12.78.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
89 changes: 89 additions & 0 deletions fmtutil/barcode/barcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package barcode provides methods to generate colored representation of unique data
package barcode

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"hash/crc32"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// dots is used for dots generation
var dots = []string{
"⠮", "⠉", "⠒", "⠤", "⠝", "⠳", "⠪", "⠭",
"⠕", "⠔", "⠮", "⠌", "⠡", "⠢", "⠜", "⠵",
}

// lines is used for lines generation
var lines = []string{
"╬", "╪", "╩", "╧", "╦", "╤", "╣", "╡",
"╠", "╞", "╝", "╛", "╚", "╘", "╕", "╔",
}

// boxes is used for boxes generation
var boxes = []string{
"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█",
"█", "▇", "▆", "▅", "▄", "▃", "▂", "▁",
}

// colors contains ASCII color codes
var colors = []int{
1, 2, 3, 4, 5, 6, 9, 10,
11, 12, 13, 14, 2, 12, 5, 14,
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Dots generates dots
func Dots(data []byte) string {
if len(data) == 0 {
return fmt.Sprintf("\033[38;5;1m%s\033[0m", "⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒")
}

return generate(data, dots)
}

// Lines generates lines
func Lines(data []byte) string {
if len(data) == 0 {
return fmt.Sprintf("\033[38;5;1m%s\033[0m", "╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬")
}

return generate(data, lines)
}

// Boxes generates boxes
func Boxes(data []byte) string {
if len(data) == 0 {
return fmt.Sprintf("\033[38;5;1m%s\033[0m", "████████████████")
}

return generate(data, boxes)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// generate generates barcode for given data
func generate(data []byte, alpha []string) string {
c := crc32.ChecksumIEEE(data)
c1, c2, c3, c4 := (c>>24)&0xFF, (c>>16)&0xFF, (c>>8)&0xFF, c&0xFF
return barcode(c1, alpha) + barcode(c2, alpha) + barcode(c3, alpha) + barcode(c4, alpha)
}

// barcode generates barcode part
func barcode(c uint32, alpha []string) string {
var result string

for _, i := range []uint32{c >> 4 & 0xF, c >> 2 & 0xF, c >> 1 & 0xF, c & 0xF} {
result += fmt.Sprintf("\033[38;5;%dm%s\033[0m", colors[i], alpha[i])
}

return result
}
41 changes: 41 additions & 0 deletions fmtutil/barcode/barcode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package barcode

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"testing"

. "github.com/essentialkaos/check"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func Test(t *testing.T) { TestingT(t) }

type BarcodeSuite struct{}

// ////////////////////////////////////////////////////////////////////////////////// //

var _ = Suite(&BarcodeSuite{})

// ////////////////////////////////////////////////////////////////////////////////// //

func (s *BarcodeSuite) TestDots(c *C) {
c.Assert(Dots([]byte("")), Equals, "\x1b[38;5;1m⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒\x1b[0m")
c.Assert(Dots([]byte("Test1234!")), Equals, "\x1b[38;5;1m⠮\x1b[0m\x1b[38;5;3m⠒\x1b[0m\x1b[38;5;6m⠳\x1b[0m\x1b[38;5;14m⠌\x1b[0m\x1b[38;5;11m⠕\x1b[0m\x1b[38;5;1m⠮\x1b[0m\x1b[38;5;2m⠉\x1b[0m\x1b[38;5;3m⠒\x1b[0m\x1b[38;5;4m⠤\x1b[0m\x1b[38;5;12m⠢\x1b[0m\x1b[38;5;14m⠌\x1b[0m\x1b[38;5;9m⠪\x1b[0m\x1b[38;5;2m⠉\x1b[0m\x1b[38;5;6m⠳\x1b[0m\x1b[38;5;14m⠌\x1b[0m\x1b[38;5;9m⠪\x1b[0m")
}

func (s *BarcodeSuite) TestLines(c *C) {
c.Assert(Lines([]byte("")), Equals, "\x1b[38;5;1m╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬\x1b[0m")
c.Assert(Lines([]byte("Test1234!")), Equals, "\x1b[38;5;1m╬\x1b[0m\x1b[38;5;3m╩\x1b[0m\x1b[38;5;6m╤\x1b[0m\x1b[38;5;14m╛\x1b[0m\x1b[38;5;11m╠\x1b[0m\x1b[38;5;1m╬\x1b[0m\x1b[38;5;2m╪\x1b[0m\x1b[38;5;3m╩\x1b[0m\x1b[38;5;4m╧\x1b[0m\x1b[38;5;12m╘\x1b[0m\x1b[38;5;14m╛\x1b[0m\x1b[38;5;9m╣\x1b[0m\x1b[38;5;2m╪\x1b[0m\x1b[38;5;6m╤\x1b[0m\x1b[38;5;14m╛\x1b[0m\x1b[38;5;9m╣\x1b[0m")
}

func (s *BarcodeSuite) TestBoxes(c *C) {
c.Assert(Boxes([]byte("")), Equals, "\x1b[38;5;1m████████████████\x1b[0m")
c.Assert(Boxes([]byte("Test1234!")), Equals, "\x1b[38;5;1m▁\x1b[0m\x1b[38;5;3m▃\x1b[0m\x1b[38;5;6m▆\x1b[0m\x1b[38;5;14m▅\x1b[0m\x1b[38;5;11m█\x1b[0m\x1b[38;5;1m▁\x1b[0m\x1b[38;5;2m▂\x1b[0m\x1b[38;5;3m▃\x1b[0m\x1b[38;5;4m▄\x1b[0m\x1b[38;5;12m▃\x1b[0m\x1b[38;5;14m▅\x1b[0m\x1b[38;5;9m▇\x1b[0m\x1b[38;5;2m▂\x1b[0m\x1b[38;5;6m▆\x1b[0m\x1b[38;5;14m▅\x1b[0m\x1b[38;5;9m▇\x1b[0m")
}

0 comments on commit e97a9c9

Please sign in to comment.