Skip to content

Instantly share code, notes, and snippets.

@junegunn

junegunn/b.rb

Last active Sep 25, 2020
Embed
What would you like to do?
b - browse Chrome bookmarks with fzf
#!/usr/bin/env bash
# vim: set filetype=ruby:
# b - browse Chrome bookmarks with fzf
[ $(uname) = Darwin ] || exit 1
which fzf > /dev/null 2>&1 || brew reinstall --HEAD fzf || exit 1
/usr/bin/ruby -x "$0" |
fzf-tmux -u 30% --ansi --multi --no-hscroll --tiebreak=begin |
awk 'BEGIN { FS = "\t" } { print $2 }' |
xargs open
exit $?
#!ruby
# encoding: utf-8
require 'json'
FILE = '~/Library/Application Support/Google/Chrome/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
@krompus

This comment has been minimized.

Copy link

@krompus krompus commented May 4, 2016

This is really cool! Is there a way to adapt this to Firefox? I don't use Chrome, but I'd love to have this!

I'll tinker with it and report back if I figure it out.

@valdoonicanlives

This comment has been minimized.

Copy link

@valdoonicanlives valdoonicanlives commented Nov 16, 2016

Nice and it was very easy to get it to work in linux

@jteneycke

This comment has been minimized.

Copy link

@jteneycke jteneycke commented Feb 12, 2018

Adapted it to work with Linux+Chromium+xdg-open. Thanks!

@pherondk

This comment has been minimized.

Copy link

@pherondk pherondk commented Feb 23, 2020

Easily updated to handle Microsoft Edge (Chromium) on MacOS Mojave as well. Thanks!

@MitchAllain

This comment has been minimized.

Copy link

@MitchAllain MitchAllain commented Mar 22, 2020

added

xopen() {
    for var in "$@"; do
        xdg-open "$var" > /dev/null 2>&1 &
    done
}

export -f xopen

/usr/bin/ruby -x "$0"                                          |
  fzf-tmux -u 30% --ansi --multi --no-hscroll --tiebreak=begin |
  awk 'BEGIN { FS = "\t" } { print $2 }'                       |
  xargs bash -c 'xopen "$@"' _ {}

exit $?

to support fzf multi-select and opening multiple tabs, as well as pushing xdg-open into the background with no output

@BachoSeven

This comment has been minimized.

Copy link

@BachoSeven BachoSeven commented Apr 18, 2020

Nice! I just adapted it to work with the Brave Browser, basically chromium; for some reason mine also needed the xargs -n 1 option in order to work with fzf --multi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.