xdg-icon-resource install Windows .ico icon on Linux

Requirements:

  • icoutils – for converting .ico to .png
  • python
  • xdg-icon-resource
  • import os
    import sys
    from os import popen, path
    
    
    def run(cmd):
        print(f'Running {cmd} ...')
        output = popen(cmd).read()
        return output
    
    def pngsize(pngfile):
        return pngfile.split('_')[-1].split('x')[0]
    
    
    if __name__ == '__main__':
        icofile = sys.argv[1]
        iconname = icofile.replace(".ico", "").replace("-", " ")
    
        if path.exists('tmp'):
            run('rm -rf tmp')
    
        os.mkdir('tmp')
    
        run(f'icotool -x  {icofile} -o tmp')
    
        pngfiles = os.listdir('tmp')
    
        for pngfile in pngfiles:
            run(f'xdg-icon-resource install --novendor --size {pngsize(pngfile)} tmp/{pngfile} "{iconname}"')Code language: JavaScript (javascript)

    Usage:

    python xdg_icon_resource_install_ico.py folder-pictures.icoCode language: CSS (css)

patch/git apply – Hunk #hunkNumber FAILED at lineNumber (different line endings).

This error commonly issues with .patch files when the patch and target file use different line ending types (i.e. Windows/DOS/CLRF vs. Unix/LF):

