[ Content | Sidebar ]

YPbPr Mode vs RGB

February 26th, 2021

I was fiddling with my monitor settings today (Dell U2415) and noticed the “Input Color Format” was set to “YPbPr” instead of “RGB”. This is a compressed colour space where the chroma channel has half the resolution of the luminance channel. Normally this would be used for TVs or video encoding rather than a PC monitor. That said I’ve been using it this way for two years without noticing…

The problem is Dell monitors advertise this mode along with RGB in their HDMI EDID. The driver for my AMD graphics card see this and prefers it over RGB with no way to override the selection. There is one creative solution I found which involves patching a local copy of the EDID and telling the driver to load that from disk rather than reading it from the monitor. I took the simpler option of spending a few quid on a DisplayPort cable which only supports RGB.

The result? Fonts look a bit sharper… maybe… but it’s hard to tell.

Minhang Riverside Park

February 20th, 2021

On the last day of the holiday I decided to go for another walk, this time almost directly south to the Huangpu river.

The long straight section is walking along the “green corridor” next to metro line 5.

Underneath the Shanghai–Jiaxing–Huzhou expressway

Eventually I reached the Huangpu river, which I believe is the southern boundary of Minhang district. There’s a small park here called Minhang Riverside Park, which isn’t particularly notable. However the view of the river is dominated by a large single-span suspension bridge. Unfortunately Wikipedia, my usual source of bridge knowledge, failed me on this one. The words on the tower read 闵浦二桥 “MinPu 2nd Bridge”. Some web searching reveals that it was opened to traffic in 2010, the tower is 148 meters tall, and the deck is 40 meters above the water. It’s a rather prosaic concrete structure: they didn’t even bother to paint it. However it is notable for having two decks. The lower deck carries the metro line 5 extension to the Fengxian suburb, the only place the Shanghai metro crosses the Huangpu river above-water. You can just make out a train crossing the bridge in the picture below. They have to drive very slowly for some reason.

Minpu II bridge with metro train crossing

Speaking of metro lines, Shanghai’s brand new line 15 opened just last month and the southern terminus is a few miles from this spot. This line is generating a lot of excitement because all the trains are driverless, like London’s DLR. (Actually Shanghai already had driverless trains on the Pujiang line but it’s so out of the way not many people knew about it.) Obviously I ran straight to the front of the train and stared transfixed at the passing tunnel for the whole journey. I made a short video so you can experience it too.

Sijing Old Town

February 14th, 2021

I’ve got a week off for Chinese new year so it seemed like a good opportunity to go out for a walk. I planned to explore westwards along the Dianpu river which I’d walked before, but I didn’t get very far as the path along the river is intermittent at best and the big roads aren’t pleasant to walk along. So I took a detour to Sijing old town, which I found by chance while looking at the map.

The main attraction is probably Futian temple, a medium-sized Buddhist temple dating from the 18th century. There weren’t many visitors so it was very peaceful, much more so than other temples I’ve visited in China.

Outside the temple

The old town itself has seen better days. I’m not sure if it’s in the process of being demolished or renovated. Anyway most of the buildings are boarded up although there’s a free architecture exhibition that’s worth a look. I actually preferred wandering around here compared to some of the more over-comercialised old towns I’ve been too.

The waterfront view is more attractive

The pagoda was closed too although it seemed operational so that might just be because it’s the new year holiday.

Hipster cafe and bookshop

Emacs: enable whitespace-mode automatically in files with tabs

January 28th, 2021

I normally only use spaces to indent but sometimes I have to edit a file that contains tabs and I don’t want to break the existing indentation. In this situation Emacs’ whitespace-mode is very useful for showing exactly which whitespace characters are present in a file. But I don’t want to enable it all the time so I use this little function to check automatically if a file contains tabs, and if so enable whitespace-mode and also indent-tabs-mode so that any new lines are indented with tabs as well.

(defun check-for-tabs ()
  "Enable whitespace-mode and indent-tabs-mode if the buffer
contains tabs."
  (when (save-excursion
          (goto-char (point-min))
          (search-forward "\t" nil t))
    (whitespace-mode +1)
    (setq-local indent-tabs-mode t)))
 
(add-hook 'find-file-hook #'check-for-tabs)

Changing Emacs C align function to only apply once per line

January 3rd, 2021

I find myself using M-x align a lot in Emacs to line up the identifiers in blocks of C declarations or statements like the following:

struct foo {
   something x;
   int hello;
   long *blodgett;
};
 
int *q;
bazbazbaz *y;
static char fnork;
 
spoonful = sugar;
spork = 1;

After marking the region containing the declarations a quick M-x align transforms it into:

struct foo {
   something  x;
   int        hello;
   long      *blodgett;
};
 
int         *q;
bazbazbaz   *y;
static char  fnork;
 
spoonful = sugar;
spork    = 1;

Which looks much neater to my eyes. However when the declarations have initialisers the result is not at all pleasing:

int x = 5;
struct baz baz = { 1, 2 };
const int *this_is_a_pointer = NULL;

Becomes

int         x                 = 5;
struct baz  baz               = { 1, 2 };
const int  *this_is_a_pointer = NULL;

