論盡蛋
心で書く

Blog


String display width in given font in Javascript

Aim

To calculate the width of a given character or string.

Why

Even if you use monospace fonts, there are some characters are not in the same width (such as Chinese and Japanese).

Furthermore there is no simple way to know if a given character is double-sized or not.

How

jQuery Version

#!Javascript
String.prototype.widthInFont = function(font) {
  var f = font || '12px arial',
      o = $('<div>' + this + '</div>').css({
        'position': 'absolute',
        'float': 'left',
        'white-space': 'nowrap',
        'visibility': 'hidden',
        'font': f
      }).appendTo($('body')),
      w = o.width();

  o.remove();

  return w;
}

Performance

Reference

If you have better implementation, please let me know :)


Missing gcc-4.2 on Mountain Lion

Recently I upgraded to Mountain Lion (OK actually not quite recent XD), which kind of resets my development environment. But it's okay, not a big deal :)

Rails 3.2.8 is out, and it fixes 3 security issues. So it should be worth to upgrade ChordsPresent's rails version. However it seems I could not get it done very easy (I expected less than 1 minute, but actually took me nearly 5 minutes...)

Replacing by gem 'rails', '3.2.8' in the Gemfile and ran bundle update rails, I get the following error:

#!bash
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/PeterWong/.rvm/rubies/ruby-1.9.3-head/bin/ruby extconf.rb 
creating Makefile

make
compiling generator.c
make: /usr/bin/gcc-4.2: No such file or directory
make: *** [generator.o] Error 1


Gem files will remain installed in /Users/PeterWong/.rvm/gems/ruby-1.9.3-head@chordspresent/gems/json-1.7.4 for inspection.
Results logged to /Users/PeterWong/.rvm/gems/ruby-1.9.3-head@chordspresent/gems/json-1.7.4/ext/json/ext/generator/gem_make.out
An error occurred while installing json (1.7.4), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.4'` succeeds before bundling.

Basically it complained that it cannot find my gcc at /usr/bin/gcc-4.2.

The first response I took was which gcc which yields: /usr/bin/gcc. But then I realised that it should not be that simple. Mountain Lion changed to llvm-gcc.

And my curiosity caused me to type ls /usr/bin/llvm-gcc together with two tabs and it showed the two results: llvm-gcc llvm-gcc-4.2.

Still do not know why bundle update wanted gcc-4.2 yet, but it should work if I just make a symbolic link:

#!bash
sudo ln -s /usr/bin/llvm-gcc-4.2 /usr/bin/gcc-4.2

And yeah it works now, bundle update rails ran successfully now :)


我的 SSH config

如果你跟我一樣有用 Heroku,相信你都有遇到過有兩個 accounts 的問題。每個 ssh key 只能上傳到一個 account。假如你想 git clone 兩個不同 account 的 projects 的話,就會發現單純 heroku login 幫不到忙。

這是因為 git clone 靠 ssh 來 authenticate 的。但 heroku login 不會更改你的 ssh keys,也不會自動選用合適的那條 key。

一個簡單的解決方法就是用 ~/.ssh/config

現況

首先先說明一下我的實際情況。

  1. 個人 account
    • peter@peterwongpp.com
    • ssh key: ~/.ssh/id_rsa
  2. 公司 account
    • peter@primitus.com
    • ssh key: ~/.ssh/primitus_heroku

這兩條 keys 以以下分式 upload 的:

#!bash
$ heroku login
Email: peter@peterwongpp.com
Password (typing will be hidden):
Authentication successful.

$ heroku keys:add
Found the following SSH public keys:
1) id_rsa.pub
2) primitus_heroku.pub
Which would you like to use with your Heroku account? 1

$ heroku login
Email: peter@primitus.com
Password (typing will be hidden):
Authentication successful.

$ heroku keys:add
Found the following SSH public keys:
1) id_rsa.pub
2) primitus_heroku.pub
Which would you like to use with your Heroku account? 2

問題

但現在應該會有如下的 error:

#!bash
$ git clone git@heroku.com:primitus-project.git
Cloning into 'primitus-project'...

 !  Your key with fingerprint ...... is not authorized to access primitus-project.

fatal: The remote end hung up unexpectedly

解決

首先是 ~/.ssh/config(如沒有請自行建立)

