From d9b853010814a3bd2c40578d5ce3a44707de26f1 Mon Sep 17 00:00:00 2001 From: Laurent Wandrebeck Date: Wed, 20 Aug 2014 10:04:46 +0200 Subject: [PATCH 1/4] Optimize ps termlet. Runs in ~1.3sec instead of ~2sec on an AMD E-350. --- data/Termlets/ps | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/data/Termlets/ps b/data/Termlets/ps index 0fcd924..3c5698c 100755 --- a/data/Termlets/ps +++ b/data/Termlets/ps @@ -1,10 +1,15 @@ #!/bin/bash +lnnb=0 ps "$@" | while IFS= read -r line; do - # Position of string "PID" in line - pid_index=$(awk -v a="$line" -v b='PID' 'BEGIN{print index(a,b)}') + if [ $lnnb == 0 ] + then + # Position of string "PID" in line + pid_index=$(awk -v a="$line" -v b='PID' 'BEGIN{print index(a,b)}') + lnnb=1 + fi if [[ "$pid_index" -gt 0 ]]; then # Header line @@ -21,4 +26,5 @@ while IFS= read -r line; do fi echo -e "$modified_line" + pid_index=0 done From 21c077b9256138de46999e4a9b9f1b54c4da6f39 Mon Sep 17 00:00:00 2001 From: Laurent Wandrebeck Date: Wed, 20 Aug 2014 10:04:46 +0200 Subject: [PATCH 2/4] Optimize ps termlet. Runs in less than half a second instead of ~2sec on an AMD E-350. --- data/Termlets/ps | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/data/Termlets/ps b/data/Termlets/ps index 0fcd924..8ef536d 100755 --- a/data/Termlets/ps +++ b/data/Termlets/ps @@ -1,24 +1,26 @@ #!/bin/bash -ps "$@" | - -while IFS= read -r line; do - # Position of string "PID" in line - pid_index=$(awk -v a="$line" -v b='PID' 'BEGIN{print index(a,b)}') - - if [[ "$pid_index" -gt 0 ]]; then - # Header line - pid_end=$(($pid_index + 3)) - modified_line="$line" - else - # Content line - left_part=${line:0:pid_end-1} - pid_part=${left_part##* } - # Remove pid_part from left_part - left_part=${left_part:0:${#left_part}-${#pid_part}} - right_part=${line:pid_end-1} - modified_line="$left_part$(text_menu_start '3')$pid_part$(text_menu_end '3')$right_part" - fi +# IFS is '\n' +IFS=$'\012' +psoutput=($(ps "$@")) +# Just in case PPID could be displayed before PID, search for ' PID' +pid_index=$(awk -v a="${psoutput[0]}" -v b=' PID' 'BEGIN{print index(a,b)}') +# don’t use $() in loops, as it spawns a sub-process, so get markings out of the loop. +begin_mark=$(text_menu_start '3') +end_mark=$(text_menu_end '3') +# last character position of PID display. +let pid_end=pid_index+4 +# display 1st line as is, then destroy it. +echo -e ${psoutput[0]} +unset psoutput[0] +for line in ${psoutput[@]}; do + # Content line + left_part=${line:0:${pid_end}-1} + pid_part=${left_part##* } + # Remove pid_part from left_part + left_part=${left_part:0:${#left_part}-${#pid_part}} + right_part=${line:pid_end-1} + modified_line="$left_part$begin_mark$pid_part$end_mark$right_part" echo -e "$modified_line" done From 318e4b04cf714dab22395e9e0f1a967f107e97c3 Mon Sep 17 00:00:00 2001 From: Laurent Wandrebeck Date: Thu, 21 Aug 2014 21:16:38 +0200 Subject: [PATCH 3/4] Optimize ls termlet a bit. 11 sec for ~1000 files instead of 22 on an AMD E-350. --- data/Termlets/ls | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data/Termlets/ls b/data/Termlets/ls index 814017c..5242d7a 100755 --- a/data/Termlets/ls +++ b/data/Termlets/ls @@ -1,6 +1,10 @@ #!/bin/bash ls_output=$(ls "$@") +dir_begin_mark=$(text_menu_start '2') +dir_end_mark=$(text_menu_end '2') +file_begin_mark=$(text_menu_start '1') +file_end_mark=$(text_menu_end '1') # Surround with additional newlines to facilitate matching (see below) ls_output=$'\n'$ls_output$'\n' @@ -8,9 +12,9 @@ ls_output=$'\n'$ls_output$'\n' # TODO: Search for files in directory passed to ls rather than the current directory for filename in *; do if [[ -d $filename ]]; then - file_substitution="$(text_menu_start '2')$filename$(text_menu_end '2')" + file_substitution="$dir_begin_mark$filename$dir_end_mark" else - file_substitution="$(text_menu_start '1')$filename$(text_menu_end '1')" + file_substitution="$file_begin_mark$filename$file_end_mark" fi # Short format ("ls"; each filename on a single line) From 74eb2eb49ef4dd3ff250d5d8e7b754cb238e0ae1 Mon Sep 17 00:00:00 2001 From: Laurent Wandrebeck Date: Sat, 23 Aug 2014 20:37:38 +0200 Subject: [PATCH 4/4] Remove a useless line in ps termlet. --- data/Termlets/ps | 1 - 1 file changed, 1 deletion(-) diff --git a/data/Termlets/ps b/data/Termlets/ps index 2649303..8ef536d 100755 --- a/data/Termlets/ps +++ b/data/Termlets/ps @@ -23,5 +23,4 @@ for line in ${psoutput[@]}; do right_part=${line:pid_end-1} modified_line="$left_part$begin_mark$pid_part$end_mark$right_part" echo -e "$modified_line" - pid_index=0 done