Tahir Hassan's Blog

My Technical Notes

Saturday, 19 August 2017

Settings for an Anki Collection

I use Anki a certain way.

  • Use a single deck I am not sure if this is recommended, but in Mnemosyne you only have a single deck. Use tags to organise content. I tend to rename the default deck to "Deck".
  • Studying New Cards I set the number of new cards on the deck options to be 0 (originally it is 20). This is so I can control the number of new cards that are shown each day. Otherwise, you may be overwhelmed when studying complex information. To study new cards, increase the new card limit by clicking on the deck to go into the deck screen, and click on the Custom Study button at the bottom of the screen then increase the New Card Limit to say 5 cards and then study.
  • "Good" on New cards shows them tomorrow For new cards, Anki will present the same card twice if you click Good (after 10 minutes). Instead I would rather have it that when I select Good, I see it tomorrow. To have this behaviour, in the Deck options window, remove the "10" from the New Cards → Steps textbox.
  • Studying for an Exam When studying for an exam, tag the note with the exam name (e.g `ms-98-375`) and other tags that are relevant to it. Then create a filtered deck (Tools → Create Filtered Deck) and set the search to `tag:ms-98-375`. Rename the filtered deck to something so it is not deleted by Anki. Before studying the filtered deck you can set the limit to the number of cards you wish to study in that session.

Wednesday, 16 August 2017

Getting Comparable Last Modified File Dates in PowerShell and Bash

ISO 8601 has become a defacto standard for giving a text representation of dates. It is used because it has the local time together with the offset. For example, BST (British Summer Time) is an offset of 1 hour, and at the end of an ISO 8601 date, you will see `+01:00`.

For PowerShell/.NET use `LastWriteTime` and use the `o` string format option:


(Get-Item $filePath).LastWriteTime.ToString("o")

For Bash/Unix use `date` command with `-r` switch which we pass a file path to, together with a formatting string to product an ISO 8601 string:


date -r $filePath +%Y-%m-%dT%H:%M:%S%z

(One very minor difference is that the PowerShell date string will have a colon separating the hours and minutes of the offset, but the Bash one won't, but both are ISO 8601 compliant).

To turn the ISO 8601 string into a string:


[DateTime]::Parse($dateString, $null, [Globalization.DateTimeStyles]::RoundtripKind)

Tuesday, 15 August 2017

Using PowerShell to transfer files to and from Android phone

Because USB Mass Storage is now unavailable on Android, and MTP cannot be used from the command-line, the only free option is either to root the device and download a USB Mass Storage app, or instead use `adb` which is part of the Android SDK. I installed Android Studio which also installs the Android SDK.

To find the path of the file you wish to download from the Android phone, use a file manager, which should give you the full path.

* You must enable USB debugging on your device.

Connect your phone to computer using a USB cable. Run command `adb devices` which should list your phone.

To access the bash shell of your phone from your computer, run command `adb shell`. First time you do it, you will get a security dialog on your phone which you have to confirm. You can use this to check that your paths are correct.

Once you have found the full path of your file, you use `pull` to get files from your phone and `push` to copy files to your phone.

For instance, `/storage/emulated/0/data.txt` is a file we wish to copy down to our computer. Do:


adb pull /storage/emulated/0/data.txt

which will result in `data.txt` being copied to the working directory.

To copy files to the Android phone it is the reverse process. Lets say that there is a file `bruno.txt` which you want to copy to a directory on your phone `/storage/emulated/0/ali-g`, then do:


adb push bruno.txt /storage/emulated/0/ali-g

Sources

Monday, 14 August 2017

Git: Pushing while keeping a maximum history of n log items


# truncate the git repository to a history depth of maxHistory
# truncate the git repository to a history depth of maxHistory
$maxHistory = 5;
$history = git --no-pager log --format="%H" -n $maxHistory
if ($history.Count -ge $maxHistory) {
    $lastID = $history[$maxHistory - 1];
    git checkout --orphan temp $lastID
    git commit -m "Truncating history to $maxHistory items"
    git rebase --onto temp $lastID master;
    git branch -D temp;
    # git reflog expire --expire=now --all
    # git gc --prune=now
}
# push the changes to the server
git push -f
Optionally, you can add the following two lines:

git reflog expire --expire=now --all
git gc --prune=now
which forces the expiry of the dangling commits, such as those older than `maxHistory` commits, and then deletes them.

Saturday, 12 August 2017

Anki: Power Format Pack Information

If the Markdown toggle button shortcut is not working `ctrl+shift+0`, then you can change it in the `keybindings_linux_windows` file, which is a simple JSON file. Change the line:


"markdown": "ctrl+shift+0",
to

"markdown": "ctrl+m",
so that you use `ctrl+m` to toggle Markdown mode.

Friday, 21 July 2017

verb `Generate`

... to mean generate something random like a guid or a random number.

Thursday, 13 July 2017

Visual Studio 2017: An Alternative to the DataTable Visualizer

In many environments, the DataTable Visualizer does not work because its DLL must reside in the "My Documents" folder. Unfortunately, if you do not have control of your computer, this folder could be on the network, which would stop the visualizer from working.

A simple alternative is to use PowerShell's `Out-GridView` within C#. The following class `_PS` defines the extension method `OutGridView` which will invoke `Out-GridView`:


using System;
using System.Collections;
using System.Data;

public static class _PS
{
    public static void OutGridView(this DataTable t)
    {
        t.Rows.OutGridView();
    }

    public static void OutGridView(this IEnumerable t)
    {
        using (var ps = System.Management.Automation.PowerShell.Create())
        {
            ps.Commands.AddCommand("Out-GridView");
            var result = ps.Invoke(t);
        }
    } 
}

For instance, if we had a `DataTable` `dt`, then in the Intermediate/QuickWatch/Watch window, you can evaluate:


dt.OutGridView()

To use the above code, install the `System.Management.Automation` NuGet package.