Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Remove unnecessary subshells #21

Closed
HaleTom opened this issue Jan 14, 2022 · 2 comments
Closed

[FEAT] Remove unnecessary subshells #21

HaleTom opened this issue Jan 14, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@HaleTom
Copy link

HaleTom commented Jan 14, 2022

Is your feature request related to a problem? Please describe.
Remove unnecessary calls to awk

Describe the solution you'd like

% col_n () {
    [[ -n $ZSH_VERSION ]] && setopt local_options sh_word_split
    local col=$1
    local IFS=$' \t'
    while read -r line; do
        set $line  # Spilt by IFS and set into $1, $2, ...
        printf "%s\n" "${@:$col:1}"
    done;
}
% echo "one two three" | col_n 2
two
@CodesOfRishi CodesOfRishi added the enhancement New feature or request label Jan 15, 2022
@CodesOfRishi
Copy link
Owner

This maybe a nice optimization! Will be looking into it!

@CodesOfRishi
Copy link
Owner

CodesOfRishi commented Jan 15, 2022

Thinking of implementing something like this:-

__smartcd::col_n() {
	local OLD_IFS=${IFS}
	
	# IFS value:
	# >  ^I$
	# > $
	IFS=" 	
	"

	local col=$1
	local count=1

	local _line
	read -r _line

	local _opt
	if [[ ${SMARTCD_CURRENT_SHELL} = "bash" ]]; then
		for _opt in ${_line}; do
			if [[ ${count} -eq "${col}" ]]; then
				printf '%s\n' "${_opt}"
				break
			fi
			count=$(( count + 1))

		done
	elif [[ ${SMARTCD_CURRENT_SHELL} = "zsh" ]]; then
		for _opt in ${=_line}; do
			if [[ ${count} -eq "${col}" ]]; then
				printf '%s\n' "${_opt}"
				break
			fi
			count=$(( count + 1))

		done
	fi

	IFS=${OLD_IFS}
}
  • Since I'm not dealing with multiple lines when it comes to extracting a particular column, there's no need for loop for read.
  • Custom IFS configuration contains a space, a tab & a new-line character.
  • In the case of Zsh, using ${=var} instead of enabling an option explicitly (like sh_word_split) seems like a better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants