Skip to content

Instantly share code, notes, and snippets.

@jgreco
jgreco / mpv.rkt
Last active September 22, 2019 08:53
quick hack mpv JSON IPC client
#lang racket
(require json
racket/port
racket/unix-socket
racket/async-channel)
(provide mpv/fire-and-forget)
(define (mpv/fire-and-forget files)
(match-let-values
([(_ _ to-mpv _) (subprocess (open-output-file "/dev/null" #:exists 'append)
(define (rmap proc target)
(cond
[(list? target) (map (curry rmap proc) target)]
[(vector? target) (vector-map (curry rmap proc) target)]
[else (proc target)]))
#lang racket/gui
(require framework)
(define swapping-tab-panel%
(class tab-panel%
(inherit get-selection set-selection set)
(super-new [callback
(lambda (b e)
(when (eq? 'tab-panel (send e get-event-type))
(send swapper-panel active-child
#lang racket
;; > (require "observer-app.rkt")
;; > (hash-set! (observers-namespace) '+ `(,(lambda (a . b) (printf "adding ~a to ~a~n" a b))))
;; > (+ 1 2 3)
;; adding 1 to (2 3)
;; 6
(provide (rename-out [observer-app #%app])
observers-namespace)
(define (sync/vector evts-v)
(let ([sync-vec (build-vector (vector-length evts-v)
(lambda (n)
(wrap-evt
(vector-ref evts-v n)
(lambda (r) (cons n r)))))])
(let loop ()
(let ([remaining-evts (filter evt? (vector->list sync-vec))])
(unless (empty? remaining-evts)
(let ([res (apply sync remaining-evts)])
#lang racket
(require db
memo
"util.rkt"
racket/serialize)
(provide #%app
#%datum
#%top
;; Remove a given tag from a set of file ids.
;; If any of the file ids is not tagged with the given tag, they are silently skipped
;; without hassling the user. If a tag is empty after this operation, it is deleted.
;;
;; TODO: This procedure performs up to three database writes. If those writes
;; happened to be interleaved with other writes which are associated with
;; the same tag, screwy things might happen. Therefore these two/three
;; writes should be performed in a single transaction.
(define (untag-files tag . file-ids)
(let* ([tag-id (get-tag-id tag)]
> .schema
CREATE TABLE Files( file_id INTEGER PRIMARY KEY, filename VARCHAR(4096) NOT NULL UNIQUE, last_modified INTEGER, size INTEGER, num_tags INTEGER DEFAULT 0);
CREATE TABLE sqlite_stat1(tbl,idx,stat);
CREATE TABLE FileTags( file_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, probability DOUBLE NOT NULL, PRIMARY KEY (file_id, tag_id), FOREIGN KEY(file_id) REFERENCES Files(file_id), FOREIGN KEY(tag_id) REFERENCES Tags(tag_id));
CREATE TABLE Tags(tag_id INTEGER PRIMARY KEY, tag_name VARCHAR[50] NOT NULL UNIQUE, num_files INTEGER DEFAULT 0);
CREATE TABLE TagCardinalities(
a INTEGER NOT NULL,
b INTEGER NOT NULL,
cardinality INTEGER,
PRIMARY KEY (a,b),
(query-exec (sqldb-param)
(prepared (string* "INSERT OR REPLACE INTO TagCardinalities (a,b,cardinality)"
" SELECT min($1,t.tag_id),max($1,t.tag_id),ifnull(sub.cnt,0) "
" FROM Tags t LEFT JOIN"
"(SELECT tag_id, COUNT(*) as cnt FROM FileTags WHERE file_id IN "
"(SELECT file_id FROM FileTags WHERE tag_id=$1)"
"GROUP BY tag_id) AS sub"
"ON t.tag_id = sub.tag_id"))
a-id)))
@jgreco
jgreco / youtube-quality.lua
Last active August 30, 2021 04:54
demo mpv user builtin for youtube-dl
-- youtube-quality.lua
--
-- Change youtube video quality on the fly.
--
-- Diplays a menu that lets you switch to different ytdl-format settings while
-- you're in the middle of a video (just like you were using the web player).
--
-- Bound to ctrl-f by default.
local mp = require 'mp'