markw-dmenu 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/dmenu.gemspec +19 -0
  4. data/lib/dmenu.rb +105 -0
  5. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ef6ca078ebbfd95bfa5054155cd8edbde1967bb
4
+ data.tar.gz: 2e192a476b78fbab2fdafc98d5150aa6697e7b33
5
+ SHA512:
6
+ metadata.gz: f38873e2aae318b9b34fbcc76f01208fdb025c32b10e733750fcdfdd1c798aaf8deb5c45c745b892ea44cd93cdad9638eae7000a2f8db78fa1aba22c1e921c7e
7
+ data.tar.gz: a226744e2cb98c5d8761e1fdae951a54853a8bba5e1f76b65ab900b8233ccdc41654b5ef5869544c114d67cc0e1402ff82f21ab9f763fbcdf88464bfcb1b7419
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ # gem "rails"
data/dmenu.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "markw-dmenu"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["Mark Watts"]
7
+ spec.email = ["wattsmark2015@gmail.com"]
8
+ spec.summary = %q{A set of utilites for working with the dmenu command line tool.}
9
+ spec.homepage = "http://github.com/mwatts15/dmenu"
10
+ spec.license = "MPL-2.0"
11
+ spec.files = `git ls-files -z`.split("\x0")
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_development_dependency "bundler", "~> 1.7"
17
+ spec.add_development_dependency "rake", "~> 10.0"
18
+ spec.add_runtime_dependency "markw-wcwidth", "~> 0.0.3"
19
+ end
data/lib/dmenu.rb ADDED
@@ -0,0 +1,105 @@
1
+ require 'open3'
2
+
3
+ class String
4
+ def initialize
5
+ super
6
+ force_encoding("utf-8")
7
+ end
8
+ end
9
+ module Dmenu
10
+ require 'wcwidth'
11
+ # Characters that, in the keyed fonts, are actually double width,
12
+ # but not reported as such by wcwidth. This isn't an exhaustive list,
13
+ # just characters I found by visually scanning some titles in my music
14
+ # collection
15
+ $DBL_CHARS = {'Noto Sans Mono CJK JP Regular' => '∞♥ⅢⅣ☆…→'}
16
+ def self.dmenu (entries, prompt='select an item', height=false,
17
+ width=1366,
18
+ fg_color='#FFFFFF',
19
+ bg_color='#000000',
20
+ sel_fg_color='#555555',
21
+ sel_bg_color='#eeeeee',
22
+ font='Sazanami Mincho:pixelsize=14',
23
+ line_height=nil)
24
+ if !height
25
+ height=entries.count
26
+ end
27
+ md = font.to_s.match('.*:pixelsize=(\d+).*')
28
+ if md.nil?
29
+ font_width = 14 #in pixels
30
+ else
31
+ font_width = md[1].to_i
32
+ end
33
+
34
+ findex = font.index(':')
35
+ base_font = font[0..(findex > 0 ? findex - 1 : -1)]
36
+ repl_chars = $DBL_CHARS[base_font]
37
+ res = ""
38
+ lr_separation = 4
39
+ textwidth = 2 * width / font_width - lr_separation
40
+ expr = %r{[^#{repl_chars}]+}
41
+ entries.collect! do |line|
42
+ line = line.each_char.reject{|char| char.ord < 32 or (char.ord >= 0x7f and char.ord < 0xa0)}.inject(:+)
43
+ l, r = line.split("|||")
44
+ loff = l.gsub(expr, "").length
45
+ roff = r.nil? ? 0 : r.gsub(expr, "").length
46
+ scrunched = scrunch(r, [textwidth - l.width - lr_separation, textwidth / 2].max)
47
+ s = r ? alignr(l, scrunched, textwidth - loff - roff) : l
48
+ s
49
+ end
50
+ cmdline = "dmenu -f -p \"#{prompt}\" -nf \"#{fg_color}\" \
51
+ -nb \"#{bg_color}\" \
52
+ -sb \"#{sel_bg_color}\" \
53
+ -sf \"#{sel_fg_color}\" \
54
+ -i -l #{height} \
55
+ -w \"#{width}\" \
56
+ -fn \"#{font}\" " + (if line_height.nil? then "" else "-lh " + line_height.to_s end)
57
+
58
+ err_messages = nil
59
+ Open3.popen3(cmdline) do |i, o, e, t|
60
+ i.print(entries.join("\n"))
61
+ i.close
62
+ err_messages = e.read
63
+ res = o.gets
64
+ end
65
+ stat = $?
66
+ if stat != 0
67
+ if err_messages.include?("Error")
68
+ raise Exception.new("Couldn't print list: #{stat}\n#{err_messages}")
69
+ end
70
+ end
71
+ res.to_s.chomp
72
+ end
73
+ def self.alignr(lhs, r, w)
74
+ x = r
75
+ str = lhs + x
76
+ while str.width < w
77
+ x = '.' + x
78
+ str = lhs + x
79
+ end
80
+ str
81
+ end
82
+ def self.scrunch(str, size, dots='...')
83
+ if str.nil?
84
+ nil
85
+ elsif str.width < size
86
+ str
87
+ else
88
+ middle = str.length / 2
89
+ # Not centered; intentional
90
+ lhs = str[0..middle]
91
+ rhs = str[-middle..-1]
92
+ lr = false
93
+ while str.width > size
94
+ if lr
95
+ lhs = lhs[0..-2]
96
+ else
97
+ rhs = rhs[1..-1]
98
+ end
99
+ lr = !lr
100
+ str = lhs + dots + rhs
101
+ end
102
+ str
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markw-dmenu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Watts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: markw-wcwidth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.3
55
+ description:
56
+ email:
57
+ - wattsmark2015@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - dmenu.gemspec
64
+ - lib/dmenu.rb
65
+ homepage: http://github.com/mwatts15/dmenu
66
+ licenses:
67
+ - MPL-2.0
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A set of utilites for working with the dmenu command line tool.
89
+ test_files: []