-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (118 loc) · 2.81 KB
/
main.go
File metadata and controls
153 lines (118 loc) · 2.81 KB
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/user"
"sort"
"strconv"
)
const defaultTopCommandsCount = 10
//Command represents shell command
type Command struct {
command string
number int
freq int
}
func (c Command) String() string {
return fmt.Sprintf("%5d: %v (x%d)", c.number, c.command, c.freq)
}
//Commands represents sortable (by freq) collection of shell commands
type Commands []*Command
func (slice Commands) Len() int {
return len(slice)
}
func (slice Commands) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func (slice Commands) Less(i, j int) bool {
return slice[i].freq < slice[j].freq
}
func main() {
topCommandsCount := getTopCommandsCount(defaultTopCommandsCount)
shellHistory := getShellHistory()
topCommands := getTopCommands(shellHistory, topCommandsCount)
for _, command := range topCommands {
fmt.Println(command)
}
}
func getTopCommands(shellHistory <-chan string, topCommandsCount int) Commands {
commands := getCommands(shellHistory)
sort.Sort(sort.Reverse(commands))
return commands[0:min(topCommandsCount, len(commands))]
}
func getTopCommandsCount(defaultTopCommandsCount int) int {
args := os.Args
if len(args) > 1 {
topCommandsCount, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal(err)
}
return topCommandsCount
} else {
return defaultTopCommandsCount
}
}
func getCommands(commandsChan <-chan string) Commands {
commandStructs := make(map[string]*Command)
number := 1
for commandString := range commandsChan {
command, exists := commandStructs[commandString]
if exists {
command.freq = command.freq + 1
command.number = number
} else {
commandStructs[commandString] = &Command{command: commandString, number: number, freq: 1}
}
number++
}
return getValuesFromMap(commandStructs)
}
//TODO check echo $SHELL
func getShellHistory() <-chan string {
historyFilename := getHistoryFilename()
return readByLine(historyFilename)
}
func getHistoryFilename() string {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
return currentUser.HomeDir + "/.bash_history"
}
func getValuesFromMap(commandStructs map[string]*Command) Commands {
values := make([]*Command, 0, len(commandStructs))
for _, value := range commandStructs {
values = append(values, value)
}
return values
}
func min(x, y int) int {
if x < y {
return x
} else {
return y
}
}
//readByLine returns a channel of lines of specified filename
func readByLine(filename string) <-chan string {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
lines := make(chan string)
go func() {
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
scannerErr := scanner.Err()
if scannerErr != nil {
log.Fatal(scannerErr)
}
}()
return lines
}