Files
ps.src
- //command: ps
- computer = get_shell.host_computer
- string.color = function(hex)
- return "<color=" + hex + ">" + self + "</color>"
- end function
- bar = function(n, bar_length = 35) // percentage number
- fill_count = ceil(n / bar_length * 10)
- empty_count = bar_length - fill_count
-
- fill_bar = "#"*fill_count
- empty_bar = "-"*empty_count
-
- bar = fill_bar.color("#21bcff") + empty_bar.color("#032e41")
- return bar
- end function
- cpu_load = 0.0
- mem_load = 0.0
- // convert ps output to a list
- tasks = []
- for line in computer.show_procs.split("\n")[1:]
- line = line.split(" ")
- task = {}
- cpu_load = cpu_load + line[2][:-1].val
- mem_load = mem_load + line[3][:-1].val
-
- task["user"] = line[0].color("#fbfbfb")
- if line[0] == "root" then
- task["user"] = line[0].color("#ff4b4b")
- end if
- task["pid"] = line[1].color("#20ff98")
- task["cpu"] = line[2].color("#21bcff")
- task["mem"] = line[3].color("#21bcff")
- task["command"] = line[4].color("#baff50")
-
- tasks.push task
- end for
- // render
- render_bar = function(bar_name, bar_load_value)
- r = ""
- r = r + (bar_name + ": [").color("#fbfbfb")
- r = r + bar(bar_load_value)
- r = r + "]==[ ".color("#fbfbfb")
- r = r + (bar_load_value + "%").color("#21bcff")
- r = r + " ]".color("#fbfbfb")
-
- return r
- end function
- print render_bar("cpu_usage", cpu_load)
- print render_bar("mem_usage", mem_load)
- print ""
- // print ps with colors
- pps = []
header = ["USER", "PID", "CPU", "MEM", "COMMAND"]
- header = task.indexes
- for i in header.indexes
header[i] = header[i].color("#9d9d9d")
- header[i] = header[i].upper.color("#9d9d9d")
- end for
- pps.push header.join(" ")
- for task in tasks
- pps.push(task.values.join(" "))
- end for
- print(format_columns(pps.join("\n")))