[{"content":"Obviously, if English isn\u0026rsquo;t your first language, you end up switching keyboard layouts a lot. On just about every system except macOS, that usually means using a keyboard shortcut. I think Caps Lock is the easiest key to reach. On Linux, all you need is a setting like grp:caps_toggle. Macs have a dedicated Globe key (🌐/Fn). Good idea, but I still have to stretch my pinky to reach it. It sometimes misses presses, too, because of delays in how special keys are handled. Caps Lock also has an LED that can show you which layout is active. That\u0026rsquo;s what Linux does by default when you set Caps Lock to switch layouts.\nOf course, Apple has already decided for you, so there\u0026rsquo;s no easy fix. Time to dig out the duct tape. First up, let\u0026rsquo;s get Caps Lock to switch layouts.\nmacOS has a built-in setting to switch layouts with Caps Lock. There\u0026rsquo;s a catch, though. It handles special keys in a weird way, so if you start typing right after pressing Caps Lock, the layout just doesn\u0026rsquo;t switch. Maybe the system thinks it was an accidental tap or is waiting for a key combination. As usual, macOS thinks it knows better than you. I tried getting rid of the delay with hidutil property --set '{\u0026quot;CapsLockDelayOverride\u0026quot;:0}'. No luck.\nSo I went the long way around and remapped Caps Lock to F18 (or another function key). I started out with Karabiner-Elements, but hidutil itself turned out to be a cleaner way to do it, with no extra software. macOS then sees a regular keypress, and the weird delay goes away.\nRemap Caps Lock to F18 Here\u0026rsquo;s the command.\n1 2 3 4 5 6 hidutil property --set \u0026#39;{ \u0026#34;UserKeyMapping\u0026#34;: [{ \u0026#34;HIDKeyboardModifierMappingSrc\u0026#34;: 0x700000039, \u0026#34;HIDKeyboardModifierMappingDst\u0026#34;: 0x70000006D }] }\u0026#39; Unfortunately, the remap only lasts until you reboot, so let\u0026rsquo;s have launchd apply it.\nCreate the agent file at ~/Library/LaunchAgents/com.local.caps-to-f18.plist.\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 \u0026lt;?xml version=\u0026#34;1.0\u0026#34; encoding=\u0026#34;UTF-8\u0026#34;?\u0026gt; \u0026lt;!DOCTYPE plist PUBLIC \u0026#34;-//Apple//DTD PLIST 1.0//EN\u0026#34; \u0026#34;http://www.apple.com/DTDs/PropertyList-1.0.dtd\u0026#34;\u0026gt; \u0026lt;plist version=\u0026#34;1.0\u0026#34;\u0026gt; \u0026lt;dict\u0026gt; \u0026lt;key\u0026gt;Label\u0026lt;/key\u0026gt; \u0026lt;string\u0026gt;com.local.caps-to-f18\u0026lt;/string\u0026gt; \u0026lt;key\u0026gt;ProgramArguments\u0026lt;/key\u0026gt; \u0026lt;array\u0026gt; \u0026lt;string\u0026gt;/usr/bin/hidutil\u0026lt;/string\u0026gt; \u0026lt;string\u0026gt;property\u0026lt;/string\u0026gt; \u0026lt;string\u0026gt;--set\u0026lt;/string\u0026gt; \u0026lt;string\u0026gt;{\u0026#34;UserKeyMapping\u0026#34;:[{\u0026#34;HIDKeyboardModifierMappingSrc\u0026#34;:0x700000039,\u0026#34;HIDKeyboardModifierMappingDst\u0026#34;:0x70000006D}]}\u0026lt;/string\u0026gt; \u0026lt;/array\u0026gt; \u0026lt;key\u0026gt;RunAtLoad\u0026lt;/key\u0026gt; \u0026lt;true/\u0026gt; \u0026lt;/dict\u0026gt; \u0026lt;/plist\u0026gt; Now load the agent into the current session.\n1 2 plutil -lint \u0026#34;$HOME/Library/LaunchAgents/com.local.caps-to-f18.plist\u0026#34; launchctl bootstrap \u0026#34;gui/$(id -u)\u0026#34; \u0026#34;$HOME/Library/LaunchAgents/com.local.caps-to-f18.plist\u0026#34; Set F18 to switch layouts Now let\u0026rsquo;s tell macOS to use F18 to switch layouts.\nIn System Settings → Keyboard → Text Input → Edit, turn off the built-in Caps Lock layout switch if it was enabled. In System Settings → Keyboard → Keyboard Shortcuts → Input Sources, enable Select next source in Input menu. Click the shortcut on the right and press Caps Lock. It should register as F18. Use the LED to show the active layout Install and launch Hammerspoon with Lua support by following the setup instructions. Add this snippet to ~/.hammerspoon/init.lua and choose Reload Config from the Hammerspoon menu. The LED stays off for the U.S. layout and lights up for every other input source.\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 local function updateCapsLED() local source = hs.keycodes.currentSourceID() if not source or source == \u0026#34;\u0026#34; then return end local ledOn = source ~= \u0026#34;com.apple.keylayout.US\u0026#34; local ok = hs.hid.led.set(\u0026#34;caps\u0026#34;, ledOn) print(string.format( \u0026#34;[caps-led] source=%s layout=%s led=%s ok=%s\u0026#34;, source, hs.keycodes.currentLayout() or \u0026#34;?\u0026#34;, ledOn and \u0026#34;ON\u0026#34; or \u0026#34;OFF\u0026#34;, tostring(ok) )) end capsLEDWatcher = hs.distributednotifications.new( updateCapsLED, \u0026#34;com.apple.Carbon.TISNotifySelectedKeyboardInputSourceChanged\u0026#34; ) capsLEDWatcher:start() updateCapsLED() ","date":"2026-12-05T14:12:39+03:00","image":"https://averylex.dev/p/switching-keyboard-layouts-with-caps-lock-on-macos/caps-lock-cover_hu_be6a9550aca3ac8c.jpg","permalink":"https://averylex.dev/p/switching-keyboard-layouts-with-caps-lock-on-macos/","title":"Switching Keyboard Layouts with Caps Lock on macOS"},{"content":"Why PicSavBot? Have you ever wanted to send a pic in a chat, you remember what was on it, but you can\u0026rsquo;t find it in your gallery? I have. That\u0026rsquo;s exactly why I created the Telegram bot @picsavbot to solve this problem. Just send it the images you like (as well as stickers and videos) and it will save them to your personal library. Now you can use that very pic at the perfect moment in any chat by simply typing @picsavbot description of the pic in the input field.\nHow to write good queries If you type \u0026ldquo;fat cat\u0026rdquo;, first (provided you have such images) you will see fat cats, then just cats, and then, most likely, other animals. The CLIP neural network is not very good at reading text, but it does understand some English. On the other hand, it often recognises logos — for example, Minecraft or the SCP Foundation — quite well. But it knows almost nothing about memes, so write descriptions of what is in the picture as if you were explaining it to your grandma.\nWhat you want to find Good search query Shlyopa Cat with big ears Gigachad Muscular man Gachi meme Fight in a locker room Mental hospital Orderly reaching out his hand It\u0026rsquo;s Wednesday my dudes Frog Among Us Red astronaut How the bot works You send an image/sticker/video to the Telegram bot @picsavbot; it passes it to the neural network CLIP (the model used is LAION\u0026rsquo;s CLIP-ViT-H-14-laion2B-s32B-b79K) and saves the resulting multi-dimensional vector representing the image content to a PostgreSQL database extended with the pgvector plugin for vector storage and search. When saving an animated sticker or video, the preview frame (usually the first frame) is taken.\nTo find the desired pic among the saved ones, you can type @picsavbot picture description in the message field of any chat. Using Yandex.Translate, the bot will translate the entered description into English, analyse it with CLIP and find the most suitable images in the database by their cosine similarity.\nWhen you send one of the found images, the bot increases its usage count in its database, and if there is no search query (you type @picsavbot without anything else) it will first show the most frequently used pics and stickers.\nAbout the project Source code: averyanalex/picsavbot.\nProgramming languages: Rust, Python.\nKey libraries: tokio (asynchronous runtime), teloxide (framework for building Telegram bots), seaorm (database interaction).\n","date":"2024-04-02T12:59:30+03:00","image":"https://averylex.dev/p/picsavbot-a-bot-for-saving-and-searching-images/picsavbot-question-cucumber_hu_1c450e076482bfe6.jpg","permalink":"https://averylex.dev/p/picsavbot-a-bot-for-saving-and-searching-images/","title":"PicSavBot: a bot for saving and searching images"},{"content":"About the Project Source code: github.com/averyanalex/matetech-answers-bot.\nProgramming languages: Rust, SQL.\nKey libraries:\ntokio (asynchronous runtime) reqwest (HTTP client) sqlx (database driver) teloxide (framework for building Telegram bots) Creation Story In 9th grade I enrolled in an \u0026ldquo;olympiad\u0026rdquo; school—a good one with almost no \u0026ldquo;extra\u0026rdquo; subjects, so you could peacefully prepare for competitions. Many non-core subjects, like biology, were given as so-called distance courses that worked roughly like this: open the e-learning site, watch a 10–15-minute lecture, take a quiz while searching the answers online, and get your well- earned 80–100 % score.\nThe Idea and the First Version The process was honestly pretty boring. And what do you do when it gets boring with a browser tab open? Right—you open the developer tools. Poke at the elements and show your friends how you got 200 % on a quiz in 3 seconds. Open the Network tab and watch what the site sends where while you\u0026rsquo;re doing the quiz. Finish a quiz and open the results…\nAnd it turns out the JSON with the results contains the correct answers, even though the site itself doesn\u0026rsquo;t show them! Interesting. Let\u0026rsquo;s dig deeper. Copy the request as curl and run it in the terminal. Great, we can see that JSON. Start a new quiz, change the attempt ID in the request, and… we get a response from the server! With the quiz results (and the right answers) before finishing the quiz itself.\nThe find was interesting, of course. But I also wanted to help my classmates and not have to open the console every time. I asked a friend to send me the attempt ID of one of his quizzes, substituted it… it worked! I removed the authorization header from the request—access error. So the genius developers of the platform did check for the presence of authorization (as I recall, a valid one—a random token won\u0026rsquo;t work), but completely skipped the permission check. Well, good job guys, at least you did something.\nThe rest was just engineering. I wrote a simple Telegram bot in Python that, given a quiz ID, sent all the answers. But I didn\u0026rsquo;t want to \u0026ldquo;out\u0026rdquo; that the answers were essentially public on the site, or reveal the bot. I rewrote the bot—now when it sees an attempt ID it hasn\u0026rsquo;t encountered before it posts the answers to a public Telegram channel, as if the quiz had been solved manually and the site had no bugs.\nWord-of-mouth did its job—in a year the channel had several hundred subscribers.\nThe Second Version The vulnerability—actually a gaping hole—was so obvious that many people found it. By the next year one brilliant weird student of the math-IT track made a project titled something like \u0026ldquo;Vulnerabilities of the Distance Course Platform.\u0026rdquo; I don\u0026rsquo;t know what exactly was in it, but some issues were fixed afterwards. First, fetching somebody else\u0026rsquo;s quiz results became impossible. Second, for multiple-choice questions the correct answer stopped being displayed in the results.\nIt was clear the bot needed to become more sophisticated, and I didn\u0026rsquo;t want to develop that Python prototype further (it had already worked for more than a year). A friend and I decided to write a new version in Rust since we knew it better. We implemented login with token saving to the database, so we could get quiz results for different users.\nThe only remaining problem was with multiple-choice questions—unlike text input ones, there were no answers. But after carefully looking at the JSON I noticed a score field. It turned out to be 0 if the current saved answer is wrong and\n0 if it\u0026rsquo;s correct. Perfect, we can simply iterate over all options until the score changes.\nOf course, this required teaching the bot to answer questions on its own. In general it wasn\u0026rsquo;t too hard, and as a bonus we gained the ability to automatically complete the quiz, not just fetch the answers. Cool—now \u0026ldquo;quizzes are solved automatically for everyone, and nobody even opens them!\u0026rdquo; (A reference to a sketch at the 9th-grade graduation. Back then it wasn\u0026rsquo;t true yet.)\nThat\u0026rsquo;s roughly how, after a couple of evenings of Rust coding, the second version of the bot for getting distance-course answers was written. We released it, posted a link in the Telegram channel. Not everyone rushed to sign up—many were afraid to send their login/password from the course site to our bot. But, as they say, that\u0026rsquo;s their problem: if they don\u0026rsquo;t want to use it—they don\u0026rsquo;t have to.\nAttempts to Shut the Bot Down Soon some enterprising school students decided to try making money off it. They created their own similar bot (with pay-per-use) and started messaging me with proposals to shut mine down. Actually, I think they first tried back when instead of a bot there were Telegram channels, but less actively.\nThey offered super \u0026ldquo;profitable\u0026rdquo; (not) deals: make my bot paid with the same price as theirs, or shut it down and get up to 10 % of their profit. I of course refused and jokingly offered them a price of 300 k. For some reason they weren\u0026rsquo;t happy. I thought that was it, but no…\nThey created a channel where they started claiming that I, allegedly, made the bot to steal account credentials and complete quizzes for 0 points. They created a fake profile of me, screencapped chats supposedly with me where \u0026ldquo;I\u0026rdquo; say that cheating is bad and therefore I completed their quizzes with zeros. I don\u0026rsquo;t know who believed them—seems almost no one. I still don\u0026rsquo;t know who those people were and frankly I don\u0026rsquo;t care.\nExamples of Platform Bugs ","date":"2024-03-26T23:08:36+03:00","image":"https://averylex.dev/p/how-laziness-and-curiosity-saved-me-dozens-of-hours/bot-v2-example_hu_d156a4ae30b881b9.png","permalink":"https://averylex.dev/p/how-laziness-and-curiosity-saved-me-dozens-of-hours/","title":"How Laziness and Curiosity Saved Me Dozens of Hours"},{"content":"What is Yggdrasil? Yggdrasil is a decentralized mesh network where every participant is equal. Each computer in the network (a node) is assigned a static public IPv6 address, and any other participant can reach it directly.\nThis can be used, for example, to play network games across different local networks. One player runs a Minecraft server without paying for an external (\u0026ldquo;white\u0026rdquo;) IP address, and another player from a different city connects to it via Yggdrasil.\nInstallation Let\u0026rsquo;s get started: we\u0026rsquo;ll download and install Yggdrasil, configure peers to connect to the rest of the network, check that everything works, and obtain our own IP address.\nDownload the installer Open the page with the latest Yggdrasil release on GitHub.\nFind the file yggdrasil-x.y.z-x64.msi (for version 0.4.7 this is yggdrasil-0.4.7-x64.msi). If you have a 32-bit system (from the 2000s), download yggdrasil-x.y.z-x86.msi instead.\nInstalling the MSI Open the downloaded MSI file.\nConfirm the launch by clicking Run.\nWhen prompted, grant the installer administrator rights.\nThe installer window will open and no additional questions will be asked. After installation finishes (it takes only a few seconds) the window will close automatically.\nThat\u0026rsquo;s it for the installation process, now let\u0026rsquo;s proceed to configuration.\nError 2503 Sometimes the 2503 error appears when installing MSI files. Open the properties of the folder C:\\Windows\\Temp, go to the Security tab and make sure that the Administrators group and your user (add it if it isn\u0026rsquo;t listed) have full permissions. Information taken from Microsoft Community.\nConfiguring peers Peers are the servers Yggdrasil connects to in order to reach the rest of the network.\nIt is better to choose one common, stable peer for all players – this minimizes ping and prevents other people\u0026rsquo;s traffic from passing through your connection (public peers are responsible for forwarding traffic).\nChoosing peers There is a repository with a list of public peers on GitHub. Peers are grouped by continent, country and city. Choose peers that are geographically closer to the host – this will reduce ping for all players.\nFor example, you can use my peer (Moscow): tls://ygg-msk-1.averyan.ru:8363 or quic://ygg-msk-1.averyan.ru:8364 (the quic variant is preferable if it works for you and isn\u0026rsquo;t blocked).\nHowever, not all of them may work – you can check the list of available peers on the developer\u0026rsquo;s website.\nEditing the config Press Win+R to open the Run dialog. Enter the path to the config: %ALLUSERSPROFILE%\\Yggdrasil\\yggdrasil.conf\nClick OK. If Windows asks how to open the file, choose Notepad.\nInsert the peers you chose into the Peers list:\n1 2 3 4 Peers: [ tls://ygg.averyan.ru:8362 tcp://box.paulll.cc:13337 ] Save the file with Ctrl+S or via the File menu.\nRestarting Yggdrasil For Yggdrasil to apply the new settings, it needs to be restarted. The most obvious (but slow) way is to reboot the computer. Instead, we\u0026rsquo;ll restart it via Task Manager.\nOpen Task Manager with Ctrl+Shift+Esc or by right-clicking the Windows icon (Start button) on the taskbar.\nIf the simplified Task Manager opens, click More details.\nOpen the Services tab, find Yggdrasil at the very bottom, right-click it and choose Restart.\nFinal steps Installation and configuration are complete – let\u0026rsquo;s check that everything works and find out our IP address.\nChecking functionality Open Command Prompt.\nEnter the command \u0026quot;C:\\Program Files\\Yggdrasil\\yggdrasilctl.exe\u0026quot; getPeers to view the list of connected peers. There should be as many as you configured. If there are fewer, some peers are unavailable.\nIf an error occurs when entering this command, try rebooting the computer.\nTry opening and pinging the Yggdrasil website (available only from the Yggdrasil network and may load quite slowly):\n1 ping [319:3cf0:dd1d:47b9:20c:29ff:fe2c:39be] Getting your IP address To find out your own IP address, enter the command \u0026quot;C:\\Program Files\\Yggdrasil\\yggdrasilctl.exe\u0026quot; getSelf:\nThe highlighted line will show your IPv6 address in the Yggdrasil network.\nYou\u0026rsquo;ll notice that the ping to your own address is practically zero.\nConfiguring games Yggdrasil is now installed on your computer and running. Let\u0026rsquo;s use it for network games.\nMinecraft Install Yggdrasil on the device that will host the server and on those that will connect to it.\nStart a LAN game on the host (Open to LAN) and note the port:\nFind out the host\u0026rsquo;s IP address. (In my case it is 200:d08:17f6:af6e:b285:16de:d3ba:387a)\nConnect to the server (enter the IP in square brackets and specify the port after the colon):\nIf you can\u0026rsquo;t connect, try configuring the Windows Firewall or turning it off completely, and also check the ping to the host.\nDone! Now you can play Minecraft with your friends at any time.\n","date":"2022-12-14T23:33:19+03:00","image":"https://averylex.dev/p/installing-yggdrasil-on-windows/install-process_hu_c58797ea5f38c3c8.png","permalink":"https://averylex.dev/p/installing-yggdrasil-on-windows/","title":"Installing Yggdrasil on Windows"},{"content":"Advantages of managing Hugo website with Nix Reproducible builds: Nix allows you to reproducible build your Hugo website across multiple machines. Dependency management: Nix makes it easy to manage theme, other dependencies and even Hugo itself for your website. Easy deployment: just set root of your nginx virtualHost to site\u0026rsquo;s package. Setup your site Write flake.nix This is an example of flake.nix I use for this blog, adapt it for your needs.\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 { description = \u0026#34;AveryanAlex\u0026#39;s personal blog\u0026#34;; inputs = { nixpkgs.url = \u0026#34;github:nixos/nixpkgs/nixos-unstable\u0026#34;; flake-utils.url = \u0026#34;github:numtide/flake-utils\u0026#34;; # Import theme stack = { url = github:CaiJimmy/hugo-theme-stack; flake = false; }; }; outputs = { self, nixpkgs, flake-utils, stack }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; blog = pkgs.stdenv.mkDerivation { name = \u0026#34;blog\u0026#34;; # Exclude themes and public folder from build sources src = builtins.filterSource (path: type: !(type == \u0026#34;directory\u0026#34; \u0026amp;\u0026amp; (baseNameOf path == \u0026#34;themes\u0026#34; || baseNameOf path == \u0026#34;public\u0026#34;))) ./.; # Link theme to themes folder and build buildPhase = \u0026#39;\u0026#39; mkdir -p themes ln -s ${stack} themes/stack ${pkgs.hugo}/bin/hugo --minify \u0026#39;\u0026#39;; installPhase = \u0026#39;\u0026#39; cp -r public $out \u0026#39;\u0026#39;; meta = with pkgs.lib; { description = \u0026#34;AveryanAlex\u0026#39;s personal blog\u0026#34;; platforms = platforms.all; }; }; in { packages = { blog = blog; default = blog; }; devShells.default = pkgs.mkShell { buildInputs = with pkgs; [ hugo ]; # Link theme to themes folder shellHook = \u0026#39;\u0026#39; mkdir -p themes ln -sf ${stack} themes/stack \u0026#39;\u0026#39;; }; }); } Setup direnv 1 2 3 echo use flake \u0026gt;\u0026gt; .envrc echo .direnv \u0026gt;\u0026gt; .gitignore direnv allow Add result to .gitignore Result folder is a symlink to built website created by nix build command.\n1 echo result \u0026gt;\u0026gt; .gitignore Preview and build Run development server:\n1 hugo server -D Build production release with nix:\n1 nix build Deploying to nginx webserver Now we will create nginx virtualHost config for the site.\nAdd website to server\u0026rsquo;s flake inputs:\n1 2 3 { inputs.blog.url = \u0026#34;github:averyanalex/blog\u0026#34;; } Setup nginx virtualHost to serve your site:\n1 2 3 4 5 6 7 8 9 { inputs, pkgs, ... }: { services.nginx.virtualHosts.\u0026#34;averyan.ru\u0026#34; = { root = inputs.blog.defaultPackage.${pkgs.hostPlatform.system}; useACMEHost = \u0026#34;averyan.ru\u0026#34;; forceSSL = true; }; } ","date":"2022-11-29T15:31:37+03:00","image":"https://averylex.dev/p/nix-flakes-for-hugo-site/hugo-build-nix_hu_afa0a1b886929a0d.png","permalink":"https://averylex.dev/p/nix-flakes-for-hugo-site/","title":"Nix flakes for Hugo site"},{"content":"How can we tell apart lossy codecs such as MP3? I will not go into detail about how lossy audio codecs work—you can read about it in the Wikipedia article on audio data compression. To spot fake FLAC files created by converting from MP3 we will take advantage of the fact that lossy compression chops off high frequencies. For example, a 320-kbit MP3 removes everything above 20 kHz (for lower bitrates the cutoff is even lower). In lossless formats such as FLAC the audio remains in its original form.\nInstalling the spectrum analysis program Spek Spek is a free, open-source spectrum analyzer for audio files that runs on Windows, Linux and macOS. It will help us identify fake FLAC.\nWindows, macOS: spek.cc.\nOn Linux, most distributions provide the package spek.\nChecking FLAC files for authenticity Open the audio file you are interested in with Spek and look at its frequency spectrum.\nAs examples we will use Status Quo – \u0026ldquo;In The Army Now\u0026rdquo;, transcoded from a 320-kbit MP3, and AC/DC – \u0026ldquo;T.N.T.\u0026rdquo; preserved in lossless quality.\nExample spectrum of a genuine FLAC Example spectrum of a fake FLAC transcoded from MP3 The MP3 spectrum cutoff at 20 kHz is clearly visible.\n","date":"2022-08-28T18:19:11+03:00","image":"https://averylex.dev/p/how-to-check-flac-quality/spek_hu_a471501deb17b3b3.png","permalink":"https://averylex.dev/p/how-to-check-flac-quality/","title":"How to Check FLAC Quality?"}]