-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a3813b
commit 98ca78a
Showing
7 changed files
with
197 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |