Skip to content

Instantly share code, notes, and snippets.

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

Giuseppe Eletto giuseppe998e

🏠
Working from home
View GitHub Profile
@giuseppe998e
giuseppe998e / remove_by_ref.rs
Last active April 11, 2024 07:35
Rust lang example on how to remove a struct from a vector using the item reference
#[derive(Debug)]
struct Structure {
name: String,
}
#[derive(Debug)]
struct StructVector(Vec<Structure>);
impl StructVector {
fn by_name(&self, name: &str) -> Option<&Structure> {
@giuseppe998e
giuseppe998e / serde_peekable_access.rs
Last active March 25, 2024 14:36
Wrappers for peeking into Serde's "MapAccess" and "SeqAccess" implementations without advancing the iterator. The "PeekableMapAccess" struct allows peeking at the next key and/or value in a map, while "PeekableSeqAccess" provides similar functionality for sequences.
//! Licensed under either of:
//! - Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
//! - MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
//!
//! This file may not be copied, modified, or distributed except according to those terms.
use std::marker::PhantomData;
use serde::{__private::de as private_de, de};
@giuseppe998e
giuseppe998e / text_shuffle.js
Last active June 6, 2023 08:58
Javascript code that shuffles the text of an HTML element
!function () {
const shuffleVelocityMs = 50;
const shuffleElement = (element) => {
const elemText = element.getAttribute("data-text");
const elemementChars = Array.from(elemText);
const shuffleChars = (chars) => {
for (let idx = chars.length; --idx; ) {
const randomIdx = parseInt(Math.random() * idx);
@giuseppe998e
giuseppe998e / nge.rs
Last active December 14, 2022 19:29
"Next Greater Element" and "Previous Greater Element" arrays using stack in Rust - Complexity O(n)
fn next_greater_elems<T: Copy + PartialOrd>(array: &[T]) -> Box<[Option<T>]> {
let array_len = array.len();
let mut stack = Vec::<usize>::with_capacity(array_len);
let mut result = Vec::<Option<T>>::with_capacity(array_len);
stack.push(0);
result.push(None);
for (index, element) in array.iter().enumerate().skip(1) {
result.push(None);
@giuseppe998e
giuseppe998e / trait_test.php
Created October 26, 2022 23:12
Test of PHP traits behavior using "self" and "static" keywords
<?php declare(strict_types = 1);
// Tried on PHP 8.0.25
// Trait
trait TestTrait {
public static function spawnSelf(): self {
return new self();
}
public static function spawnStatic(): static {
@giuseppe998e
giuseppe998e / media_rename.py
Created July 16, 2022 14:03
A Python3 script that asynchronously renames files (media) in a directory
#!/usr/bin/env python3
# Copyright 2022 Giuseppe Eletto <giuseppe@eletto.me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@giuseppe998e
giuseppe998e / twitchchatreader.js
Last active August 4, 2022 07:34
An IRC WebSocket client that reads Twitch channel chat without the need to authenticate
/**
* MIT License
*
* Copyright (c) 2022 Giuseppe Eletto <giuseppe@eletto.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>base64 + lzma + iframe</title>
<style>iframe{position:absolute;top:0;left:0;height:100vh;width:100%;border:none}</style>
</head>
<body>
<iframe sandbox="allow-same-origin allow-downloads allow-scripts allow-forms allow-top-navigation allow-popups allow-modals allow-popups-to-escape-sandbox"></iframe>
@giuseppe998e
giuseppe998e / dead-simple-js-calculator.html
Last active June 25, 2022 10:08
A dead simple calculator written in HTML, CSS and pure JS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dead simple JS Calculator</title>
<style>
body {
background-color: whitesmoke;
margin: 0;
@giuseppe998e
giuseppe998e / kvm-image.xml
Last active May 13, 2022 12:45
KVM - User mode network port forwarding
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<!-- ... -->
<devices>
<!-- ... -->
<interface type="user">
<!-- ... -->
<model type="e1000e"/>
<!-- ... -->
</interface>
<!-- ... -->