How to Change View of Coding Block in WordPress?

Problem

Many times a good WordPress theme does not render code properly. There are problems like too big font making it harder to read the text. I use StackEdit to prepare posts and publish to my blog.

Solution

There is a way to solve this problem with custom CSS.

Description

The posting source is in Markdown. StackEdit translates MarkDown to HTML and posts to the destination. There is not much I could do with the Markdown. However, the theme on the WordPress blog allows me to set a CSS for particular content.
The code blocks are translated to pre blocks in the HTML. So adding a custom rendering for pre block should fix the problem.
I found a CSS snippet from https://wp-mix.com/css-style-pre-tags/.

pre {
	box-sizing: border-box;
	width: 100%;
	padding: 0;
	margin: 0;
	overflow: auto;
	overflow-y: hidden;
	font-size: 12px;
	line-height: 20px;
	background: #efefef;
	border: 1px solid #777;
	background: url('lines.png') repeat 0 0;
	padding: 10px;
	color: #333;
}

Another good reference is https://perishablepress.com/perfect-pre-tags/

This made the code look much much better.

Written with StackEdit.

Advertisements

How to Pick a Commit of Git Submodule?

Current State

There is a new submodule ‘test’ and we want to add it to the repo.

Steps

Git add submodule

$ cd my_repo
$ git submodule add git@github.com:vishalkanaujia/dev-tools.git

Initialize the repo

$ git submodule update --init --recursive

cd to the submodule

$ cd dev-tools

By default, the latest version is picked. We can checkout to a commit for moving a different version.

$ git checkout abdcca6

Written with StackEdit.

The Best Go Library for Redis

There are two popular choices:

  • go-redis
  • redigo

I prefer redigo over go-redis.

Cons with go-redis

  • Not enough documentation for APIs
  • Difficult to understand naming conventions for APIs
  • I’m still finding how to refresh expiry time of a key using go-redis
  • Needs Cmdable interface for calling Redis commands. The interface is not so well designed.

