Skip to content

Instantly share code, notes, and snippets.

View Ciantic's full-sized avatar

Jari Pennanen Ciantic

View GitHub Profile
// @deno-types="npm:@types/sql.js"
import { default as initSqlJs } from "npm:sql.js";
import { IDatabase, QueryResult } from "./Database.ts";
export class Database implements IDatabase {
private path: string;
private sqliteJs?: initSqlJs.SqlJsStatic;
private db?: initSqlJs.Database;
private inited = false;
@Ciantic
Ciantic / shortcode2.php
Last active October 10, 2023 10:52
Temporary shortcode block, when WordPress had a bug
<?php
add_action("init", function () {
// Ob_start twice, intentionally
ob_start();
?>
<script type="module">
<?php ob_start(); ?>
wp.blocks.registerBlockType("temp/shortcode", {
@Ciantic
Ciantic / hhook.rs
Created March 23, 2023 22:57
Just code I haven't used yet
use std::sync::mpsc::Sender;
use windows::{
core::Result,
s,
Win32::{
Foundation::{HANDLE, HWND, LPARAM, LRESULT, WPARAM},
System::{
LibraryLoader::GetModuleHandleA,
Power::{
@Ciantic
Ciantic / this-panics.rs
Last active February 23, 2023 21:13
Why this panics? "fatal runtime error: thread local panicked on drop"
struct MyThreadWrapper {
thread: Option<std::thread::JoinHandle<()>>,
}
impl Drop for MyThreadWrapper {
fn drop(&mut self) {
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}
@Ciantic
Ciantic / windows_thread.rs
Created February 18, 2023 00:01
Just simple CreateThread example with proper way to QUIT.
use windows::{
core::{GUID, HSTRING},
Win32::{
Foundation::HWND,
System::{
Com::{CoInitializeEx, COINIT_APARTMENTTHREADED},
Threading::{
CreateThread, GetCurrentThreadId, WaitForSingleObject, THREAD_CREATION_FLAGS,
},
},
@Ciantic
Ciantic / hstring.rs
Created February 17, 2023 23:47
Dependency free HSTRING implementation for Rust
// Dependency free HSTRING implementation
use std::ffi::OsStr;
use std::ffi::{c_void, OsString};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::ffi::OsStringExt;
type LPCWSTR = *const u16;
type HRESULT = i32;
@Ciantic
Ciantic / python-like-decorators-currying-in-rust.rs
Created February 11, 2023 17:05
Two examples: How to decorate (curry) a function and how to curry a method in Rust
// Cargo.toml
//
// [dependencies]
// macro_rules_attribute = "0.1"
#[macro_use]
extern crate macro_rules_attribute;
// How to decorate a function
@Ciantic
Ciantic / fetc.sh
Created December 22, 2022 20:54
fetch replies
#!/bin/bash
HEADERS='Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
curl -H "$HEADERS" "https://mastodon.social/users/Gargron/statuses/109543381373981313/replies?only_other_accounts=true&page=true" | jq
@Ciantic
Ciantic / register-reusable-blocks-as-patterns.php
Created September 27, 2022 16:16
Register reusable blocks as patterns
<?php
// Show menu item for reusable blocks
add_action('admin_menu', function () {
add_menu_page(_x('Reusable blocks', 'post type general name'), _x('Reusable blocks', 'post type general name'), 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22);
});
add_action("should_load_remote_block_patterns", function () {
// I chose `should_load_remote_block_patterns` action because it's triggered
@Ciantic
Ciantic / sqlite-worker-query.ts
Created June 26, 2022 22:44
SQLite worker snippet
const worker = new Worker("https://cdn.jsdelivr.net/npm/[email protected]/dist/worker.sql-wasm.js");
let rpcid = 0;
async function query(sql: string, params: any, action = "exec") {
const id = rpcid++;
return new Promise<any>((res, rej) => {
const listener = (e: MessageEvent<any>) => {
if (e.data && e.data.id === id) {
worker.removeEventListener("message", listener);
res(e.data);
}