-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
9 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm... no, we don't want to introduce a dependence on Switch. Was there a problem with using coloralias?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Perl simply doesn't have a "case" statement, and that's something we have to live with (there was a
feature switch
, but I think it was experimental and never got through to stable):http://perldoc.perl.org/feature.html#The-%27switch%27-feature
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that switch statement can of course be replaced with a couple of ifs.
I think this made it easier to combine multiple features (like BOLD and RED).
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if-approach would look like this.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I almost forgot I know the answer of this already! Whenever you are dealing with equality to drive behaviour, you can see this as a form of multiple dispatch which Perl can support via... Hashes.
So you would do the behaviour declaration as a package-level var:
And then simply use it:
You can store all kinds of objects as hash values, including pointers to functions, so it's both a convenient and efficient "dispatch table". Much cleaner than the
elsif
chains.eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I then use
$color_scheme{$alias}
directly at the print statement or still delegate its usage to thecolorizeString
function?And how would I use this approach to simulate
return BOLD RED $string;
?eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
BOLD RED $string
actuallyBOLD(RED($string))
? If so, you can make an alias function:and store a reference to it in the hash:
I think invoking it then becomes:
or something on those lines (maybe the & deref is optional?)
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, here is a free book on similar Higher Order Perl tricks if you're curious: http://hop.perl.plover.com/
Perl has all the infrastructure to do pretty neat functional programming, the significant drawback being that it isn't optimized for it and doing clever abstractions costs you heavily in runtime.
I am only sincerely advertising the hash-based dispatch in general, since that's heavily optimized and O(1).
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for the suggestion!
The problem with the dispatch table approach is that it seems to be impossible to combine both constants and function refs easily.
Consider
and the call
this works for function refs but throws
for the constant RED.
These constants are defined as a list
in
... core_perl/Term/ANSIColor.pm
.eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it looks like you'll need an extra
ref
check to figure out if you are looking at a function or at a constant instead. Maybe it's easier to define a BOLDRED constant instead of making a function? That would make it consistent.eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd recommend something like
For some reason
throws a syntax error.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you need
&$spec($string)
? But yes, that's what I meant.eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, that was it!
But .. ehm .. why? Do tertiary operators require special magic?
Looks much nicer now :-)
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure... I am actually wondering if it would still work if you skip the
ref
check and just always do&$spec($string)
. Shouldn't that also work for the function args?Btw, another curious link to check out - it's undecidable to figure out the abstract syntax tree of a Perl program before runtime:
http://www.perlmonks.org/?node_id=663393
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually yes :-)
The function now looks nice and cozy:
That link is super interesting!
That property is not specific to Perl, is it? It works for any language which allows ambiguous function signatures?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the context sensitivity that makes it undecidable and most languages don't venture in that territory. In other words, all it takes to avoid this problem is to make it mandatory to provide function arguments, so that you always write
foo()
when you invoke the function foo, even when there are no arguments.Because Perl tries to be super smart and lets authors be lazy with specifying arguments, you end up with these problematic cases.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tips Deyan! Just did something similar to a snippet in sTeX.
BTW I was looking at
switch
as well, but gave it up, because of its fallacy plus no one else has (yet?) introduced it into sTeX or LaTeXML.Getting ride of if and elsif alike : )
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to this discussion it might be enough to simply check if the streams are ttys or not (and en-/disable colors accordingly).
A way of doing this in perl seems to described here.
What do you think?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that would work.
Is it enough to only check STDERR, or does LaTeXML also do some STDOUT related stuff?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love
-t STDERR
it's quite elegant.But of course I have bad news :>
When the LaTeXML.pm flow "binds" all logging to a handle, it doesn't know in advance whether the passed logging information will be passed into a terminal, or into a file/server socket.
The decision is deferred to the executable - but in the current state of latexml, latexmlpost and latexmlmath, which don't use LaTeXML.pm, the solution you have in mind will work great.
However, for latexmlc, latexmls and ltxmojo, you will never pass on coloured output, as they bind the log in advance to a non-terminal filehandle. That's probably fine, since they almost never need coloring, but the case where latexmlc writes into the original STDERR will remain uncolored.
I am quite happy that
-t
enforces coloring only when there is 100% certainty there is a write to a terminal, which is quite safe for the exotic scenarios.eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That being said, given that there is theoretical desire to move towards latexmlc as a default executable, we should find a way to make it work.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that "non-terminal filehandle" have any special properties which could be detected?
If they behave similar to just piping into a file, there doesn't seem to be a way of detected it, right?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I just talked to the guy who wrote latexmlc, apparently when it's really writing to STDERR it doesn't bind it. No problems at all.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only cases where we're binding STDERR away from the terminal is when the log is passed back via some channel, and in those cases it should never be colored anyway. Possibly accidentally good design, but good design nevertheless.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am already using BOLD to distinguish "error" from "fatal" (both are red). Should I additionally underline "fatal" instead?
Also, this does probably also depend on the used font, terminal, etc. (in my terminal I can not see a difference between with and without BOLD due to my weird font setup :( )
Here's how it looks with bold everywhere.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using orange for error and red for Fatal in CorTeX, but that was also experimental. And not sure you can emulate orange here.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could make fun bits, like successful math parses, green :-)
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Math parsing succeeded:
part, or the individual elements?Again, the whole
Symbols assumed as simple identifiers (with # of occurences):
, or each individual symbol?eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The yellow is somehow interesting. But I have seen it used for warnings, so Kim's screenshot looks pretty neat.
And indeed, it would be quite cool to have the final summary colored.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more comment, this time separate from this PR - while you're at it can you check whether there is an easy way to use coloring for the output of MakeMaker? I had checked some time back but couldn't find anything. I am quite used to colored test results from tool such as cmake or crate, would love to see that in LaTeXML.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering whether I should make the 'ltx:..' stuff colored as well.
Does the yellow look different for you?
Googling and looking through their repo don't show promising results. So I'd assume rather not :-(
But yes, cmake builds always look beautiful 😁
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
ltx:XMath:
andltx:XMArg:
also be colored, or only the corresponding numbers (0/1
,6/6
)?eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy, happy Wednesday to everyone!
eb9bda4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