Skip to content

Commit f0f302d

Browse files
committed
Added option for timestamps with python on bash < 5 and reverted "date" timestamps to not using fifo
1 parent 90ecb50 commit f0f302d

File tree

2 files changed

+39
-45
lines changed

2 files changed

+39
-45
lines changed

bashtop

+32-44
Original file line numberDiff line numberDiff line change
@@ -277,47 +277,6 @@ box[double_right_corner_down]="╝"
277277
box[double_title_left]=""
278278
box[double_title_right]=""
279279

280-
#* If using bash version 5, set timestamps with EPOCHREALTIME variable
281-
if [[ -n $EPOCHREALTIME ]]; then
282-
get_ms() { #? Set given variable to current epoch millisecond with EPOCHREALTIME varialble
283-
local -n ms_out=$1
284-
ms_out=$((${EPOCHREALTIME/[.,]/}/1000))
285-
}
286-
287-
#* If not, use date command, through fifo if possible
288-
else
289-
tmpdir=""
290-
if [[ -n $XDG_RUNTIME_DIR && -w "$XDG_RUNTIME_DIR" ]]; then
291-
tmpdir="$XDG_RUNTIME_DIR"
292-
elif [[ -w /dev/shm ]]; then
293-
tmpdir="/dev/shm"
294-
elif [[ -w /tmp ]]; then
295-
tmpdir="/tmp"
296-
elif [[ -w "$HOME" ]]; then
297-
tmpdir="$HOME"
298-
fi
299-
300-
if [[ -n $tmpdir ]] && command -v ${stdbuf} >/dev/null 2>&1; then
301-
${mkfifo} "${tmpdir}/bashtop_datefifo"
302-
exec 5> >(exec ${stdbuf} -o0 ${date} -f - +%s%3N > "${tmpdir}/bashtop_datefifo" 2>&1)
303-
exec 6< "${tmpdir}/bashtop_datefifo"
304-
${rm} -f "${tmpdir}/bashtop_datefifo"
305-
306-
get_ms() { #? Set given variable to current epoch millisecond with date command through background fifo
307-
local -n ms_out=$1
308-
echo now >&5 &&
309-
read -u 6 ms_out
310-
}
311-
312-
else
313-
get_ms() { #? Set given variable to current epoch millisecond with forked date command
314-
local -n ms_out=$1
315-
ms_out=""
316-
read ms_out < <(${date} +%s%3N)
317-
}
318-
fi
319-
fi
320-
321280
init_() { #? Collect needed information and set options before startig main loop
322281
if [[ -z $1 ]]; then
323282
local i stx=0
@@ -4801,8 +4760,28 @@ if [[ $use_psutil == true ]]; then
48014760
fi
48024761
fi
48034762

4804-
#* if we have been sourced by another shell, quit. Allows sourcing only function definition.
4805-
[[ "${#BASH_SOURCE[@]}" -gt 1 ]] && { return 0; }
4763+
#* If using bash version 5, set timestamps with EPOCHREALTIME variable
4764+
if [[ -n $EPOCHREALTIME ]]; then
4765+
get_ms() { #? Set given variable to current epoch millisecond with EPOCHREALTIME varialble
4766+
local -n ms_out=$1
4767+
ms_out=$((${EPOCHREALTIME/[.,]/}/1000))
4768+
}
4769+
4770+
#* If not, but using psutil, set timestamps with python
4771+
elif [[ $use_psutil == true ]]; then
4772+
get_ms() {
4773+
local -n ms_out=$1
4774+
py_command -v ms_out "get_ms()"
4775+
}
4776+
4777+
#* Else use date command
4778+
else
4779+
get_ms() { #? Set given variable to current epoch millisecond with date command
4780+
local -n ms_out=$1
4781+
ms_out=""
4782+
read ms_out < <(${date} +%s%3N)
4783+
}
4784+
fi
48064785

48074786
#* Setup psutil script
48084787
if [[ $use_psutil == true ]]; then
@@ -4870,7 +4849,8 @@ allowed_commands: Tuple[str] = (
48704849
'get_net',
48714850
'get_cmd_out',
48724851
'get_sensors',
4873-
'get_sensors_check'
4852+
'get_sensors_check',
4853+
'get_ms'
48744854
)
48754855
command: str = ''
48764856
cpu_count: int = psutil.cpu_count()
@@ -4884,6 +4864,11 @@ def get_cmd_out(cmd: str):
48844864
'''Save bash the trouble of creating child processes by running through python instead'''
48854865
print(subprocess.check_output(cmd, shell=True, universal_newlines=True).rstrip())
48864866
4867+
def get_ms():
4868+
'''Get current epoch millisecond'''
4869+
t = str(time.time()).split(".")
4870+
print(f'{t[0]}{t[1][:3]}')
4871+
48874872
def get_sensors():
48884873
'''A clone of "sensors" but using psutil'''
48894874
temps = psutil.sensors_temperatures()
@@ -5266,6 +5251,9 @@ else
52665251
exec 2>/dev/null
52675252
fi
52685253

5254+
#* If we have been sourced by another shell, quit. Allows sourcing only function definition.
5255+
[[ "${#BASH_SOURCE[@]}" -gt 1 ]] && { return 0; }
5256+
52695257
#* Call init function
52705258
init_
52715259

src/bashtop.psutil.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
'get_net',
3333
'get_cmd_out',
3434
'get_sensors',
35-
'get_sensors_check'
35+
'get_sensors_check',
36+
'get_ms'
3637
)
3738
command: str = ''
3839
cpu_count: int = psutil.cpu_count()
@@ -46,6 +47,11 @@ def get_cmd_out(cmd: str):
4647
'''Save bash the trouble of creating child processes by running through python instead'''
4748
print(subprocess.check_output(cmd, shell=True, universal_newlines=True).rstrip())
4849

50+
def get_ms():
51+
'''Get current epoch millisecond'''
52+
t = str(time.time()).split(".")
53+
print(f'{t[0]}{t[1][:3]}')
54+
4955
def get_sensors():
5056
'''A clone of "sensors" but using psutil'''
5157
temps = psutil.sensors_temperatures()

0 commit comments

Comments
 (0)