Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
list/select and rewrite ~/.mikutter symlink
#!/usr/bin/env ruby
FLOATING_PATH = File.join(ENV["HOME"], ".mikutter")
FLOATING_PATH_DIR = File.dirname(FLOATING_PATH)
FLOATING_PATH_BASE = File.basename(FLOATING_PATH)
if File.exists? FLOATING_PATH and not File.symlink? FLOATING_PATH
puts "error: #{FLOATING_PATH} exists but not a symlink"
exit 1
end
current_link = File.basename(File.readlink(FLOATING_PATH))
candidates = Dir.entries(FLOATING_PATH_DIR).select{ |entry|
entry =~ /^#{FLOATING_PATH_BASE}/
}.reject{ |entry|
entry == FLOATING_PATH_BASE
}
select = 1
candidates.each do |dir|
if dir == current_link
printf " <%d> ", select
else
printf " %d ", select
end
puts dir
select += 1
end
changeto = nil
loop do
print "change to [1..#{select-1} or query]: "
changeto = STDIN.gets
if changeto == nil
puts
puts "cancelled"
exit
end
changeto.chomp!
match = candidates.select{ |dir| dir =~ /#{changeto}/ }
if changeto =~ /^\d+$/
break if (1..select-1).to_a.include? changeto.to_i
elsif match.size == 1
changeto = candidates.index(match.first) + 1
break
else
p match
end
end
newdir = candidates[changeto.to_i-1]
File.delete FLOATING_PATH
File.symlink File.join(FLOATING_PATH_DIR, newdir), FLOATING_PATH
puts "updated symbolic link: #{FLOATING_PATH_BASE} => #{newdir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment