F# - card suits not displaying in console - unicode

I'm making console based card game in f# and I'm struggling with displaying card suits using unicode chars. Mapping suit-to-char is represented as following function:
let suitSymbol = function
| Spades -> "\u2660"
| Clubs -> "\u2663"
| Diamonds -> "\u2666"
| Hearts -> "\u2665"
Displaying this using
printf "%s" <| suitSymbol Spades
works fine in fsi:
but when compiled using fsc.exe it displays diffrent (not suit) chars:
I've tried changing encoding of source file but to no effect. Is there any way for it to work when compiled?
EDIT (30.01.2017):
Stuart's anwser was correct, but I couldn't get over fact, that It required to enter
chcp 65001
every time I wanted to run my game.
After studying ways of referencing DLLs within F#, I came up with following solution:
module Kernel =
[<DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
extern bool SetConsoleOutputCP(uint32 wCodePageID)
And in main function code I've added
[<EntryPoint>]
let main args =
Kernel.SetConsoleOutputCP 65001u |> ignore
It modifies code page for this process only, so other apps will behave normally.

In your command prompt you will need to change your code page like this:
chcp 65001
After some testing I was able to reproduce your issue, and this fixes it. Credit to #s952163

Related

vscode .net interactive notebooks to display latex

I have been looking for a way to display latex in vscode .Net interactive notebooks, but I've had no luck.
on my C# .Net interactive Code Cell I have
#r "nuget: AngouriMath.Interactive, 1.3.0"
using AngouriMath.Interactive;
using AngouriMath;
Entity entity= "X^2+1";
var latexText = entity.Latexise();
display(latextText,"text/latex") //<< this will not work because "text/latex" mimetype is not valid.
I found this for image https://stackoverflow.com/a/70850956/7197556 where the bitmap image is converted to base64string before being passed to the display() function. Is there a way to do this for latex? or are there any other straightforward solutions?
side-note: I'd like to avoid writing javascript/html code cells as much as possible.
Using visualization for AngouriMath
You should be able to do it without display. In Notebooks you can visualize any expression just by running a cell with it, see the screenshot
By the way, there's still some problem with support of F# in .NET Interactive extensions, so the kernel might fail to load. In case it fails, you can either activate it manually:
AngouriMath.InteractiveExtension.KernelExtension.applyMagic()
Or install the last preview version:
#i "https://www.myget.org/F/angourimath/api/v3/index.json"
#r "nuget:AngouriMath.Interactive, 0.0.0-*"
Using Interactive LaTeX visualization
If you want to display LaTeX without AngouriMath, you can use MathString. Just create an instance of it, and it will be displayed for you:

How to decode HEX code to regular text in Apple Script

I'm using the todo list app Things from Cultured Code on my Mac and when I copy a link to a task to clipboard it ends up encoded as HEX code in the clipboard. No problem when I paste it into a text file – it then shows as decoded text.
But, I need to use the clipboard content in an AppleScript and have difficulties decoding it to plain text there.
I have tried multiple subroutines but they did not work in my case. Most examples that I found online deal with simple encoded URLs. And the code that I have so far works to decode for example "0348" correctly to the number 1000 but my script cannot decode the encoded Things link (that long line of numbers at the top).
Can somebody help me please?
Here's what I have so far:
-- The link to a task in THINGS, encoded: 7468696e67733a2f2f2f73686f773f69643d41463645303746462d394230462d343539332d423143332d313846303337434237363836
-- Above link to the task in THINGS, unencoded: things:///show?id=AF6E07FF-9B0F-4593-B1C3-18F037CB7686
-- Online converter: http://www.unit-conversion.info/texttools/hexadecimal/
-- Number 1000 encoded: 03E8
set theEncodedText to "03E8"
set theDecodedText to (do shell script "perl -e 'printf(hex(\"" & theEncodedText & "\"))'") as string
set theDisplayedText to theDecodedText
display dialog theDisplayedText
Thank you,
Martin
The following example AppleScript code is a proof of concept of how I'd handle setting to a variable a Things URL link that's saved to the clipboard as «data url ...» where ... is Hex Data.
This script writes the «data url ...» to a temporary file, reads the temporary file, which is now a text string of the «data url ...», and sets it as a the value of a variable, and then deletes the temporary file. It then displays the Things URL link as a text string or displays a message that the clipboard did not contain a 'things:///show?id=' URL link.
Example AppleScript code:
if ((clipboard info) as string) contains "URL" then
set thingsURL to "/tmp/thingsURL.tmp"
try
set f to open for access thingsURL with write permission
set eof f to 0
write (the clipboard) to f
close access f
on error
close access f
end try
set thingsURLtext to (read thingsURL)
tell application "System Events" to delete file thingsURL
display dialog thingsURLtext buttons {"OK"} default button 1
else
display dialog "The clipboard did not contain a 'things:///show?id=' URL link." buttons {"OK"} default button 1
end if
Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

xtext writing and interpreter

currently I am writing an interpreter for my DSL which is launched from a menu in a eclipse application . The interpreter read the active file with:
IXtextDocument document = ((XtextEditor) activeEditor).getDocument().get();
(return a String)
Then I read line by line with a scanner the active file , but is really hard , reading all lines and make switches and ifs for every possibility for their execution .
is there a way to use my grammar information(know what is the line: declaration , function , ...) while I read the active file in the eclipse application ? , or is there a better way to implement this without reading the file like a normal string?
IXtextDocument implements IReadAccess<XtextResource> interface, so you should be able to call editor.document.readOnly(...), like here: https://github.com/eclipse/xtext-eclipse/blob/01ffa30f58160e4c13cb193da3d1e21a40e0d508/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/SimpleEditorTest.java#L64

Ipython Notebook writefile and execute cell at the same time

When using the magic cell %%writefile, it seems that ipython treat the content of the cell as plain txt and will not execute it.
Is there any way to run the code in the cell and then automatically export it to some script file?
This is useful because it gives you the ability to programmatically sync your notebook with a script that only keep some parts of the notebook.
In the notebook, some codes are only scaffolds, downloading the notebook as a py script will include these unnecessary codes. Meanwhile, you may want to re-structure the document, put two code blocks together while in the notebook there are a lot of draft content between them.
Ugly, all of the above things can be done by copying codes manually. But it will make future maintenance full of tedious recopy and paste work.
Thanks to author for answer. I think full code will be useful for others:
from IPython.core.magic import register_cell_magic
#register_cell_magic
def write_and_run(line, cell):
argz = line.split()
file = argz[-1]
mode = 'w'
if len(argz) == 2 and argz[0] == '-a':
mode = 'a'
with open(file, mode) as f:
f.write(cell)
get_ipython().run_cell(cell)
You can declare this in your Jupiter notebook. Mark cells like:
%%write_and_run some.py
or
%%write_and_run -a some.py
Well, seems that those using ipython don't care my question. I think I have sorted out the solution by myself.
Actually, you can write your own magic command following this post. The key command to use related to ipython is
self.shell.run_cell(cell)
You can write a magic command that firstly executes the cell using the above command and then save the cell content to some file. Ipython will pass the line and cell args as unicode object.
I have implemented one that does the following job.
run the code
write the code to some file given by -f arg
if a -before arg(a str) is given, then put the code just before that str.
if an -indent arg is given, then write a indented version of cell

Why would PDF::FromHTML behave differently when called from my web app?

I'm using PDF::FromHTML to generate a PDF from HTML(as the name of the module would imply) :)
I'm able to run it from the command line fine and receive my expected output - however when I use the exact same code in my web app, the output doesn't look correct at all - text is appearing in the wrong places and not all of it is appearing.
I am using the exact same input file in the web app and on the command line - some reason when it's called from inside my web app, it's appearing differently.
Here is the code:
use PDF::FromHTML;
my $filename = '/tmp/backup.html';
my $font = 'Helvetica';
my $encoding = 'utf-8';
my $size = 12;
my $landscape = 0;
my $pdf = PDF::FromHTML->new(
encoding => $encoding,
);
my $input_file = $filename;
my $output_file = "$input_file.pdf";
warn "$input_file\n$output_file\n";
$pdf->load_file($input_file);
$pdf->convert(
Font => $font,
LineHeight => $size,
Landscape => $landscape,
);
$pdf->write_file($output_file);
The web app code is the same, just with that block thrown into a method.
I have looked at the two generated PDF files in a hex editor and found the differences. They're the same until a block whose intent I can't understand...
Good PDF contents at that block:
/Length 302 >> stream
**binary data
endstream endobj
10 0 obj << /Filter [ /FlateDecode ] /Length 966
Bad PDF contents:
/Length 306 >> stream
**binary data
endstream endobj
10 0 obj << /Filter [ /FlateDecode ] /Length 559
As you can see, the length of the content contained in here differs, as does the binary data contained in that stream(the length 302 vs length 306 one) as well as the next stream(the length 966 vs 559 one).
I'm not entirely sure what could be causing this discrepancy, the only thing I can think of is some sort of difference in the environments when I'm running this as my user on the command line versus running it from the web app. I don't know where I should start with debugging that, however.
In general, the CGI environment is different than your interactive login environment just like someone else's login environment is different than yours. The trick is to figure out what thing you have set or unset on your command line that makes your program work.
You might want to see my Troubleshooting Perl CGI scripts for a step-by-step method to track down these problems.
Some things to investigate:
Is your CGI script running on the same platform (i.e. is it a Windows versus Unix sorta thing)
What's different about the environment variables?
Does your CGI script use the same version of Perl?
Does that perl binary have different compilation options?
Are you using the same versions of the modules?
If some of those modules use external libraries, are they the same?
A useful technique is to make your login shell temporarily have the same setup as your CGI environment. Once you do that, you should get the same results on the command line even if those results are wrong. However, once you get the wrong results you can start tracking it down from the command line.
Good luck.
Couple of suggestions:
PDF::FromHTML uses PDF::Writer, which in turn uses a PDF rendering library as a plugin (think the options are PDFLib and some others). Are the same version of the libraries available as plugins?
Does your HTML input file have a CSS file that you haven't uploaded?
Try setting the other PDF::FromHTML variables: PageWidth, PageResolution, PageSize etc
Is the ordering of the output text different or merely the postions? If it's position then try setting the PageWidth etc as the library being used (PDFLib or whatever) may pick different defaults between the two environments. If ordering is wrong then I have no idea.
The two PDF blocks you posted don't really show much - just shows that the compressed sections are of different sizes. There's nothing actually wrong syntactically with either example.
Maybe there is some encoding problem? Have a look at the headers.
I would take a good look at what user the Web server is running as and what that users environment variables look like. Also pay attention to that users permissions on the directories. Also are there other things limiting the web server users such as SElinux on a linux box?