#!bash
Host HerokuPrimitus
  HostName heroku.com
  User git
  IdentityFile ~/.ssh/primitus_heroku
  IdentitiesOnly yes

簡單來說就是用 HerokuPrimitus 來代替 heroku.com,但指定了 ssh key。(另外也可以指定 port number etc)

然後是再來 git clone

#!bash
$ git clone git@HerokuPrimitus.com:primitus-project.git
Cloning into 'primitus-project'...
remote: Counting objects: ......

ActiveRecord::RecordNotUnique and Postgresql

OK I am not sure if this is a postgresql-specific problem or not, but I believe it is. If you did something like this (such as in seed.rb):

#!ruby
User.create(id: 1, ...)

Then the next time you create new user, the ActiveRecord::RecordNotUnique error would be raised.

But why you specified id!?

I have to admit that this is a lazy and silly decide. I re-created ChordsPresent and migrated from Heroku bamboo stack to cedar stack. The models have some schema-level changes also. Thus the easier way for me to migrate the old data to the new database is:

  1. download the remote database
  2. create a rake file to output a seed.rb file which based on the existing data (collecting only the required data, and matching the new schema)
  3. add the outputted seed.rb file to the new app source code, and push and run it in the cedar stack (so the new database now has all the new data with correct schema)
  4. revert the seed.rb and destroy the last commit completely (git tracking sensitive data is always a bad practice!, although the most sensitive data are the users' provider and uid only)
  5. push to heroku again with -f flag to force replace it

As I want to retain the chords and users id, thus I included the id field of both table. However explicitly assigning the id field causes problem.

How to fix?

(Provided this is a postgresql problem)

#!ruby
ActiveRecord::Base.connection.execute("SELECT setval('table_name_id_seq', (SELECT max(id) FROM table_name));")

replace the table_name with your actual table name. In my case, it is:

#!ruby
ActiveRecord::Base.connection.execute("SELECT setval('users_id_seq', (SELECT max(id) FROM users));")
ActiveRecord::Base.connection.execute("SELECT setval('chords_id_seq', (SELECT max(id) FROM chords));")

沙巴自由行 - 準備篇

沙巴,一個陽光與海灘的地方。準備篇會詳列所有的準備,既可作個記錄,也可為將來去沙巴的人留個參考 :)

這次行程的基本資料如下:

  • 預計抵達日期時間:2012.06.05 11:00(08:00上機)
  • 預計離開日期時間:2012.06.10 11:55
  • 來回機票:港龍
  • 酒店:The Pacific Sutera

五晚都睡在同一間酒店,因為那裡交通似乎很方便,有私人海灘,碼頭,無線 Wifi 等,很不錯!

而這六天的安排如下:

  1. (5/6) Check in,到市中心閒逛
  2. (6/6) 美人魚島潛水一日遊(晚上回市中心逛)
  3. (7/6) 神山公園一日遊(包吊橋、波令溫泉)(晚上回市中心逛)
  4. (8/6) 紅樹林尋覓長鼻猴 + 螢火蟲(早上在酒店玩)
  5. (9/6) 自由行(到外島玩水上活動和享受陽光與海灘)
  6. (10/6) Sunday Market

Checklist

出遠門當然要帶齊東西!要確保東西帶齊最好預先寫好 checklist,確定無誤後可以逐一比對有沒有漏。

必需類

這類東西理應大家都適用。

  1. 證件、文件類(護照、機票、酒店、保險等文件)
  2. 藥物類(暈浪藥)
  3. 太陽油、蚊怕水、電風扇
  4. 適量金錢
  5. 相機(確保已叉電,叉電器,記憶卡,...)
  6. 電話(我到達後會買當地電話卡與 agent 聯絡)
  7. 傘具(旱季,帶不帶自行決定;我就會預備便利雨衣)
  8. 短袖衣服,薄外套(室內冷氣、落水後、神山公園等場合用)
  9. 個人護理用品(牙刷、牙膏、沐浴露、洗頭水、鬚等)

特定類

這類東西是我個人為這次旅程準備的,並不一定適合你 ;)

  1. 浮潛用品(潛水鏡、呼吸管、防霧劑)
  2. 2L 水袋,寶礦力水特沖劑
  3. 電腦(用來儲存相片和寫 blog,酒店有私人海灘,)
  4. 不同種類的膠袋、密實袋等(水上活動難免濕身,有些袋總有用得著的時候;而且既輕又不佔位)