Skip to content

Commit 0d7c21c

Browse files
Improve layout function
1 parent 5e5e710 commit 0d7c21c

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/TerminalSystemMonitor.jl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ busy_time(info::Sys.CPUinfo) = Int64(
1111
)
1212

1313
"""
14-
cpu_percent(period)
14+
get_cpu_percent(period)
1515
1616
CPU usage between 0.0 and 100 [percent]
1717
The idea is borrowed from https://discourse.julialang.org/t/get-cpu-usage/24468/7
1818
Thank you @fonsp.
1919
"""
20-
function cpu_percent(period::Real = 1.0)
20+
function get_cpu_percent(period::Real = 1.0)
2121

2222
info = Sys.cpu_info()
2323
busies = busy_time.(info)
@@ -59,10 +59,22 @@ function unhidecursor()
5959
print("\u001B[?25h") # unhide cursor
6060
end
6161

62-
function layout(x, y)
62+
function extract_number_and_unit(str::AbstractString)
63+
m = match(r"(\d+\.\d+)\s*(\w+)", str)
64+
if !isnothing(m)
65+
return parse(Float64, m.captures[1]), m.captures[2]
66+
else
67+
return nothing, nothing
68+
end
69+
end
70+
71+
function plot_cpu_utilization_rates()
72+
y = get_cpu_percent()
73+
npad = 1+floor(Int, log10(length(y)))
74+
x = ["id: $(lpad(i-1, npad))" for (i, _) in enumerate(y)]
75+
6376
ncpus = length(y)
6477
y = round.(y, digits = 1)
65-
_, cols = displaysize(stdout)
6678

6779
plts = []
6880

@@ -73,16 +85,19 @@ function layout(x, y)
7385
barplot(x[c], y[c], maximum = 100, width = max(5, 15), height = length(c)),
7486
)
7587
end
76-
memoryusageGB = round((Sys.total_memory() - Sys.free_memory()) / 2^30, digits = 1)
77-
memorytotGB = Sys.total_memory() / 2 ^ 30
78-
(memorytot, memoryusage, memoryunit) = if memorytotGB 1.0
79-
round(1024memorytotGB), 1024memoryusageGB, "MB"
80-
else
81-
round(memorytotGB), memoryusageGB, "GB"
82-
end
88+
return plts
89+
end
90+
91+
function plot_cpu_memory_utilization()
92+
memorytot, memoryunit = Sys.total_memory() |> Base.format_bytes |> extract_number_and_unit
93+
memoryfree, _ = Sys.free_memory() |> Base.format_bytes |> extract_number_and_unit
94+
memoryusage = memorytot - memoryfree
95+
memorytot = round(memorytot)
8396

8497
seconds = floor(Int, Sys.uptime())
8598
datetime = DateTime(1970) + Second(seconds)
99+
100+
plts = []
86101
push!(
87102
plts,
88103
barplot(
@@ -103,26 +118,24 @@ function layout(x, y)
103118
),
104119
)
105120

106-
n = max(1, cols ÷ 25)
107-
chunks = collect(Iterators.partition(plts, n))
108-
109-
110-
return foldl(/, map(c -> prod(UnicodePlots.panel.(c)), chunks))
111121
end
112122

113123
function main(dummyargs...)
114124
hidecursor()
115125

116126
while true
117127
try
118-
y = cpu_percent()
119-
npad = 1+floor(Int, log10(length(y)))
120-
x = ["id: $(lpad(i-1, npad))" for (i, _) in enumerate(y)]
128+
plts = []
129+
append!(plts, plot_cpu_utilization_rates())
130+
append!(plts, plot_cpu_memory_utilization())
121131

122-
f = layout(x, y)
132+
# adjust layout
133+
_, cols = displaysize(stdout)
134+
n = max(1, cols ÷ 25)
135+
chunks = collect(Iterators.partition(plts, n))
136+
f = foldl(/, map(c -> prod(UnicodePlots.panel.(c)), chunks))
123137

124138
clearlinesall()
125-
126139
display(f)
127140
catch e
128141
unhidecursor() # unhide cursor

0 commit comments

Comments
 (0)