sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit b5406c18053775cda27c923f32a2b5ea1b9e83ec
parent 148cabb073b32123b798a1fc3e876ec997bd8688
Author: Timon Vonk <timonv@gmail.com>
Date:   Fri,  2 Jan 2015 23:08:37 +0100

Replace dl/import with Fiddle

Diffstat:
M bin/sup | 34 ++++++++++------------------------
A lib/sup/util/locale_fiddler.rb | 24 ++++++++++++++++++++++++
A test/unit/test_locale_fiddler.rb | 15 +++++++++++++++
3 files changed, 49 insertions(+), 24 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -7,6 +7,7 @@ require 'rubygems'
 require 'ncursesw'
 
 require 'sup/util/ncurses'
+require 'sup/util/locale_fiddler'
 
 no_gpgme = false
 begin
@@ -102,32 +103,17 @@ global_keymap = Keymap.new do |k|
   end
 end
 
-## the following magic enables wide characters when used with a ruby
-## ncurses.so that's been compiled against libncursesw. (note the w.) why
-## this works, i have no idea. much like pretty much every aspect of
-## dealing with curses.  cargo cult programming at its best.
 require 'rbconfig'
-unless RbConfig::CONFIG['arch'] =~ /openbsd/
-  require 'dl/import'
-  module LibC
-    extend DL.const_defined?(:Importer) ? DL::Importer : DL::Importable
-    setlocale_lib = case RbConfig::CONFIG['arch']
-      when /darwin/; "libc.dylib"
-      when /cygwin/; "cygwin1.dll"
-      when /freebsd/; "libc.so.7"
-      else; "libc.so.6"
-    end
 
-    debug "dynamically loading setlocale() from #{setlocale_lib}"
-    begin
-      dlload setlocale_lib
-      extern "void setlocale(int, const char *)"
-      debug "setting locale..."
-      LibC.setlocale(6, "")  # LC_ALL == 6
-    rescue RuntimeError => e
-      warn "cannot dlload setlocale(); ncurses wide character support probably broken."
-      warn "dlload error was #{e.class}: #{e.message}"
-    end
+unless RbConfig::CONFIG['arch'] =~ /openbsd/
+  debug "dynamically loading setlocale()"
+  begin
+    class LibC; extend LocaleFiddler; end
+    debug "setting locale..."
+    LibC.setlocale(6, "")
+  rescue RuntimeError => e
+    warn "cannot dlload setlocale(); ncurses wide character support probably broken."
+    warn "dlload error was #{e.class}: #{e.message}"
   end
 end
 
diff --git a/lib/sup/util/locale_fiddler.rb b/lib/sup/util/locale_fiddler.rb
@@ -0,0 +1,24 @@
+## the following magic enables wide characters when used with a ruby
+## ncurses.so that's been compiled against libncursesw. (note the w.) why
+## this works, i have no idea. much like pretty much every aspect of
+## dealing with curses.  cargo cult programming at its best.
+require 'fiddle'
+require 'fiddle/import'
+
+module LocaleFiddler
+  extend Fiddle::Importer
+
+  SETLOCALE_LIB = case RbConfig::CONFIG['arch']
+                  when /darwin/; "libc.dylib"
+                  when /cygwin/; "cygwin1.dll"
+                  when /freebsd/; "libc.so.7"
+                  else; "libc.so.6"
+                  end
+
+  dlload SETLOCALE_LIB
+  extern "char *setlocale(int, char const *)"
+
+  def setlocale(type, string)
+    LocaleFiddler.setlocale(type, string)
+  end
+end
diff --git a/test/unit/test_locale_fiddler.rb b/test/unit/test_locale_fiddler.rb
@@ -0,0 +1,15 @@
+require 'test_helper'
+require 'sup/util/locale_fiddler'
+
+class TestFiddle < ::Minitest::Unit::TestCase
+  # TODO this is a silly test
+  def test_fiddle_set_locale
+    before = LocaleDummy.setlocale(6, nil).to_s
+    after = LocaleDummy.setlocale(6, "").to_s
+    assert(before != after, "Expected locale to be fiddled with")
+  end
+end
+
+class LocaleDummy
+  extend LocaleFiddler
+end