An interface in go-redis

   type Cmdable interface {
   TTL(key string) *DurationCmd
   Restore(key string, ttl time.Duration, value string) *StatusCmd

Pros with redigo

  • Easy to understand APIs
  • Ability to call Redis commands from the client so you never have to learn a new wrapper
  • No extra interface like go-redis

A sample client with go-redis is as following:

  package main

  import (
      "fmt"

      "github.com/go-redis/redis"
  )

  func HandleErr(err error) {
      if err != nil {
          panic(err)
      }
  }

  /*
  type DurationCmd struct {
      baseCmd

      val       time.Duration
      precision time.Duration
  }
  */

  func ExampleNewClient() {
      client := redis.NewClient(&redis.Options{
          Addr:     "localhost:6379",
          Password: "", // no password set
          DB:       0,  // use default DB
      })
      
      err = client.Set("key", "value", 0).Err()
      HandleErr(err)

      val, err := client.Get("key").Result()
      HandleErr(err)
      fmt.Println("key", val)

      ttl := client.TTL("key")
      fmt.Println("ttl", ttl)
  }

  func main() {
      ExampleNewClient()
  }

Simplifying go-kit toolkit for Microservices – Part I

Introduction

go-kit is one of the most complete and flexible toolkits for developing microservices in Go language. At the same time, the learning curve of go-kit is steep. In this post, I’m trying to explain go-kit fundamental components using general purpose client-server model of Linux.

General Purpose Client-Server Architecture

A server in Linux binds & listens on a port, accepts connections and server requests. A client creates its own socket, makes a request to the server (IP: PORT). The request is just a buffer. The server spawns a thread or runs the handler in the same process. The handler is a function that understands request parameters and typecast it to its expected type.
The client receives a response from the server. The response is read into a buffer and then interpreted.

go-kit in Client-Server Paradigm

go-kit helps create a service. A service listens on a well-known port and IP. It is the server equivalent of Linux server. The server defines handlers for different types of request (PUT, GET, POST). A handler is a function called for a service request. The handler is a generic wrapper over service specific business logic implementation. A handler for GET call may eventually call get_status_reservation() in a sample reservation system.

go-kit intends to keep the core logic away from go-kit influence. The core logic (set of functions that your service implements) stays in a file called service.go. The remaining go-kit code tries to access these functions in an abstract manner. There are entities called endpoints, transport and server. Each of these allows a generic interaction of service functions through HTTP (or gRPC).

The overall objective is to expose service functionality through go-kit toolchain.

Transport

It defines the send and receiver buffer structure. Each service API could expect and send different data. The transport defines structures that define request and response for each API

In the next post, I will share endpoints, the most interesting part of go-kit.

Install Redis on Mac OS via Homebrew

Install Redis on Mac OS via Homebrew

  • Install redis package
    $ brew install redis
    Updating Homebrew...
    ==> Auto-updated Homebrew!
    
  • After installation, you’d see the following message:
     ==> Downloading https://homebrew.bintray.com/bottles/redis-5.0.4.mojave.bottle.tar.gz
     ############################################################# 100.0%
    ==> Pouring redis-5.0.4.mojave.bottle.tar.gz
    ==> Caveats
    To have launchd start redis now and restart at login:
      brew services start redis
    Or, if you don't want/need a background service you can just run:
    redis-server /usr/local/etc/redis.conf
    
  • Run redis as following:
    $ redis-server /usr/local/etc/redis.conf
    oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    

Connect to Redis with redis-cli

redis-cli 5.0.4

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
    -n <db>            Database number.
$ redis-cli -h <host> -p <port> -n <db>

Find a String in Files Excluding Some Files


tags: ‘shell, linux, command, development, grep,find’
categories: development

I use the following command to find a string in a Go repo. The vendor directory is not needed, so skip it.

$ find . -type f |grep -v vendor|cut -d":" -f2|xargs grep -n my_search_string

The command find all files in current directory. Next, it removes all files that have vendor in their path.

401:./.git/HEAD
402:./.git/info/exclude
403:./.git/logs/HEAD

The cut command picks the filename and passed to xargs. Each file is then processed by grep for the search string.

Golang Type Assertions: Dynamic Type Checking

Type Assertions

Go supports dynamic type checking of variables. If the type of a variable is unknown till run-time, type assertions can run a validity check.

// getRequest definition
struct getRequest {
    name string
}
// Declare a void (anonymous) type variable
request interface{}

// Checking if variable request is not nil && 
// has type getRequest. 
request.(getRequest)

Summary

  • Type Assertion does runtime type check of a variable.
  • It also checks that value is non-nil.
  • Return true on success, false otherwise.

References

Written with StackEdit.

How to Quickly Find WordAds Earning for WordPress Blog?

How to Quickly Find WordAds Earning for WordPress Blog?

WordAds earning is revealed after the third week of the next month. The earning of January 2019 would come by Feb 21st or later.

However, there is a quicker way!

This method is applicable for WordPress hosted blogs.

Steps

Wordpress Blog Earning

Summary

This page has detailed information on earnings and ads served on your website. You need not wait for three weeks anymore now :).

Mac OS iTerm2 validateTextureDimensions: failed assertion

I was playing with font sizes on iTerm2 on Mac OS (Mojave) and suddenly iTerm2 started to crash uncontrollably. In spite of uninstall/new install, the crash persisted.
The panic string was:

validateTextureDimensions:1081: failed assertion
`MTLTextureDescriptor has width (78728) greater
than the maximum allowed size of 16384.'

However, trying to start iTerm2 would open an empty terminal and its menu bar. I thought of checking the iTerm2 settings and found font size value of 5000.

The value might have changed while I was trying to update font size. iTerm could never recover after seeing such a huge value as rendering them would need more memory that could fit in its buffers.

Fix

  • Just make sure you have a reasonable font size (< 20 ?) value. That is all.

What iTerm versions are affected?

  • Build 3.3.20190430-nightly

Written with StackEdit.

Fixing Terminal Row & Columns Display on Docker and Linux

Ever faced garbled screen on a terminal, text wrapping over and screen command string messed up!
It happens because the terminal is using default row and column value (e.g. columns = 80).

The following command fixes it (tested on Docker container’s terminal)

docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -ti 

How it Works?

  • tput initializes or reset a terminal by providing terminal info to the shell.
  • From man page of tput:
    The tput utility uses the terminfo database to make the
    values of terminal-dependent capabilities and information available to the shell
    
  • Let’s find the type of terminal
    # echo $TERM
      xterm
    
  • Let’s see what’s suggested rows and columns for this terminal.
    # tput cols
    167
    # tput lines
    49
    
  • While accessing the container terminal, we passed the number of columns and rows to COLUMNS & LINES variable.
  • The terminal database is present at /usr/share/terminfo.

References

Written with StackEdit.