#!/usr/bin/env bash

set -o nounset -o noglob +o braceexpand
shopt -s lastpipe
export LC_ALL='C.UTF-8'

tab_spaces=8

sed_expr='s/[[:blank:]]+$//'

test=$'  \tLine with tabs\t why?\t  '

buffer_setting="${1}"

coproc buffer_test {
  stdbuf --output="${buffer_setting}" -- \
      strace -e -o sed-trace.txt \
      sed --binary --regexp-extended --expression="${sed_expr}" |
   stdbuf --output="${buffer_setting}" -- \
       strace -e -o expand-trace.txt \
       expand --tabs="${tab_spaces}"
}

printf '%s\n' "${test}" >&"${buffer_test[1]}"
IFS='' read -r line <&"${buffer_test[0]}"
printf '|%s|\n' "${line//$'\t'/TAB}"

exec {buffer_test[0]}<&- {buffer_test[1]}>&-
wait "${buffer_test_PID}"
