Skip to content

Instantly share code, notes, and snippets.

@BachoSeven
Forked from jteneycke/b.rb
Last active June 14, 2021 16:55
Show Gist options
  • Save BachoSeven/8ce2df9ef735111127d248a2635f53b9 to your computer and use it in GitHub Desktop.
Save BachoSeven/8ce2df9ef735111127d248a2635f53b9 to your computer and use it in GitHub Desktop.
b - browse Chrome bookmarks with fzf
#!/usr/bin/env bash
# vim: set filetype=ruby:
# b - browse Chrome bookmarks with fzf
# Usage: if you use a popup, you might want to call `bm full `
# NOTE: You might want to install w3m for the preview window.
[[ $1 = full ]] && opts="--height 100%"
/usr/bin/ruby -x "$0" |
fzf-tmux -u 30% --ansi --multi --no-hscroll --tiebreak=begin --preview " echo {-1} | xargs w3m -dump " --preview-window=right:30% $opts |
awk 'BEGIN { FS = "\t" } { print $2 }' |
xargs -n 1 xdg-open > /dev/null 2>&1
exit $?
#!ruby
# encoding: utf-8
require 'json'
FILE = '~/.config/chromium/Default/Bookmarks'
CJK = /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/
def build parent, json
name = [parent, json['name']].compact.join('/')
if json['type'] == 'folder'
json['children'].map { |child| build name, child }
else
{ name: name, url: json['url'] }
end
end
def just str, width
str.ljust(width - str.scan(CJK).length)
end
def trim str, width
len = 0
str.each_char.each_with_index do |char, idx|
len += char =~ CJK ? 2 : 1
return str[0, idx] if len > width
end
str
end
width = `tput cols`.strip.to_i / 2
json = JSON.load File.read File.expand_path FILE
items = json['roots']
.values_at(*%w(bookmark_bar synced other))
.compact
.map { |e| build nil, e }
.flatten
items.each do |item|
name = trim item[:name], width
puts [just(name, width),
item[:url]].join("\t\x1b[36m") + "\x1b[m"
end
@BachoSeven
Copy link
Author

BachoSeven commented May 23, 2020

Revision: added preview panel with w3m.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment