Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rule and rulem bash functions coverted to Fish by Jay Berringer
#|------------------------------------------------------------------------------
#| Print a ruler in terminal window
#|
#| set -l _hr..printf assigns the result of the string interpolation to the local variable “_hr”
#| %*s waits for numeric input to define the width of the string, which in this case will be output #| as that number of spaces
#| (tput cols) is replaced with the number of columns in the current terminal as reported by tput
#| (passed to the %*s)
#| The variable is then output with sed substitution to replace the spaces with a - (default) or the #| desired character
#|------------------------------------------------------------------------------
function rule
set -l _hr (printf "%*s" (tput cols))
if test -z $argv
printf $_hr | sed -e s/ /\-/g
else
printf $_hr | sed -e s/ /\$argv/g
end
end
#|------------------------------------------------------------------------------
#| Print a ruler in terminal window with message
#|
#| set -l _hr..printf assigns the result of the string interpolation to the local variable “_hr”
#| %*s waits for numeric input to define the width of the string, which in this case will be output #| as that number of spaces
#| (tput cols) is replaced with the number of columns in the current terminal as reported by tput
#| (passed to the %*s)
#| The variable is then output with sed substitution to replace the spaces with a - (default) or the #| desired character
#|------------------------------------------------------------------------------
function rulem
set -l _hr (printf "%*s" (tput cols))
if test -z $argv[1]
echo "Usage: rulem MESSAGE [RULE_CHARACTER]"
return
else
if test (count $argv) -eq 2
printf "$_hrr33[2C$argv[1]" | sed -e s/ /\$argv[2]/g
else
printf "$_hrr33[2C$argv[1]" | sed -e s/ /\-/g
end
end
end
@BarbzYHOOL
Copy link

How to use this? (please notify me if you answer)

@krry
Copy link

krry commented Mar 13, 2019

@BarbzYHOOL, in the fish shell, you can define custom functions from the command line. If you paste the whole contents above into fish, then you can call rule or rulem. Looks like they accept arguments too. Then if you follow up with funcsave rule for example, it'll save the function to a file called rule.fish in ~/.config/fish/functions or wherever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment