<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>code on MXI</title><link>https://mxii.eu.org/categories/code/</link><description>Recent content in code on MXI</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 01 Apr 2020 21:45:11 +0300</lastBuildDate><atom:link href="https://mxii.eu.org/categories/code/index.xml" rel="self" type="application/rss+xml"/><item><title>A case of adapting a CLI test command from *NIX to Windows</title><link>https://mxii.eu.org/2020/04/01/a-case-of-adapting-a-cli-test-command-from-nix-to-windows/</link><pubDate>Wed, 01 Apr 2020 21:45:11 +0300</pubDate><guid>https://mxii.eu.org/2020/04/01/a-case-of-adapting-a-cli-test-command-from-nix-to-windows/</guid><description>&lt;p>There is a package I wanted to publish to npmjs.com. That package provides a CLI tool among other features, and contains tests for that tool. Unfortunately, tests were made with only *NIX in mind, so it wasn&amp;rsquo;t possible to pass all tests and publish the package from Windows.&lt;/p>
&lt;p>The main culprit is inside the following function:&lt;/p>
&lt;pre>&lt;code class="language-javascript">function runWithInputAndExpect (input, args, expectedOutput, done) {
var command = 'echo &amp;quot;' + input.replace(/&amp;quot;/g, '\\&amp;quot;') + '&amp;quot; | node bin/cli.js ' + args;
exec(command, function callback (error, stdout, stderr) {
expect(error).to.be.a('null');
expect(stderr).to.equal('');
expect(stdout).to.equal(expectedOutput + '\n');
done(error);
});
}
&lt;/code>&lt;/pre>
&lt;p>What makes the difference between *NIX and Windows (as far as we concerned here) is how &lt;a href="https://ss64.com/nt/echo.html">&lt;code>echo&lt;/code> command&lt;/a> works. First of all, it keeps the quotes! But after we try to remove them - we will soon discover that the rabbit hole might be quite deep.&lt;/p>
&lt;p>And down the rabbit hole we go:&lt;/p></description></item><item><title>PowerShell, square brackets and apostrophes</title><link>https://mxii.eu.org/2018/01/08/powershell-square-brackets-and-apostrophes/</link><pubDate>Mon, 08 Jan 2018 02:15:11 +0300</pubDate><guid>https://mxii.eu.org/2018/01/08/powershell-square-brackets-and-apostrophes/</guid><description>&lt;p>This is a follow-up post to the one about &lt;a href="https://mxii.eu.org/2017/03/19/powershell-explorer-square-brackets-and-unicode-paths/">square brackets and Unicode&lt;/a> in PowerShell.&lt;/p>
&lt;p>I discovered recently that there is another PITA character that interferes with my workaround &amp;mdash; it&amp;rsquo;s a single quote, or apostrophe.&lt;/p>
&lt;p>It&amp;rsquo;s fine to start PowerShell inside a folder with an apostrophe in its name, but we have to escape it when using inside Powershell commands or passing into PowerShell from anywhere else.&lt;/p>
&lt;p>Below are my experiments to find the best way to open PowerShell in any folder&amp;hellip; if ever possible.&lt;/p></description></item><item><title>JavaScript spoiler with a twist (collapsing up or down)</title><link>https://mxii.eu.org/2017/09/07/javascript-spoiler-with-a-twist-collapsing-up-or-down/</link><pubDate>Thu, 07 Sep 2017 21:28:11 +0300</pubDate><guid>https://mxii.eu.org/2017/09/07/javascript-spoiler-with-a-twist-collapsing-up-or-down/</guid><description>&lt;p>I thought that I will eventually need to use spoilers in this blog to hide long and optional blocks of text. And at the same time I&amp;rsquo;m often unhappy with collapsible block implementations across the Internet.&lt;/p>
&lt;p>So I decided to design my own spoiler with one useful feature: it can be collapsed not only from the top edge but also from the bottom edge &amp;mdash; you will not lose the line you were reading and you don&amp;rsquo;t have to scroll all the way to the beginning of the spoiler.&lt;/p>
&lt;p>Examples:&lt;/p>
&lt;div class="spoiler">
&lt;div class="spoiler-btn spoiler-btn-top">&lt;i class="fa-solid fa-chevron-down">&lt;/i> click to &lt;span class="show">show&lt;/span>&lt;span class="hide">hide&lt;/span>&lt;/div>
&lt;div class="spoiler-body">
&lt;p>This spoiler can be expanded up and down. And your mouse is left right over the button you just pressed whenever it is possible (on top and bottom of a page there may be not enough stuff to keep it so, obviously).&lt;/p>
&lt;p>&lt;img src="https://picsum.photos/410/410?random=1" alt="" />&lt;/p>
&lt;/div>
&lt;div class="spoiler-btn spoiler-btn-bottom">click to &lt;span class="show">show&lt;/span>&lt;span class="hide">hide&lt;/span> &lt;i class="fa-solid fa-chevron-up">&lt;/i>&lt;/div>
&lt;/div>
&lt;div class="spoiler simple">
&lt;div class="spoiler-btn spoiler-btn-top">&lt;i class="fa-solid fa-chevron-down">&lt;/i> click to &lt;span class="show">show&lt;/span>&lt;span class="hide">hide&lt;/span>&lt;/div>
&lt;div class="spoiler-body">
&lt;p>This spoiler can be expanded in one direction only. Collapsing behavior is still the same. The difference is just a few lines in CSS.&lt;/p>
&lt;p>&lt;img src="https://picsum.photos/410/410?random=2" alt="" />&lt;/p>
&lt;/div>
&lt;div class="spoiler-btn spoiler-btn-bottom">click to &lt;span class="show">show&lt;/span>&lt;span class="hide">hide&lt;/span> &lt;i class="fa-solid fa-chevron-up">&lt;/i>&lt;/div>
&lt;/div>
&lt;p>Keep reading to see how it is done.&lt;/p></description></item><item><title>PowerShell, Explorer, square brackets and Unicode paths</title><link>https://mxii.eu.org/2017/03/19/powershell-explorer-square-brackets-and-unicode-paths/</link><pubDate>Sun, 19 Mar 2017 00:55:11 +0300</pubDate><guid>https://mxii.eu.org/2017/03/19/powershell-explorer-square-brackets-and-unicode-paths/</guid><description>&lt;p>Let&amp;rsquo;s make a folder &lt;code>C:\[test]&lt;/code> and try to open PowerShell there from Explorer (using Ribbon or context menu).&lt;/p>
&lt;p>&lt;img src="https://mxii.eu.org/post/2017-03-19-powershell-square-brackets/test-doesnt-exist.png" alt="set-location: Cannot find path 'C:[test]' because it does not exist. At line:1 char:1" />&lt;/p>
&lt;p>Oops!&lt;/p>
&lt;p>Let&amp;rsquo;s see what we can do with this.&lt;/p></description></item><item><title>HTML aspect ratio box using Custom Element</title><link>https://mxii.eu.org/2016/12/04/html-aspect-ratio-box-using-custom-element/</link><pubDate>Sun, 04 Dec 2016 23:45:23 +0300</pubDate><guid>https://mxii.eu.org/2016/12/04/html-aspect-ratio-box-using-custom-element/</guid><description>&lt;p>There is a standard trick to make HTML block with given proportions:&lt;/p>
&lt;pre>&lt;code>&amp;lt;div id=&amp;quot;containingBlock&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;wrapper&amp;quot; style=&amp;quot;position:relative; width:100%; height:0px; padding-bottom:56.25%;&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;content&amp;quot; style=&amp;quot;position:absolute; left:0; top:0; width:100%; height:100%&amp;quot;&amp;gt;
...
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>Since padding is computed according to containing element width, it&amp;rsquo;s possible to use it along with width to get required proportions (16:9 in this example, 56.25% = 100% * 9 / 16). And content block is positioned absolutely to use the space taken by padding.&lt;/p>
&lt;p>After learning about &lt;a href="https://developers.google.com/web/fundamentals/getting-started/primers/customelements">Custom Elements&lt;/a> I decided to see if I can abstract the trick and turn it into something clean and reusable. So it could be used just like this:&lt;/p>
&lt;pre>&lt;code>&amp;lt;ratio-box ratio=&amp;quot;16:9&amp;quot;&amp;gt;
...
&amp;lt;/ratio-box&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>Custom Elements is &lt;a href="http://caniuse.com/#search=custom%20element">quite recent&lt;/a> feature in HTML, with version 0 available only in Chrome and version 1 just coming into play. It also requires JavaScript to work. So it is not practical to use yet, but still an interesting experiment.&lt;/p>
&lt;p>See the code and demo below. In addition, there is also a slightly improved HTML+CSS version.&lt;/p></description></item><item><title>Parallel processing in PowerShell</title><link>https://mxii.eu.org/2016/09/16/parallel-processing-in-powershell/</link><pubDate>Fri, 16 Sep 2016 21:05:05 +0300</pubDate><guid>https://mxii.eu.org/2016/09/16/parallel-processing-in-powershell/</guid><description>&lt;p>Running arbitrary number of tasks in parallel with PowerShell, while showing overall progress.&lt;/p>
&lt;p>The main disadvantage of the code I provided in &lt;a href="https://mxii.eu.org/2016/08/17/blog-minification-in-a-fancy-way-with-powershell/">previous post&lt;/a> is that it is performed sequentially. We can speed it up by means of parallel processing, since main limiting factor is not an I/O but minifying algorithms performance.&lt;/p>
&lt;p>There are a lot of ways to use parallelism in PowerShell and it wasn&amp;rsquo;t clear for me which one suits well for my task. I.e. to convert my script, which is processing a lot of small files on a local machine.&lt;/p>
&lt;p>It also should:&lt;/p>
&lt;ul>
&lt;li>provide updates in real time;&lt;/li>
&lt;li>keep output table formatting as close to original as possible.&lt;/li>
&lt;/ul></description></item><item><title>Ellipsis function for PowerShell</title><link>https://mxii.eu.org/2016/09/03/ellipsis-function-for-powershell/</link><pubDate>Sat, 03 Sep 2016 00:07:11 +0300</pubDate><guid>https://mxii.eu.org/2016/09/03/ellipsis-function-for-powershell/</guid><description>&lt;p>&lt;em>One more unintended thing arose from another post.&lt;/em>&lt;/p>
&lt;p>I was looking for a way to fit a lot of lengthy paths into console screen by removing less important parts. &amp;ldquo;&lt;a href="https://en.wikipedia.org/wiki/Ellipsis">Ellipsis&lt;/a>&amp;rdquo; is a standard name for such a thing. You&amp;rsquo;re dealing with it every time you&amp;rsquo;re looking at &amp;ldquo;&amp;hellip;&amp;rdquo; in place of something too long to fit into borders.&lt;/p>
&lt;p>Here is my implementation for PowerShell.&lt;/p></description></item><item><title>Blog minification in a fancy way with PowerShell</title><link>https://mxii.eu.org/2016/08/17/blog-minification-in-a-fancy-way-with-powershell/</link><pubDate>Wed, 17 Aug 2016 00:31:11 +0300</pubDate><guid>https://mxii.eu.org/2016/08/17/blog-minification-in-a-fancy-way-with-powershell/</guid><description>&lt;p>There is no built-in support for minification in Hugo engine, which isn&amp;rsquo;t a bad thing on its own. But I had to figure out a way to minify my files while keeping building and maintenance extremely simple.&lt;/p>
&lt;p>At first point I added &lt;a href="https://github.com/tdewolff/minify/tree/master/cmd/minify">minify&lt;/a> for text files. It&amp;rsquo;s a single binary (written in Go), and with a single call it can run recursively over Hugo&amp;rsquo;s public folder, minifying output files in-place, just as I needed.&lt;/p>
&lt;p>But minifying images is a more tricky task. I decided to use &lt;a href="http://jpegclub.org/jpegtran/">jpegtran&lt;/a> and &lt;a href="http://optipng.sourceforge.net/">optipng&lt;/a>, but they can&amp;rsquo;t be run recursively. Some scripting is required to use them. While the task of finding all the images in a folder and applying minifiers on them is trivial and can be solved with PowerShell one-liner, it was not satisfying enough, so I end up with the thing described below in this post.&lt;/p>
&lt;p>Additional motivation is to have a tool that can be adopted easily to any other processing task and will give me a fancy progress report.&lt;/p></description></item><item><title>Quick note about objects in PowerShell</title><link>https://mxii.eu.org/2016/08/12/quick-note-about-objects-in-powershell/</link><pubDate>Fri, 12 Aug 2016 03:14:15 +0300</pubDate><guid>https://mxii.eu.org/2016/08/12/quick-note-about-objects-in-powershell/</guid><description>&lt;p>&lt;em>I decided to move this note out from another post.&lt;/em>&lt;/p>
&lt;p>It&amp;rsquo;s a kind of summary on a question &amp;ldquo;Why &lt;code>[PSCustomObject]@{}&lt;/code> syntax is the preferred way to create objects in PS&amp;rdquo;. After looking into StackOverflow (&lt;a href="http://stackoverflow.com/questions/14012773/difference-between-psobject-hashtable-and-pscustomobject">one&lt;/a>, &lt;a href="http://stackoverflow.com/questions/35894272/powershell-type-accelerators-psobject-vs-pscustomobject">two&lt;/a>) and some other resources (&lt;a href="https://learn-powershell.net/2014/01/11/custom-powershell-objects-and-performance-revisited/">three&lt;/a>), I decided to test and write it down for myself.&lt;/p>
&lt;p>Nothing really new here. I only hope it will work as a good summary.&lt;/p>
&lt;p>&lt;strong>Update:&lt;/strong> Select-Object examples. Additional links.&lt;/p></description></item><item><title>Ways to measure execution time in PowerShell</title><link>https://mxii.eu.org/2016/08/05/ways-to-measure-execution-time-in-powershell/</link><pubDate>Fri, 05 Aug 2016 21:28:11 +0300</pubDate><guid>https://mxii.eu.org/2016/08/05/ways-to-measure-execution-time-in-powershell/</guid><description>&lt;p>Ok, back to business. No electronics at least for a few posts though.&lt;/p>
&lt;p>It will be clear later why I became interested in this topic. But it is, on its&amp;rsquo; own, worth for a post, so here it is.&lt;/p>
&lt;h1 id="measure-command">Measure-Command&lt;/h1>
&lt;p>The canonical way to measure how long something runs in PowerShell is to use &lt;a href="http://ss64.com/ps/measure-command.html">Measure-Command&lt;/a> cmdlet.&lt;/p>
&lt;pre>&lt;code>&amp;gt; Measure-Command { &amp;quot;do stuff&amp;quot; }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 7436
TotalDays : 8,60648148148148E-09
TotalHours : 2,06555555555556E-07
TotalMinutes : 1,23933333333333E-05
TotalSeconds : 0,0007436
TotalMilliseconds : 0,7436
&lt;/code>&lt;/pre>
&lt;p>One notable downside of this is that you see no output from a command. Time span is returned instead.&lt;/p>
&lt;p>What else can we do?&lt;/p></description></item></channel></rss>