Skip to content

Instantly share code, notes, and snippets.

@ginpei
ginpei / getCpuTemperature.win.js
Last active February 4, 2024 20:47
Get CPU temperature for Windows by Node.js
Math.round((require('child_process').execSync('wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature').toString().split('\r\n')[1].trim()) / 10 - 273)
@ginpei
ginpei / example.tsx
Created August 7, 2023 00:46
History system using React Redux
import { PayloadAction, configureStore, createSlice } from "@reduxjs/toolkit";
import type { NextPage } from "next";
import { Provider, useDispatch, useSelector } from "react-redux";
import undoable, { StateWithHistory, UndoableOptions } from "redux-undo";
// partial state for a section (undo target)
interface NumberState {
id: string;
title: string;
value: number;
import type { NextPage } from "next";
import { useState } from "react";
import { HStack } from "../../src/lib/layout/HStack";
import { VStack } from "../../src/lib/layout/VStack";
import { Button } from "../../src/lib/style/Button";
import { H1 } from "../../src/lib/style/H1";
import { H2 } from "../../src/lib/style/H2";
interface Task {
done: boolean;
@ginpei
ginpei / filter.js
Created December 29, 2022 11:40
Dim off items without keywords in Amazon search result
var words = twotabsearchtextbox.value.split(" ");
[...document.querySelectorAll(".s-result-item")]
.filter((v) => !words.every((w) => v.textContent.toLowerCase().includes(w)))
.forEach((v) => v.style.opacity = "0.3")
@ginpei
ginpei / .js
Last active October 6, 2022 21:18
Measure Express.js middleware runtime performance
const app = express();
const _use = app.use;
let numUseCalls = 0;
app.use = function(...args) {
const fn = args[args.length - 1];
// except `express.static()`
if (fn && fn.name === 'serveStatic') {
_use.apply(this, args);
import { useFocusRing } from "@react-aria/focus";
import { useListBox, useOption } from "@react-aria/listbox";
import { mergeProps } from "@react-aria/utils";
import { ListProps, ListState, useListState } from "@react-stately/list";
import { Node } from "@react-types/shared";
import { useRef } from "react";
interface ListBoxProps<T> extends ListProps<T> {
label?: string;
}
interface FruitMap {
'Apple': AppleFruit;
'Banana': BananaFruit;
}
type FruitName = keyof FruitMap;
type Fruit = FruitMap[FruitName]
interface FruitBase<Type extends FruitName> {
type FruitTemplate<Type extends string, Props> ={ type: Type } & Props
type Fruit =
| AppleFruit
| BananaFruit
type AppleFruit = FruitTemplate<'Apple', {
sliced: boolean;
peeled: boolean;
}>
import React, {
CSSProperties, useCallback, useEffect, useMemo, useState, useRef,
} from 'react';
import Head from 'next/head';
import { Pos } from '../models/Length';
const DraggablePage: React.FC = () => {
const [dragging, setDragging] = useState(false);
const [from, setFrom] = useState<Pos>({ x: 0, y: 0 });
const [pointerType, setPointerType] = useState('');