Skip to content

Instantly share code, notes, and snippets.

View hungtcs's full-sized avatar

鸿则 hungtcs

View GitHub Profile
@hungtcs
hungtcs / docker-entrypoint.sh
Created April 17, 2024 01:34
docker 支持使用 ENV_FILE 环境变量
#!/usr/bin/env bash
# set -v
set -e
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
@hungtcs
hungtcs / temp.go
Created April 11, 2024 08:27
starlark format function like js string interpolation
var predeclared = starlark.StringDict{
"format": starlark.NewBuiltin("format", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var function = thread.DebugFrame(1).Callable().(*starlark.Function)
var locals = function.Locals()
var globals = function.Globals()
var formatArgs = make(map[string]starlark.Value)
for key, val := range globals {
formatArgs[key] = val
@hungtcs
hungtcs / capture.js
Last active February 2, 2024 05:25
Capture elements with html2canvas
async function capture(element, options = {}) {
const module = await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')
const html2canvas = module.default;
return await html2canvas(element, options);
}
async function canvasToBlob(canvas, type, quality) {
return await new Promise((resolve) => {
canvas.toBlob(blob => resolve(blob), type, quality);
});
@hungtcs
hungtcs / dark-mode.css
Created February 2, 2024 02:34
Change any site to dark mode
/* https://github.com/openstyles/stylus/ */
html {
filter: invert(1) hue-rotate(180deg);
color-scheme: dark;
}
html img{
filter: invert(1) hue-rotate(180deg);
}
@hungtcs
hungtcs / timezones.json
Created January 26, 2024 05:38
timezone list
[
{
"label": "Asia/Kabul",
"value": "+04:30"
},
{
"label": "Europe/Tirane",
"value": "+01:00"
},
{
@hungtcs
hungtcs / base64-to-file.ts
Created December 26, 2023 01:22
Convert base64 string to File
function base64ToFile(base64: string, filename: string) {
const [header, code] = base64.split(',')
const cotnentType = header.replace(/(data:)|(;base64)/g, '');
const byteStr = atob(code);
const bytes = new Array(byteStr.length);
for (var i = 0; i < byteStr.length; i++) {
bytes[i] = byteStr.charCodeAt(i);
}
const byteArray = new Uint8Array(bytes);
@hungtcs
hungtcs / Expr.g4
Last active December 13, 2023 06:06
搜索过滤表达式匹配
grammar Expr;
fragment ESC: '\\' .;
fragment DIGIT: [0-9];
WS: [ \t\r\n]+ -> skip;
COMMA: ',';
EXCLUDE: '-';
KEYWORLD: ~[ \t\r\n:,-]+;
@hungtcs
hungtcs / 00.html
Last active October 26, 2023 09:36
Show fallback image when image loading fails
<object data="path/to/image-00.png" type="image/png">
<img src="path/to/fallback.png" />
</object>
<!-- why not this -->
<img src="path/to/image-00.png" onerror="this.src = 'path/to/fallback.png'" />
<!-- 1. object is a pure html solution and does not require js. -->
<!-- 2. if the backup image fails to load, there is a risk of an infinite loop -->
<!-- 3. however, the object tag has a larger overhead -->
@hungtcs
hungtcs / emojis.json
Created October 25, 2023 03:11
Unicode Emojis Collection
[
{
"name": "smileys_and_people",
"emojis": [
{
"emoji": "😀",
"unicode": "U+1F600",
"shortcode": ":grinning_face:"
},
{
@hungtcs
hungtcs / 00.sql
Created October 24, 2023 03:36
Postgres 数据库、schema、table 大小获取方式
-- 数据库大小
select pg_size_pretty(pg_database_size(current_database()));
-- 只计算table size
select schemaname, pg_size_pretty(sum(pg_relation_size(schemaname || '.' || tablename))) from pg_tables group by schemaname;