Skip to content

Instantly share code, notes, and snippets.

@ykai55
ykai55 / cli_progress.py
Created May 20, 2019 13:07
python cli progress
import time
def print_progress(completed, length=20, cell='█', empty='░', begin='', end=''):
completed = int(completed)
completed = length if completed > length else completed
print(begin + cell * completed + empty * (length-completed) + end, end='')
for i in range(51):
print('\r', end='')
print_progress(i, 50, cell='=', empty=' ', begin='[', end=']')
@ykai55
ykai55 / path_of_fd.md
Created May 10, 2019 15:24
Linux从文件描述符获取文件名

通过fchdir和getcwd获取文件描述符对应的文件名

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char cwdbuf[4096];
@ykai55
ykai55 / multiscreen.md
Last active May 4, 2019 03:53
多显示器排列组合
# 有三个显示器
outs = ['e1', 'h1', 'e2']

# 需要得到的数组
[['e1'],
 ['h1'],
 ['e2'],
 ['e1', 'h1'],
 ['e1', 'e2'],
@ykai55
ykai55 / args_parse.md
Last active March 21, 2019 07:11
shell script arguments parsing
want_touch_pinch=1
want_debug=0
want_temp_profile=0
want_verbose=0
while [ $# -gt 0 ]; do
  case "$1" in
    -h | --help | -help )
      usage
 exit 0 ;;
@ykai55
ykai55 / args_test.md
Created March 21, 2019 07:08
arguments test of shell function
test "$VERSION" || return 2
test "$FILE_NAME" || return 3
@ykai55
ykai55 / autofeh.py
Created January 7, 2019 07:50
autofeh: auto wallpaper changer for i3wm
#! /usr/bin/env python3
# change wallpaper every argv[2] seconds
# photo folder is argv[1]
# use "kill -s 10 `/tmp/autofeh.pid`" to change by yourself
import os
import sys
import time
import signal
@ykai55
ykai55 / bytes_struct.cs
Created November 24, 2018 18:57
C#: interconversion between bytes and struct
protected static TStruct BytesToStruct<TStruct>(byte[] bs, int bs_length) where TStruct : new()
{
int typeLen = Marshal.SizeOf(typeof(TStruct));
IntPtr tmp = Marshal.AllocHGlobal(typeLen); // 分配托管内存
Marshal.Copy(bs, 0, tmp, bs_length); // 将字节复制到托管内存
TStruct rv = (TStruct)Marshal.PtrToStructure(tmp, typeof(TStruct));
Marshal.FreeHGlobal(tmp); // 释放托管内存
return rv;
}
@ykai55
ykai55 / rekde.sh
Created November 4, 2018 05:21
restart plasma
#! /usr/bin/bash
kquitapp5 plasmashell && kstart5 plasmashell
@ykai55
ykai55 / random.sh
Created November 3, 2018 17:16
generate random characters from /dev/urandom on Linux
cat /dev/urandom | od -x | cut - -d ' ' -f 2- --output-delimiter ''
@ykai55
ykai55 / forward.md
Last active December 18, 2018 09:33
使用ncat端口转发
tail -f server_output | ncat -l -p 8888 --keep-open | ncat localhost 8000 >> server_output

这行命令可以将来自8888端口的数据转发到localhost:8000

Update

mkfifo fifo
cat fifo | ncat -l -p 8888 | pv | ncat localhost 8000 > fifo