Skip to content

Instantly share code, notes, and snippets.

@moogoo78
moogoo78 / gist:d55c91376228659f3207
Created March 24, 2016 09:57
lua truncate multi-byte string
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 March 24, 2016 09:55
count length of multi-byte string
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
(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 December 23, 2015 05:49
get Corona SDK info
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))
#!/usr/bin/env python
# -.- coding: utf-8 -.-
"""
Usage:
landsat5.py <username> <password>
Requirements:
beautifulsoup4==4.0.5
requests==2.0.1
@moogoo78
moogoo78 / gist:5e7842bddadf928a09a7
Created June 2, 2014 05:32
markdown link bookmarklet
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>');})()