selfjungle Just another WordPress weblog

7May/180

colorful bash prompt with git branch

Edit .bashrc :

export COLOR_NC='\e[0m' # No Color
export COLOR_WHITE='\e[1;37m'
export COLOR_BLACK='\e[0;30m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_GRAY='\e[0;30m'
export COLOR_LIGHT_GRAY='\e[0;37m'

function parse_git_dirty {
    [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*"
}
function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
PS1=""
PS1=$PS1"\[$COLOR_GREEN\]\u@\h\:"                # Display user@host
#PS1=$PS1"\[$Yellow\]\@:"                        # Display time
#PS1=$PS1"\[$Blue\][\[$Cyan\]\d\[$Blue\]] "      # Display date
#PS1=$PS1"\[$Yellow\]\@:"                        # Display time
PS1=$PS1"\[$COLOR_CYAN\]\w"                      # Display pwd
PS1=$PS1"\[$COLOR_BROWN\]"'$(parse_git_branch)'  # Display git-branch
PS1=$PS1"\[$COLOR_NC\]$ "                        # Turn off color and end prompt
export PS1=$PS1  
Tagged as: , No Comments
8Mar/180

git checkout pull requests

Edit .git/config :

[remote "origin"]
        url = ...
#       fetch = +refs/heads/*:refs/remotes/origin/*
        fetch = +refs/pull/*/head:refs/remotes/origin/pull/*
Tagged as: No Comments
17Feb/170

binding c++ class members as c signal handlers

#include <signal.h>
#include <functional>

std::function<void(int)> callback_wrapper;
void callback_function(int value)
{
  callback_wrapper(value);
}

class Foo {
public:
  void catch_signal(int) {}
};

int main(int argc, char** argv)
{
    Foo foo;

    // deprecated since C++-11
    callback_wrapper = std::bind1st(std::mem_fun(&Foo::catch_signal),
                                    &foo);
    // or
    callback_wrapper = std::bind(&Foo::catch_signal,
                                 &foo,
                                 std::placeholders::_1);

    struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = callback_function;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGTERM, &sigIntHandler, NULL);
}
Tagged as: , No Comments
31Oct/160

git word diff

git diff --word-diff=color

shows changes in place.

Tagged as: No Comments
16Jul/150

c, c++ measure elapsed time

Elapsed time of function, for performance measurement

C style

#include <sys/time.h>

struct timeval begin_v, end_v;
gettimeofday(&begin_v, NULL);
// the function
gettimeofday(&end_v, NULL);

const double s = end_v.tv_sec + end_v.tv_usec / 1e6 - begin_v.tv_sec - begin_v.tv_usec / 1e6;
printf ("Elasped time: %f s\n", s );

C++ style

#include <chrono>

std::chrono::time_point<std::chrono::steady_clock> start, end;
start = std::chrono::steady_clock::now();
// the function
end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;

std::cout << "Elapsed time: " << elapsed_seconds.count() << " s" << std::endl;

edit: instead of user-adjustable system_clock, the monotonic steady_clock can be a better choice.

Tagged as: , No Comments
26Jun/150

set mouse sensitivity – with xset

# get current stat:
xset q | grep -A 1 Pointer
# set new:
# xset m acceleration threshold
xset m 3/2 4

https://wiki.archlinux.org/index.php/Mouse_acceleration

Filed under: Uncategorized No Comments
21Apr/150

openwrt post-flash

# set root password
telnet 192.168.1.1
  passwd

# install web UI
ssh root@192.168.1.1
  opkg update
# if you'll get 404 errors, edit /etc/opkg.conf and change the package paths:
# https://downloads.openwrt.org/snapshots/trunk/ar71xx/generic/packages/packages
  opkg install luci-ssl
# if you have default_postinst: not found, script returned status 127 errors, check if the package version is ok: http://downloads.openwrt.org/barrier_breaker/14.07-rc3/ar71xx/generic/packages maybe?
# remove & try again:
opkg remove --force-remove luci-ssl px5g libustream-polarssl libpolarssl luci luci-proto-ppp luci-mod-admin-full luci-base luci-lib-nixio  uhttpd-mod-ubus  uhttpd lua luci-app-firewall  luci-theme-bootstrap libiwinfo-lua liblua libuci-lua rpcd luci-lib-ip libubus-lua

# start web server
/etc/init.d/uhttpd start
/etc/init.d/uhttpd enable

# enable wifi
uci set wireless.@wifi-device[0].disabled=0; uci commit wireless; wifi
uci show wireless | grep disabled

login to https://192.168.1.1 and do the rest...

Tagged as: No Comments
20Apr/150

zfs and portage’s var directories

While /var is usually for non-crucial content, caches[3], pid files, etc, portage has a different idea [1]:

/var/db/pkg Portage stores the state of your system
/var/lib/portage The versions for the applications you have explicitly installed

These directories store the current tree state, there is no way recreating them if they are deleted.[2]

So if you plan to use ZFS with separate / and /var to take a snapshot of /, install some packages and then rollback the snapshot as you changed your mind, your / and /var will be out of sync!

/var/db/pkg and /var/lib/portage has to be on /.

mkdir /usr/var_db_pkg /usr/var_lib_portage
cp -r /var/lib/portage /usr/var_lib_portage
cp -r /var/db/pkg /usr/var_db_pkg
rm -rf /var/lib/portage /var/db/pkg
ln -s /usr/var_lib_portage /var/lib/portage
ln -s /usr/var_db_pkg /var/db/pkg

[1] https://wiki.gentoo.org/wiki/Directories
[2] Or at least it is painfull. To avoid the initial circular-dependency hell, issue:

emerge --nodeps dev-lang/perl dev-lang/python dev-libs/libxml2 dev-util/cmake dev-util/pkgconfig sys-apps/acl ys-apps/systemd sys-devel/automake sys-libs/glibc sys-libs/ncurses sys-libs/zlib virtual/libudev

[3] what you'd have to recreate: powertop's calibration measurements, gentoolkit's busybox and initramfs

Tagged as: , No Comments
20Apr/150

zfs set ditto blocks after file system creation

"The copies property works for all new writes, so I recommend that you set that policy when you create the file system or immediately after you create a zpool." [1]

So how can you force a complete reread-rewrite?
With (non-incremental) backup and restore:

# settings properties won't work:
zfs set copies=2 POOL/FS
zfs snapshot SNAPSHOT
zfs send SNAPSHOT | xz --threads=12 --verbose > FILE.img.xz
zfs destoy POOL/FS
xz --threads=12 --decompress --verbose  FILE.img.xz -c | zfs receive POOL/FS

# so you have to create & override a new FS with copies=2
zfs snapshot SNAPSHOT
zfs send SNAPSHOT | xz --threads=12 --verbose > FILE.img.xz
zfs destoy POOL/FS
zfs create ... -o copies=2  POOL/FS
xz --threads=12 --decompress --verbose  FILE.img.xz -c | zfs receive POOL/FS -F

[1] https://blogs.oracle.com/relling/entry/zfs_copies_and_data_protection

Tagged as: No Comments
30Mar/150

HiDPI

add -dpi 276 to /etc/X11/xinit/xserverrc

at firefox about:config set layout.css.devPixelsPerPx to 2

to be continued...

https://wiki.archlinux.org/index.php/HiDPI

Filed under: Uncategorized No Comments