From 77dd6b1d66d5f906aa1d7939e8be264802959441 Mon Sep 17 00:00:00 2001 From: Ev Bogdanov Date: Sat, 19 Aug 2017 17:38:08 +0300 Subject: [PATCH] AWK is awesome! --- bin/t2s | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/bin/t2s b/bin/t2s index 9a1fecd..13379da 100755 --- a/bin/t2s +++ b/bin/t2s @@ -1,26 +1,42 @@ -#!/usr/bin/env python3 +#!/usr/bin/env bash -import sys +awk -v n_spaces="$1" ' -DEFAULT_N_SPACES = 4 -TAB, SPACE = ' ', ' ' +BEGIN { + tab = " " + space = " " -try: - n_spaces = int(sys.argv[1]) -except: - n_spaces = DEFAULT_N_SPACES + if (n_spaces < 1) { + n_spaces = 4 + } -if n_spaces < 1: - n_spaces = DEFAULT_N_SPACES + spaces_per_tab = "" + for (i = 1; i <= n_spaces; i += 1) { + spaces_per_tab = spaces_per_tab space + } +} -for line in sys.stdin: - line = line.rstrip() - n_tabs = 0 +function spaces_for_n_tabs(n) { + spaces = "" + while (n--) { + spaces = spaces spaces_per_tab + } + return spaces +} - for ch in line: - if ch != TAB: - break - n_tabs += 1 +function tabs_to_spaces(line) { + n_tabs = 0 + for (i = 1; i <= length(line); i += 1) { + if (substr(line, i, 1) != tab) { + break + } + n_tabs += 1 + } + return spaces_for_n_tabs(n_tabs) substr(line, i) +} - spaces = SPACE * n_spaces * n_tabs - print(spaces, line[n_tabs:], sep='') +{ + print tabs_to_spaces($0) +} + +'