Skip to content

Commit

Permalink
Break out CPU count into internal function
Browse files Browse the repository at this point in the history
One of the last pieces of code not in a function. This would normally
never cause a problem, except that while we are running tests, we need
to pretend to have a different OS to test OS specific data sources. When
pretending to be Darwin, the CPU count would fail.

By moving this check into an internal function we get to skip running it
during tests, and the added benefit is that we only need to run it when
LP_ENABLE_LOAD is true, saving startup time for those with it enabled.

The downside being it is run every time lp_activate() is run, when
rarely does a machine change the number of CPUs while a user is active.
But with virtualization today, it is theoretically possible, so I count
that as another benefit.
  • Loading branch information
Rycieos committed Dec 18, 2020
1 parent 9a00ead commit e5047c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/functions/internal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ Formatting

.. _`ANSI escape color code`: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

Load
----

.. function:: __lp_cpu_count() -> var:_lp_CPUNUM

Returns the number of CPUs on the machine. The implementation depends on the
operating system.

.. versionadded:: 2.0

Path
----

Expand Down
17 changes: 14 additions & 3 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ lp_activate() {
__lp_temp_detect acpi sensors || LP_ENABLE_TEMP=0
fi

if (( LP_ENABLE_LOAD )); then
# Find and save the number of CPUs
__lp_cpu_count
fi

if [[ -n ${_LP_THEME_ACTIVATE_FUNCTION-} ]]; then
# Reactivate current theme
"$_LP_THEME_ACTIVATE_FUNCTION"
Expand Down Expand Up @@ -2103,14 +2108,18 @@ _lp_runtime_color() {
# Get CPU count and current load
case "$LP_OS" in
Linux)
_lp_CPUNUM=$( nproc 2>/dev/null || \grep -c '^[Pp]rocessor' /proc/cpuinfo )
__lp_cpu_count() {
_lp_CPUNUM=$( nproc 2>/dev/null || \grep -c '^[Pp]rocessor' /proc/cpuinfo )
}
_lp_cpu_load () {
local eol IFS=$' \t'
read lp_cpu_load eol < /proc/loadavg
}
;;
FreeBSD|Darwin|OpenBSD)
_lp_CPUNUM=$( sysctl -n hw.ncpu )
__lp_cpu_count() {
_lp_CPUNUM=$( sysctl -n hw.ncpu )
}
_lp_cpu_load () {
local bol eol IFS=$' \t'
# If you have problems with syntax coloring due to the following
Expand All @@ -2120,7 +2129,9 @@ case "$LP_OS" in
}
;;
SunOS)
_lp_CPUNUM=$( kstat -m cpu_info | \grep -c "module: cpu_info" )
__lp_cpu_count() {
_lp_CPUNUM=$( kstat -m cpu_info | \grep -c "module: cpu_info" )
}
_lp_cpu_load () {
lp_cpu_load="$( LC_ALL=C uptime | sed 's/.*load average: *\([0-9.]*\).*/\1/' )"
}
Expand Down

0 comments on commit e5047c0

Please sign in to comment.