pawelgrzybek / snippet-generator

Snippet generator for Visual Studio Code, Sublime Text and Atom
https://snippet-generator.app/
1.74k stars 202 forks source link

Fixes issue #68 Handle bash variable escapes in body #84

Open disaac opened 2 years ago

disaac commented 2 years ago
function blog() {
  local msg
  local logtype
  local loglevel
  local logfile txtblu txtgrn txtylw txtred txtrst thisfunc
  loglevel=4
  logfile="$(mktemp -t "$(basename "$0")" || exit 1)"
  logtype="$1"
  msg="$2"
  datetime="$(date +'%F.%H:%M:%S')"
  txtblu=$(tput setaf 12) # Blue
  txtgrn=$(tput setaf 10) # Green
  txtylw=$(tput setaf 11) # Yellow
  txtred=$(tput setaf 1) # Red
  txtrst=$(tput sgr0)
  thisfunc="${FUNCNAME[*]/blog/}"
  thisfunc="${thisfunc/main /}"
  logformat="[${logtype}]\t${datetime}\tfuncname: ${thisfunc}\t[line:$(caller 0 | cut -f 1 -d ' ')]\t${msg}"
  {
    case $logtype in
      debug)
        [[ $loglevel -le 0 ]] && echo -e "${txtblu}${logformat}${txtrst}"
        ;;
      info)
        [[ $loglevel -le 1 ]] && echo -e "${txtgrn}${logformat}${txtrst}"
        ;;
      warn)
        [[ $loglevel -le 2 ]] && echo -e "${txtylw}${logformat}${txtrst}"
        ;;
      error)
        [[ $loglevel -le 3 ]] && echo -e "${txtred}${logformat}${txtrst}"
        ;;
    esac
  } | tee -a "$logfile"
}

Issue: #68

pawelgrzybek commented 2 years ago

Thank you for this contribution. Let me run your implementation against few different test cases manually and I will merge it. most likely after the weekend. Thank you again!

disaac commented 2 years ago

Thank you for this contribution. Let me run your implementation against few different test cases manually and I will merge it. most likely after the weekend. Thank you again!

@pawelgrzybek I actually found some bashisms that will still fail to be properly converted. I don't have time at the moment to PR on this but wanted to mention it since my PR would only address the cases mentioned in the issue:

echo "${someArray[@]}"
echo "${someArray[*]}"
settingDefaultValue="${settingDefaultValue:-"DefaultValue"}"

The above examples wouldn't be properly handled by the \w+ and would need to be addressed by another replace.