#2 Add coerce.sh for character set coercion
Merged 2 years ago by netvor. Opened 2 years ago by netvor.

file modified
+17
@@ -172,6 +172,23 @@ 

   This sub-package contains 'charmenu', a Shellfu/sh module for building

   interactive terminal menus

  

+ Package: shellfu-sh-coerce

+ Architecture: all

+ Depends: shellfu-sh

+ Description: Shellfu/sh module with data coercion helpers

+  Shellfu is an attempt to add modularity to your shell scripts.

+  .

+  With Shellfu you can develop your shell modules separately from your

+  scripts, and test, use, explore or study them without need to be aware

+  of details such as where the actual files are placed.

+  .

+  Shellfu is mostly intended for cases when there's need for non-trivial

+  amount of reliable body of shell scripts, and access to advanced modular

+  languages such as Python or Perl is limited.

+  .

+  This sub-package contains 'coerce', Shellfu module containing few

+  data coercion helpers.

+ 

  Package: shellfu-sh-exit

  Architecture: all

  Depends: shellfu-sh

file modified
+7
@@ -97,6 +97,13 @@ 

  This sub-package contains 'charmenu', a Shellfu/sh module for building

  interactive terminal menus

  

+ %package sh-coerce

+ Summary: Shellfu/sh module with data coercion helpers

+ Requires: shellfu-sh

+ %description sh-isa

+ This sub-package contains 'coerce', Shellfu module containing few

+ data coercion helpers.

+ 

  %package sh-exit

  Summary: Shellfu/sh module for standard exit statuses

  Requires: shellfu-sh

@@ -0,0 +1,79 @@ 

+ #!/bin/sh

+ 

+ #

+ # Data coercion helpers

+ #

+ # This module provides several simple functions to help transform

+ # data to fit certain constraints.

+ #

+ 

+ #

+ # Replacement character

+ #

+ COERCE__REPCHAR=${COERCE__REPCHAR:-�}

+ 

+ coerce__nocolor() {

+     #

+     # Remove ANSI color codes

+     #

+     perl -CS -Mutf8 -MTerm::ANSIColor=colorstrip -ne 'print colorstrip $_;'

+ }

+ 

+ coerce__noctl() {

+     #

+     # Remove non-printable characters

+     #

+     perl -CS -Mutf8 -pe 'tr|[:graph:]\n\t ||c;'

+ }

+ 

+ coerce__nofdraw() {

+     #

+     # Replace frame-drawing characters with ASCII

+     #

+     # Replace frame-drawing characters according

+     # to following mapping:

+     #

+     #     ┌ ┬ ┐ └ ┴ ┘ ├ ┤ │ ┼ ─

+     #

+     #     ' ' ' . . . | | | | -

+     #

+     # This converts frame-drawing to ASCII, making it

+     # safer when fonts on terminals are not rendering

+     # properly.

+     #

+     perl -CS -Mutf8 -pe "

+         tr{┌┬┐}{.};

+         tr{└┴┘}{'};

+         tr{├┼┤│}{|};

+         tr{─}{-};

+     "

+ }

+ 

+ coerce__for_yaml() {

+     #

+     # Replace yaml-invalid characters

+     #

+     # Yaml won't allow all characters:

+     #

+     # > [...] The allowed character range explicitly excludes the C0 control

+     # > block #x0-#x1F (except for TAB #x9, LF #xA, and CR #xD which are

+     # > allowed), DEL #x7F, the C1 control block #x80-#x9F (except for NEL

+     # > #x85 which is allowed), the surrogate block #xD800-#xDFFF, #xFFFE,

+     # > and #xFFFF.

+     #

+     # so take stdin and replace all unacceptable characters with '�'.

+     #

+     perl -CS -Mutf8 -pe "tr/$(__coerce__for_yaml_bad)/$COERCE__REPCHAR/"

+ }

+ 

+ __coerce__for_yaml_bad() {

+     #

+     # Print all YAML-bad chars

+     #

+     printf '\N{U+0}-\N{U+8}\N{U+B}\N{U+C}\N{U+E}-\N{U+1F}'    # C0 with some gaps

+     printf '\N{U+7F}'                                         # DEL alone

+     printf '\N{U+80}-\N{U+84}\N{U+86}-\N{U+9F}'               # C1 with NEL gap

+     # printf -n '\N{U+D800}-\N{U+DFFF}'                        # surrogates

+     # printf -n '\N{U+FFFE}-\N{U+FFFF}'                        # 0xFFFE and 0xFFFF

+     #FIXME: for some reasons perl complains about these ^^

+ }

For situations when there are constraints on what characters are
allowed, this module provides several non-reversible conversion
functions.

Pull-Request has been merged by netvor

2 years ago