Skip to content

Commit

Permalink
remastered edition!
Browse files Browse the repository at this point in the history
  • Loading branch information
reinerRubin committed Dec 25, 2019
1 parent 7a3813b commit 98ca78a
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
mkdir -p bin
go build -o bin/li cmd/li/main.go
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# li
```
$ cat /tmp/stat | head | li
795 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ "AU"
511 ■■■■■■■■■■■■■■■■■■■ "AT"
179 ■■■■■■ "AR"
20 ■ "AL"
13 ■ "AF"
9 ■ "AE"
6 ■ "AO"
4 ■ "AW"
4 ■ "AM"
3 ■ "AI"
```
36 changes: 36 additions & 0 deletions cmd/li/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"

"github.com/reinerRubin/li"
)

func main() {
if err := app(); err != nil {
log.Fatal(err)
}
}

func app() error {
lines, err := li.ReadStdin()
if err != nil {
return err
}

frequencies, err := li.NewFrequencies(lines)
if err != nil {
return err
}

barChatSectionWidth, err := li.BarChatSectionWidth()
if err != nil {
return err
}

barChat := li.FrequenciesToBarChat(frequencies, barChatSectionWidth)
fmt.Print(barChat)

return nil
}
78 changes: 78 additions & 0 deletions frequency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package li

import (
"fmt"
"regexp"
"sort"
"strconv"
)

var uniqCRegexp = regexp.MustCompile(`^\s*(\d+)\s+(.*)$`)

type Frequency struct {
Item string
Count int
}

// Frequencies is a slice of item/count pairs, must be sorted by count
type Frequencies []Frequency

func (v Frequencies) maxCount() (c int) {
for _, frequency := range v {
if frequency.Count > c {
c = frequency.Count
}
}

return
}

func (v Frequencies) Len() int {
return len(v)
}

func (v Frequencies) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}

func (v Frequencies) Less(i, j int) bool {
if v[i].Count == v[j].Count {
return v[i].Item > v[j].Item
}

return v[i].Count > v[j].Count
}

func NewFrequencies(lines []string) (Frequencies, error) {
fr := make(Frequencies, 0, len(lines))

for _, line := range lines {
f, err := newFrequency(line)
if err != nil {
return nil, err
}

fr = append(fr, f)
}

sort.Sort(fr)

return fr, nil
}

func newFrequency(line string) (Frequency, error) {
m := uniqCRegexp.FindStringSubmatch(line)
if len(m) != 3 {
return Frequency{}, fmt.Errorf("failed to parse %s: %+v", line, m)
}

count, err := strconv.Atoi(m[1])
if err != nil {
return Frequency{}, err
}

return Frequency{
Item: m[2],
Count: count,
}, nil
}
65 changes: 65 additions & 0 deletions li.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package li

import (
"bufio"
"fmt"
"os"
"strings"
)

func FrequenciesToBarChat(fr Frequencies, barChatWidth int) string {
var (
maxCount = len(fmt.Sprintf("%d", fr.maxCount()))
acc = strings.Builder{}
)

if len(fr) == 0 {
return ""
}

biggest := fr[0].Count
count := func(quantity int) int {
w := quantity * barChatWidth / biggest
if w == 0 {
w = 1 // for aesthetic
}
return w
}

format := "%" + fmt.Sprintf("%d", maxCount) + "d"
for _, f := range fr {
acc.WriteString(fmt.Sprintf(" "+format+" ", f.Count))

rightOffset := count(f.Count)
for i := 0; i < rightOffset; i++ {
acc.WriteString("■")
}
for i := 0; i < barChatWidth-rightOffset; i++ {
acc.WriteString(" ")
}
acc.WriteString(fmt.Sprintf(" %s\n", f.Item))
}

return acc.String()
}

func ReadStdin() ([]string, error) {
var (
lines = make([]string, 0)
scanner = bufio.NewScanner(os.Stdin)
)

for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err := scanner.Err(); err != nil {
return nil, err
}

return lines, nil
}

func BarChatSectionWidth() (int, error) {
return 30, nil
}
188 changes: 0 additions & 188 deletions main.go

This file was deleted.

0 comments on commit 98ca78a

Please sign in to comment.