Setting encoding - Write-Host - Powershell - powershell-2.0

I just want to set encoding for the 'Write-Host' command in powershell, version 2.0, but there is no option available. How to do this.

Setting $OutputEncoding = [System.Text.Encoding]::UTF8 did not produce the right output characters for me.
Encoding the PowerShell script file as UTF8-BOM worked, without any further settings.

The output encoding from PowerShell cmdlets is controlled by the $OutputEncoding variable, which is by default set to ASCII.
You can try to change it to say UTF8:
$OutputEncoding = [ System.Text.Encoding]::UTF8

Related

Jenkins Pipeline: How to write UTF-8 files with writeFile?

I'm hoping this is not a bug and that I'm just doing something wrong. I have a Jenkins (v2.19.1) Pipeline job and in it's groovy script, I need to search and replace some text in an existing text file, on a Windows node.
I've used fart.exe and powershell to do the search and replace, but I would really like to do this with just the groovy in Jenkins and eliminate dependency on fart/powershell/etc. and make this code more reusable on both linux and windows nodes.
After much googling and trying various approaches, the closest I got was to use readFile and writeFile. However, I've not been able to get writeFile to create a UTF-8 file. It creates an ANSI file even when I specify UTF-8 (assuming I'm doing it correctly).
Here's what I have so far...
def fileContents = readFile file: "test.txt", encoding: "UTF-8"
fileContents = fileContents.replace("hello", "world")
echo fileContents
writeFile file: "test.txt", text: fileContents, encoding: "UTF-8"
I've confirmed with multiple text editors that the test.txt file is UTF-8 when I start, and ANSI after the writeFile line. I've tried all combinations of including/not-including the encoding property and "utf-8" vs "UTF-8". But in all cases, the file is written out as ANSI (as reported by both Notepad++ and VS Code). Also, a question mark (HEX 3F) is added as the very first character of the file.
The echo line does not show the extra 3F character, so it seems the issue is in the writeFile line.
Your code looks correct. See that ANSI and UTF-8 are the same if you are using just non accented chars and numbers. Try to have some accented letters (áéíóúç) in your file that the editor will probably recognize it as an UTF-8 file.
You must use the Jenkins pipeline writeFile function. This is a special Jenkins method to write files inside your workspace. The default Java File objects won't work.
To specify the encoding, you must use named parameters. Here is a an example:
writeFile(file: "filename.txt", text: "áéíóú", encoding: "UTF-8")
This will create at the root of your workspace, a file named filename.txt with "áéíóú" as content and encoded as UTF-8.
BTW, if you have full control of the file you must search and replace, consider using Groovy's builtin SimpleTemplateEngine

Print special characters in console

In my console (XP), the following line echo "áéíóú" in a utf-8 encoded file prints this:
├í├®├¡├│├║
Im not sure if this is something i have to handle in nim.
I am not sure if it works on Windows XP, but if you have Windows 8.1 your Nim code should be fine provided that in your console you change code page to UTF-8 with the following command:
chcp 65001
You need Lucida Console font too.
Keep in mind that in the Output window of Aporia editor you don't need to change anything in order to see the correct characters.

"C:/Program" is not recognized as an internal or external command - Setting up Lua with LOVE and Sublime text 2

I am trying to use the LOVE graphics library for Lua, and use Sublime text 2 to write in. The LOVE website says i can use this code:
{
"selector": "source.lua",
"cmd": ["C:/Program Files/LOVE/love.exe", "$file_path"],
"shell": true
}
to create a build system that will allow me to run my scripts directly from inside Sumblime text. but when i use this build system with ctrl+B it says:
" 'C:/Program' is not recognized as an internal or external command, operable program or batch file."
it is stopping at the space in "Program Files". how do i stop it doing this?
The problem is the space in the filename that is being wrongly used as a parameter delimiter.
Depending on your environment you can either double quote the executable name or use dir /x to get the short (8.3) filename for the path.
You can just move "LOVE" directory in C Drive like
C:\LOVE
It will work.

How do I make sure that a directory name is quoted in OMake?

I have a relatively complicated suite of OMake files designed for cross-compiling on a specific platform. My source is in C++.
I'm building from Windows and I need to pass to the compiler include directories which have spaces in their names. The way that the includes string which is inserted in the command line to compile files is created is by the line:
public.PREFIXED_INCLUDES = $`(addprefix $(INCLUDES_OPT), $(set $(absname $(INCLUDES))))
At some other point in the OMake files I have a line like:
INCLUDES += $(dir "$(LIBRARY_LOCATION)/Path with spaces/include")
In the middle of the command line this expands to:
-IC:\Library location with spaces\Path with spaces\include
I want it to expand to:
-I"C:\Library location with spaces\Path with spaces\include"
I don't want to change anything but the "INCLUDES += ..." line if possible, although modifying something else in that file is also fine. I don't want to have to do something like change the definition of PREFIXED_INCLUDES, as that's in a suite of OMake files which are part of an SDK which may change beneath me. Is this possible? If so, how can I do it? If not, in what ways can I make sure that includes with spaces in them are quoted by modifying little makefile code (hopefully one line)?
The standard library function quote adds escaped quotes around its argument, so it should do the job:
INCLUDES += $(quote $(dir "$(LIBRARY_LOCATION)/Path with spaces/include"))
If needed, see quote in Omake manual.
In case someone else is having the same problem, I thought I'd share the solution I eventually went with, having never figured out how to surround with quotes. Instead of putting quotes around a name with spaces in it I ended up converting the path to the short (8.3) version. I did this via a a simple JScript file called shorten.js and a one line OMake function.
The script:
// Get Access to the file system.
var FileSystemObject = WScript.CreateObject("Scripting.FileSystemObject");
// Get the short path.
var shortPath = FileSystemObject.GetFolder(WScript.Arguments(0)).ShortPath;
// Output short path.
WScript.StdOut.Write(shortPath);
The function:
ShortDirectoryPath(longPath) =
return $(dir $(shell cscript /Nologo $(dir ./tools/shorten.js) "$(absname $(longPath))"))
So now I just use a line like the following for includes:
INCLUDES += $(ShortDirectoryPath $(dir "$(LIBRARY_LOCATION)/Path with spaces/include"))

Iconv.conv in Rails application to convert from unicode to ASCII//translit

We wanted to convert a unicode string in Slovak language into plain ASCII (without accents/carons) That is to do: č->c š->s á->a é->e etc.
We tried:
cstr = Iconv.conv('us-ascii//translit', 'utf-8', a_unicode_string)
It was working on one system (Mac) and was not working on the other (Ubuntu) where it was giving '?' for accented characters after conversion.
Problem: iconv was using LANG/LC_ALL variables. I do not know why, when the encodings are known, but well... You had to set the locale variables to something.utf8, for example: sk_SK.utf8 or en_GB.utf8
Next step was to try to set ENV['LANG'] and ENV['LC_ALL'] in config/application.rb. This was ignored by Iconv in ruby.
Another try was to use global system setting in /etc/default/locale - this worked in command line, but not for Rails application. Reason: apache has its own environment. Therefore the final solution was to add LANG/LC_ALL variables into /etc/apache2/envvars:
export LC_ALL="en_GB.utf8"
export LANG="en_GB.utf8"
export LANGUAGE="en_GB.utf8"
Restarted apache and it worked.
This is more a little how-to than a question. However, if someone has better solution I would like to know about it.
You can try unaccent approach instead.

Resources