Skip to content

Instantly share code, notes, and snippets.

View juancampa's full-sized avatar
🏠
Working from home

Juan Campa juancampa

🏠
Working from home
View GitHub Profile
@juancampa
juancampa / egui_marks.rs
Created January 6, 2024 15:59
Render marks under pointer check for slow input
// Put this in your `update` to render marks under the cursor to tell if your input is slow
let input_pos = ui.ctx().input(|input| input.pointer.latest_pos());
let marks = ui.memory_mut(|mem| {
let marks: &mut VecDeque<Pos2> =
mem.data.get_temp_mut_or_default(Id::new("dots"));
if let Some(pos) = input_pos {
if pos == marks.back().cloned().unwrap_or_default() {
// Add a small offset if position is repeated
marks.push_back(pos + vec2(3.0, 3.0));
@juancampa
juancampa / tab_switcher.rs
Created November 16, 2023 22:24
Egui Tab Switcher
pub fn render_switch(&self, ui: &mut Ui, id: Id, labels: &[&str; 2], selected: usize, available: Rect) -> Response {
let text_color = ui.visuals().strong_text_color();
let galley1 = ui.fonts(|fonts| fonts.layout_no_wrap(labels[0].to_owned(), FontId::monospace(9.0), text_color));
let galley2 = ui.fonts(|fonts| fonts.layout_no_wrap(labels[1].to_owned(), FontId::monospace(9.0), text_color));
let margin = vec2(10.0, 4.0);
// Initial estimation of the position. It will be moved below
let rect = Rect::from_min_size(Pos2::ZERO,
vec2(
@juancampa
juancampa / egui_inner_shadow.rs
Created March 31, 2023 17:43
Egui inner shadow
#[extension_trait]
pub impl UiExt for Ui {
fn inner_shadow_top(&mut self, mut avail_rect: Rect, opacity: f32) {
let painter = self.painter();
avail_rect.set_height(1.0);
const STEPS: usize = 8;
for i in 0..STEPS {
let alpha = 1.0 - (i as f32 / STEPS as f32);
let shift = avail_rect.height() * i as f32;
let rect = avail_rect.translate((0.0, shift).into());
@juancampa
juancampa / egui_centerer.rs
Created March 16, 2023 13:50
Centering arbitrary UIs in egui
// Helper function to center arbitrary widgets. It works by measuring the width of the widgets after rendering, and
// then using that offset on the next frame.
fn centerer(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) {
ui.horizontal(|ui| {
let id = ui.id().with("_centerer");
let last_width: Option<f32> = ui.memory_mut(|mem| mem.data.get_temp(id));
if let Some(last_width) = last_width {
ui.add_space((ui.available_width() - last_width) / 2.0);
}
let res = ui
@juancampa
juancampa / pind.sh
Last active December 1, 2021 14:42
pind: Pins a command to a directory
#!/bin/sh
# Any command invoked with the pind prefix gets stored along with its $PWD so
# subsequent invocations are executed in the same directory. For example:
#
# $ pind pwd
# /home/juan
# $ cd /etc
# $ pind pwd
# /home/juan
@juancampa
juancampa / stashit.sh
Last active June 22, 2021 18:05
Iterate over all stash items
stashit ()
{
for i in `seq 0 20`;
do
echo Changed files:;
git stash show stash@{$i};
echo Untracked files:;
git diff --summary stash@{$i}^2;
echo;
while true; do
@juancampa
juancampa / default.conf
Created March 6, 2020 16:01
Minimal openresty config streaming HTTP V2 issue
server {
listen 443 ssl http2;
error_log stderr warn;
ssl_certificate /etc/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/certs/nginx-selfsigned.key;
server_name localhost;
location / {
access_by_lua_block {
@juancampa
juancampa / hyper-throughput-test.sh
Created December 12, 2018 20:24
Hyper throughput perf test
#!/bin/bash
RUNS=5
for i in `seq 1 $RUNS`; do
starttime=`date "+%s%3N"`
# Generate a significant amount of output
ls -Rl /usr/ /usr/ /usr/ /usr/
@juancampa
juancampa / gmail-compose-encoder.js
Created August 26, 2018 20:59 — forked from danrouse/gmail-compose-encoder.js
gmail `compose` query parameter encoder/decoder
const fullAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const restrictedAlphabet = 'BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz';
const threadPrefix = 'thread-';
const messagePrefix = 'msg-';
const isWhitespace = str => /^[\s\xa0]*$/.test(str);
const isInvalidString = str => str ? (str.indexOf(threadPrefix) !== -1 || str.indexOf(messagePrefix) !== -1) : false;
const encode = function(str) {
if (isWhitespace(str)) return str;
@juancampa
juancampa / membrane-cli-query.sh
Last active July 4, 2018 03:09
Example of a Membrane query from the CLI
$ membrane query
'github:users.one(name:"facebook").repos.one(name:"react")'
'{
issues {
page {
items {
state
comments
self
}