Template/mixin ideas?

Anonymouse asdf at asdf.net
Fri Oct 5 09:51:32 UTC 2018


On Wednesday, 3 October 2018 at 11:01:53 UTC, Chris Katko wrote:
> I've got this simple task but I'm trying to perfect it as best 
> I can to learn something in the process.
>
> I have Linux terminal ASCII codes for coloring terminal output.
>
> string red(string) { /* ... */ }
>
> "Hello world".red => "\033[31mHello World\033[0m"
>
> which translates to "[red]Hello World[reset to normal text]".
>
> I have to do some slight trickery so I can chain them. But it 
> all works fine. __The function is the same__ no matter what 
> kind of color, bold, etc attribute I want. The only difference 
> is the tag/prefix string.
>
> So I have a table (or enum):
> enum colors{
>      reset = "\033[0m",
>      red = "\033[31m",
>      bold = "\033[1m" //...
>      }
>
> Absolute perfection would be some way to add a single line to 
> that enum (or table) and magically get a new function. I add 
> "blue" with its prefix code to the enum and immediately I can 
> do:
>
> "hello world".blue
>
> Add yellow = "\033..." and I can do:
>
> "hello world".bold.yellow

https://run.dlang.io/gist/run-dlang/e0d0bcebe6c4edcc3cd0c2858183357d?compiler=dmd

https://issues.dlang.org/show_bug.cgi?id=19286 prevents us from 
using static foreaches to declare the aliases.


More information about the Digitalmars-d-learn mailing list