Skip to content

Instantly share code, notes, and snippets.

View srishanbhattarai's full-sized avatar
💭
Make software fast again

Srishan srishanbhattarai

💭
Make software fast again
View GitHub Profile
@srishanbhattarai
srishanbhattarai / flatten.js
Last active April 4, 2019 05:27
JS Object Deep Flatten (ES6)
/**
* Flattens a nested object into a flat form delimited by "." character at
* each level. Check tests for an example.
*
* @param {Object} nestedObject
* @returns {Object}
*/
export const flattenObject = nestedObject => {
const flattenedObject = {}
const keys = Object.keys(nestedObject)
@srishanbhattarai
srishanbhattarai / git-local-del
Created March 27, 2019 06:30
Oneliner to remove local Git branches that don't exist on remote
git for-each-ref --format '%(refname:short)' refs/heads | grep -Ev `git ls-remote --quiet --heads origin | awk '{print substr($2, 12)}'| paste -sd "|" -` | xargs git branch -D
@srishanbhattarai
srishanbhattarai / mergeSort.go
Last active February 28, 2019 15:42
Merge Sort in Go
package main
import "fmt"
func main() {
sortAndPrint([]int{5, 2, 4, 6, 1, 3})
sortAndPrint([]int{5, 6, 7, 1, 2, 3})
sortAndPrint([]int{500, 400, 300, 200, 100, 0})
}
@srishanbhattarai
srishanbhattarai / screenshot.sh
Created February 13, 2019 10:43
Screenshot Android ADB
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
@srishanbhattarai
srishanbhattarai / new-command.vim
Last active February 4, 2019 09:39
New uesr-defined commands in vim
" Entry point. Initialize RPC. If it succeeds, then attach commands to the `rpcnotify` invocations.
function! s:connect()
let id = s:initRpc()
if 0 == id
echoerr "calculator: cannot start rpc process"
elseif -1 == id
echoerr "calculator: rpc process is not executable"
else
" Mutate our jobId variable to hold the channel ID
" Initialize the channel
if !exists('s:calculatorJobId')
let s:calculatorJobId = 0
endif
" The path to the binary that was created out of 'cargo build' or 'cargo build --release". This will generally be 'target/release/name'
let s:bin = '/path/to/rust-binary'
" Entry point. Initialize RPC. If it succeeds, then attach commands to the `rpcnotify` invocations.
function! s:connect()
@srishanbhattarai
srishanbhattarai / eventhandler4.rs
Last active February 4, 2019 09:26
Echoing back responses to neovim
fn recv(&mut self) {
let receiver = self.nvim.session.start_event_loop_channel();
for (event, values) in receiver {
match Messages::from(event) {
// Handle 'Add'
Messages::Add => {
let nums = values
.iter()
.map(|v| v.as_i64().unwrap())
@srishanbhattarai
srishanbhattarai / eventhandler3.rs
Last active February 4, 2019 07:05
Implementation of data serialization for Neovim plugins
fn recv(&mut self) {
let receiver = self.nvim.session.start_event_loop_channel();
for (event, values) in receiver {
match Messages::from(event) {
// Handle 'Add'
Messages::Add => {
let nums = values
.iter()
.map(|v| v.as_i64().unwrap())
@srishanbhattarai
srishanbhattarai / eventhandler2.rs
Created February 4, 2019 06:40
Skeleton of the recv() method for eventhandler
// Rest of the stuff remains the same.
fn recv(&mut self) {
let receiver = self.nvim.session.start_event_loop_channel();
for (event, values) in receiver {
match Messages::from(event) {
Messages::Add => {
// do stuff
}
@srishanbhattarai
srishanbhattarai / messages.rs
Last active February 4, 2019 06:29
Messages.rs for the Neovim Plugin blog
enum Messages {
Add,
Multiply,
Unknown(String),
}
impl From<String> for Messages {
fn from(event: String) -> Self {
match &event[..] {
"add" => Messages::Add,