Skip to content

Instantly share code, notes, and snippets.

@moogoo78
moogoo78 / gist:d55c91376228659f3207
Created Mar 24, 2016
lua truncate multi-byte string
View gist:d55c91376228659f3207
local function truncate(s, n, tail)
local c = 0 -- 判斷字寬, 中文: 2, 西歐字母: 1
local c2 = 0 -- 判斷字元 (中文大部分3byte, TODO)
local add_tail = false
local r = ""
for i in s:gmatch("[\1-\127\194-\244][\128-\191]*") do
if c > n then
add_tail = true
break
end
@moogoo78
moogoo78 / mb_len.lua
Created Mar 24, 2016
count length of multi-byte string
View mb_len.lua
local function mb_len(s)
local c = 0 -- 判斷字寬, 中文: 2, 西歐字母: 1
local c2 = 0 -- 判斷字元 (中文大部分3byte, TODO)
local add_tail = false
local r = ""
for i in s:gmatch("[\1-\127\194-\244][\128-\191]*") do
if string.byte(i) > 127 then
c = c + 2
c2 = c2 + 3
else
View landsat5
#!/usr/bin/env python
# -.- coding: utf-8 -.-
"""
Usage:
landsat5.py <username> <password>
Requirements:
beautifulsoup4==4.0.5
requests==2.0.1
View gist:5e7842bddadf928a09a7
javascript:(function(){t='['+decodeURIComponent(document.title)+']('+decodeURIComponent(window.location.href)+')';win=window.open('','_new','location=no,links=no,scrollbars=no,toolbar=no,width=550,height=150');win.document.write('<form><textarea%20name="a"%20rows="5"%20cols="50"%20onClick="javascript:this.form.a.focus();this.form.a.select();">'+t+'</textarea></form>');})()
View elisp 跑步計算機
(defun run-calc (d hh mm ss)
"running calculator"
(interactive "nDistance (km): \nnHours: \nnMinutes: \nnSeconds: ")
(let ((km (* d 1000))
(tm (+ (* hh 3600) (* mm 60) ss)))
(setq hkm (/ tm d 60.0))
(setq hkm-m (/ tm d 60))
(setq hkm-s (% (/ tm d) 60))
(setq mpr (/ tm d 2.5))) ;;沒有進位
(message "%dkm, %02d:%02d:%02d => %.2f m/km (%dm%ds), %.2f s/round" d hh mm ss hkm hkm-m hkm-s mpr)
@moogoo78
moogoo78 / gist:6589902
Last active Dec 23, 2015
get Corona SDK info
View gist:6589902
local function info()
print("========= display info =========")
print(string.format("pixel: %dx%d, ratio: %.02f",
display.pixelWidth,
display.pixelHeight,
display.pixelHeight/display.pixelWidth))
print(string.format("content: %dx%d, ratio: %.02f",
display.contentWidth,
display.contentHeight,
display.contentHeight/display.contentWidth))