touch .build/x64/status/clone
cd .build/x64/src/sshfs && for f in /cygdrive/c/github.com/sshfs-win/patches/*.patch; do patch --binary -p1 <$f; done
patching file sshfs.c
Hunk #1 FAILED at 365 (different line endings).

The unix2dos / dos2unix and commands can be used to perform conversion. One way to determine the line endings type is to open the files in a text editor like Notepad++ which can indicate the line endings type. But this can also be achieved using the unix2dos command’s -i / --info option:

dos2unix -i 00-passwd.patch
      36       0       0  no_bom    text    00-passwd.patch

The 36 indicates the number of lines that use DOS-style line endings. Here is a wrapper python script for this which prints out the line endings type:

import os
import sys
import re

if __name__ == '__main__':
    filename = sys.argv[1]
    infooutput = os.popen(f'dos2unix -i {filename}').read().strip()
    doslines = int(re.split(r'\s+', infooutput)[0])
    line_ending_type = 'Windows/DOS (CR LF)' if doslines > 0 else 'Unix (LF)'
    print(line_ending_type)Code language: JavaScript (javascript)

convert an .svg icon to .ico that contains all icon sizes

Requirements:

param(
    [Parameter(Mandatory=$true,Position=0)]
    [Alias("s")]
    $SvgFilename,
    [Parameter(Mandatory=$false,Position=1)]
    $IcoFilename = ""
)
if (-not [System.IO.Directory]::Exists("tmp")) {
    New-Item -Type Directory -Path tmp
}
$sizes = (16, 20, 24, 32, 40, 48, 64, 128, 256)
$filename = [System.IO.Path]::GetFileNameWithoutExtension($SvgFilename)

for ($i = 0; $i -lt $sizes.length; $i++) {
    $size = $sizes[$i];
    Write-Host $sizes[$i]
    Write-Host $filename
    inkscape -z -e tmp/${filename}_${size}.png -w $size -h $size -d 300 $SvgFilename
}

if ($IcoFilename.length -eq 0) {
    $IcoFilename = $SvgFilename.Replace(".svg", ".ico")
}
if (-not $IcoFilename.EndsWith(".ico")) {
    $IcoFilename = "${IcoFilename}/${filename}.ico"
}
Write-Host "Writing $IcoFilename"
convert tmp/${filename}_16.png tmp/${filename}_20.png tmp/${filename}_24.png tmp/${filename}_32.png tmp/${filename}_40.png tmp/${filename}_48.png tmp/${filename}_64.png tmp/${filename}_128.png tmp/${filename}_256.png ${IcoFilename}
Remove-Item -Path tmp -Recurse
Write-Host "Successfully wrote $IcoFilename"Code language: PHP (php)

Usage:

svg2ico.ps1 device-harddisk-usb.svg device-harddisk-usb.icoCode language: CSS (css)

Sysinternals Suite for Windows: a collection of CLI and GUI system utilities

Sysinternals Suite is a set of command-line and GUI tools for Windows.

Sysinternals Suite - Process Monitor UI

The default download (for x64/x86 CPUs) contains both 64- and 32-bit binaries. If you are using a 64-bit machine, you can run the following PowerShell script in your directory of the extracted zip files to have the default .exe file be a symlink to the 64-bit version rather than the 32-bit executable:

$files = (ls)
$x64s = @{}
$x86s = @{}
$x64sTox86s = @{}

for ($i = 0; $i -lt $files.Length; $i++) {
    $filename = $files[$i].Name
    if ($filename.EndsWith("64.exe")) {
        $x64s[$filename] = $true
    } elseif ($filename.EndsWith(".exe")) {
        $x86s[$filename] = $true
    }
}

foreach ($x64 in $x64s.Keys) {
    $x86 = $x64.Replace("64.exe", ".exe")
    if ($x86s.ContainsKey($x86)) {
        $x64sTox86s[$x64] = $x86
    }
}

foreach ($x64 in $x64sTox86s.Keys) {
    $x86 = $x64sTox86s[$x64]
    Remove-Item -Path $x86
    New-Item -Type SymbolicLink -Path $x86 -Target $x64
}Code language: PHP (php)

Change process’s priority in Windows

The simplest means to do this is by simply opening Task Manager in Windows and navigating to the Details pane. Right-click on a process in the list, then select Set priority from the menu.

Alternatively, similar to the Linux renice command, the priority can also be set via command-line in Windows using

wmic process where name="devenv.exe" CALL setpriority "high priority"Code language: JavaScript (javascript)

The aforementioned methods will not persist upon system restart. Permanently setting the default priority of a process can be achieved using a registry script. This file can contain as many entries as desired. In this example below, I set the default process priority of devenv.exe and sqlservr.exe to High 00000003:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\devenv.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\devenv.exe\PerfOptions] 
"CpuPriorityClass"=dword:00000003

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sqlservr.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sqlservr.exe\PerfOptions] 
"CpuPriorityClass"=dword:00000003

Compressing PNG images with pngout, optipng, and advpng

Screenshot tools for Linux and macOS generally produce image files in the .png file format. PNG is a lossless compression format, meaning that it can be compressed without losing quality, which is unlike another commonly used image format, JPG. There are 3 command-line tools which can be used to compress PNG files, pngout, optipng, and advpng.

➜  pngout terminator-terminal-window.png 
 In:   11405 bytes               terminator-terminal-window.png /c6 /f5
Out:    8790 bytes               terminator-terminal-window.png /c2 /f5
Chg:   -2615 bytes ( 77% of original)
➜  optipng -o7 terminator-terminal-window.png 
** Processing: terminator-terminal-window.png
800x266 pixels, 3x8 bits/pixel, RGB+transparency
Input IDAT size = 8715 bytes
Input file size = 8790 bytes

Trying:
  zc = 9  zm = 9  zs = 0  f = 0		IDAT size = 8140
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 8140
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 8140

Output IDAT size = 8140 bytes (575 bytes decrease)
Output file size = 8215 bytes (575 bytes = 6.54% decrease)

➜  advpng -z4 terminator-terminal-window.png 
        8215        7232  88% terminator-terminal-window.png
        8215        7232  88%

vim and spaces/tabs

vim is my favorite cli text editor. It doesn’t require much configuration, but if you edit code and use spaces in place of tabs, you probably want to add a line to your ~/.vimrc:

set tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab

Of course, choose the number of spaces that suits your needs. 🙂