Skip to content
This repository was archived by the owner on Jan 10, 2026. It is now read-only.

Commit 9ae020b

Browse files
feat: 🎸 ignore some commands (#13)
add some tests, fix weekly and daiy bugs
1 parent ea0bad5 commit 9ae020b

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ chr-*.tar
2424

2525
# Temporary files, for example, from tests.
2626
/tmp/
27+
.DS_Store

lib/chr.ex

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Chr do
33
Chr module
44
"""
55

6+
@bar_chart_length 20
7+
68
@doc """
79
return command history
810
"""
@@ -83,9 +85,10 @@ defmodule Chr do
8385
@doc """
8486
top commands
8587
"""
86-
def top_commands(history_list) do
88+
def top_commands(history_list, ignore_commands) do
8789
history_list
8890
|> Enum.map(&pick_up_command/1)
91+
|> Enum.filter(fn command -> command not in ignore_commands end)
8992
|> Enum.reduce(%{}, fn command, acc ->
9093
Map.update(acc, command, 1, &(&1 + 1))
9194
end)
@@ -186,6 +189,11 @@ defmodule Chr do
186189

187190
@doc """
188191
Weekly Activity
192+
193+
## Examples
194+
195+
iex> Chr.weekly_activity([": 1709957744:0;cat .zsh_history",": 1710044144:0;cat .zsh_history"])
196+
[monday: 0, tuesday: 0, wednesday: 0, thursday: 0, friday: 0, saturday: 20, sunday: 20]
189197
"""
190198
def weekly_activity(history_list) do
191199
map =
@@ -200,14 +208,44 @@ defmodule Chr do
200208
max = if max == 0, do: 1, else: max
201209

202210
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
203-
|> Enum.with_index()
211+
|> Enum.with_index(1)
204212
|> Enum.map(fn {day, index} ->
205-
{day, (Map.get(map, index, 0) / max * 20) |> round()}
213+
{day, (Map.get(map, index, 0) / max * @bar_chart_length) |> round()}
206214
end)
207215
end
208216

209217
@doc """
210218
Daily Activity
219+
220+
## Examples
221+
222+
iex> Chr.daily_activity([": 1709957744:0;cat .zsh_history",": 1709968544:0;cat .zsh_history", ": 1710000944:0;cat .zsh_history"])
223+
[
224+
{"01", 20},
225+
{"02", 0},
226+
{"03", 0},
227+
{"04", 0},
228+
{"05", 0},
229+
{"06", 0},
230+
{"07", 0},
231+
{"08", 0},
232+
{"09", 0},
233+
{"10", 0},
234+
{"11", 0},
235+
{"12", 0},
236+
{"13", 20},
237+
{"14", 0},
238+
{"15", 0},
239+
{"16", 20},
240+
{"17", 0},
241+
{"18", 0},
242+
{"19", 0},
243+
{"20", 0},
244+
{"21", 0},
245+
{"22", 0},
246+
{"23", 0},
247+
{"24", 0}
248+
]
211249
"""
212250
def daily_activity(history_list) do
213251
map =
@@ -225,9 +263,9 @@ defmodule Chr do
225263
|> Enum.map(fn hour ->
226264
hour |> Integer.to_string() |> String.pad_leading(2, "0")
227265
end)
228-
|> Enum.with_index()
266+
|> Enum.with_index(1)
229267
|> Enum.map(fn {time, index} ->
230-
{time, (Map.get(map, index, 0) / max * 20) |> round()}
268+
{time, (Map.get(map, index, 0) / max * @bar_chart_length) |> round()}
231269
end)
232270
end
233271
end

lib/chr/cli.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Chr.Cli do
33
CLI module
44
"""
55

6+
@ignore_commands ["cd", "pwd", "clear", "exit", "ls", "code"]
7+
68
@doc """
79
main function
810
"""
@@ -12,7 +14,7 @@ defmodule Chr.Cli do
1214
Chr.Print.print_logo()
1315
|> Owl.IO.puts()
1416

15-
Chr.Print.print_top_commands(histories)
17+
Chr.Print.print_top_commands(histories, @ignore_commands)
1618
|> Owl.IO.puts()
1719

1820
Chr.Print.print_top_directories(histories)

lib/chr/print.ex

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,25 @@ defmodule Chr.Print do
8484
@doc """
8585
print top commands
8686
"""
87-
def print_top_commands(histories) do
88-
histories
89-
|> Chr.top_commands()
90-
|> Enum.map(fn {command, count} ->
91-
count_stringify(5, count |> Integer.to_string(), command)
92-
end)
93-
|> Enum.join("\n")
94-
|> print_count_number("🏆 Top Commands", :red)
87+
def print_top_commands(histories, ignore_commands) do
88+
top_commands =
89+
histories
90+
|> Chr.top_commands(ignore_commands)
91+
|> Enum.map(fn {command, count} ->
92+
count_stringify(5, count |> Integer.to_string(), command)
93+
end)
94+
|> Enum.join("\n")
95+
96+
ignore_commands = Enum.join(ignore_commands, ", ")
97+
98+
footer =
99+
"""
100+
\n
101+
Ignored commands: #{ignore_commands}
102+
"""
103+
|> String.trim_trailing()
104+
105+
(top_commands <> footer) |> print_count_number("🏆 Top Commands", :red)
95106
end
96107

97108
@doc """

0 commit comments

Comments
 (0)