Really I only want it to align the identifiers and leave the = where they were. I’m sure I can’t be the only one with this problem but I’ve no idea what to type into Google or Stack Overflow to find the solution, and the manual doesn’t help either.

My current solution is to patch the list of alignment rules in align-rules-list with this bit of hackery:

;;; Define this in a file with `lexical-binding' set or else change the
;;; `let' below to `lexical-let'.
(defun nick-fix-c-align ()
  (let ((var-decl-regexp (alist-get 'regexp
                                    (alist-get 'c-variable-declaration
                                               align-rules-list))))
    (push `(valid . ,(lambda ()
                       (not (save-excursion
                              (end-of-line)
                              (looking-back var-decl-regexp)))))
          (alist-get 'c-assignment align-rules-list))))

The variable align-rules-list holds a list of rules for patterns like “C assignment”, “C variable declaration”, and so on. Each rule is an alist with a regex trigger, a list of modes to enable it in, an optional predicate valid to further restrict when it is run, and some other irrelevant options. The align function loops over this list and aligns any text where the regex matches and valid returns true.

The problem is C declarations with intialisers trigger both the c-variable-declaration and the c-assignment rules. There’s no way to tell it “stop processing rules if this matches” so the function above modifies the rules list to add an extra predicate to the c-assignment rule which says “do not apply this rule when the line also matches the regex for C variable declarations”.

align-load-hook is called after align.el has been loaded, which is a convienent time to patch the rules list. Afterwards M-x align lines up only the identifiers in declarations:

int         x = 5;
struct baz  baz = { 1, 2 };
const int  *this_is_a_pointer = NULL;

Mince Pie Scalpers

December 31st, 2020

It’s no secret that I love mince pies. But due to the pandemic I’m stuck in Shanghai this Christmas and the seasonal delicacy is hard to source. Now everyone who’s lived in China knows you can buy anything on Taobao, and sure enough I found a 代购 daigou specialising in British foodstuffs.

Unfortunately the price is around 5-10x what you’d normally pay in the UK. Feeling desperate I bought one box of Waitrose pies anyway, for a total of ¥154 with shipping (about £17). It eventually arrived two days before Christmas and a joyous time was had by all.

A delicous Waitrose regular mince pie

Minhang Loop

December 31st, 2020

What a terrible year 2020 was. With swimming, running, and gym all out I tried to keep fit by power walking this loop around my local neighbourhood. I’m recording it here for posterity.

This was mostly a night time activity due to the extreme heat in the summer. At one point you pass under this rather impressive three layer elevated interchange.

New Lego Pirates Ahoy

December 27th, 2020

A few years ago I thought I had finally collected all the classic Lego pirates. But no! Lego have just recently release a new set in the traditional style! Obviously I had to get it for Christmas and I wasn’t disappointed…

Thank you Santa

The set has over 2500 pieces and I spent two days building it. I’ve literally never spent that long on a Lego set.

The island form

It’s supposed to be a pirate base made out of an old pirate ship that ran aground.

Pirate captain and pirate pub

You can actually build two models and switch between them without much effort: the three boat parts clip out and can be assembled into a whole pirate ship.

The boat form, resembling the old Lego pirate ship

Minhang Sports Park

December 23rd, 2020

Last weekend I went out for a walk to Minhang sports park, a large green space in the west of Shanghai. Although I’m not sure what qualifies it to be a “sports park” rather than just a regular park. Minhang is the name of this particular suburb.

Minhang sports park

Afterwards I kept walking south to Xinzhuang and then even further south to a part of the city I’ve never visited before.

This area is a bit industrial and there wasn’t much interesting to look at except metro line 5, which I’d never visited before. It’s elevated above the track and has this walking path underneath it at least part of the way.

Underneath line 5

Best Shell Prompt Colour Scheme

December 19th, 2020

It can be agonizing to pick a good colour scheme for your shell prompt. Especially when you have 256 or more colours to pick from. So rather than waste my time I decided to embrace serendipity and have my shell pick a random colour when it starts. The results are rather pleasing, as you can see below, and if I don’t like a particular colour then it will only last as long as that particular shell.

It also helps to visually distinguish different windows that are being used for different tasks, and root shells are coloured an alarming shade of red. Just pop the following in your .bashrc.

PS1=${SSH_CLIENT:+$(hostname -s):}'\w \$ '
case "$TERM" in
  *-256color)
    if [ "$UID" = 0 ]; then
      color=196   # Red
    else
      color=$((16+(36*(1+RANDOM%5))+(6*(1+RANDOM%5))+(1+RANDOM%5)))
    fi
    PS1='\[\033[1m\033[38;5;'$color'm\]'$PS1'\[\033[00m\]'
    ;;
  *-color)
    if [ "$UID" = 0 ]; then
      color=31   # Red
    else
      color=$((31+RANDOM%8))
    fi
    PS1='\[\033[1m\033['$color'm\]'$PS1'\[\033[00m\]'
    ;;
esac
unset color

For *-256color terminals the codes above 36 are a 6x6x6 RGB colour cube. This script avoids darker colours but you can tweak it to your liking. Most modern terminals also support a true colour escape sequence giving full 24-bit colour, but 120 different shades is surely enough for anyone.