Sha256: 6e11225ceaa5ef64a3ae9ed4fde61a17b33590492b566408c46555fe840ac0ae
Contents?: true
Size: 1.87 KB
Versions: 6
Compression:
Stored size: 1.87 KB
Contents
# -*- coding: UTF-8 -*- require 'mj/path' require 'tempfile' module MJ; module Tools @@editor = nil # Look for a suitable editor in the users environment. # # We check # 1. +$VISUAL+ (if +$DISPLAY+ is set) # 2. +$GUIEDITOR+ (if +$DISPLAY+ is set) # 3. +$EDITOR+ # 4. Giving up # The result is cached. def Tools.editor() return @@editor if @@editor logger.debug( 'Looking for a editor.' ) if ENV.include?( 'DISPLAY' ) logger.debug( 'Checking $VISUAL' ) return @@editor = ENV['VISUAL'] if ENV.include?( 'VISUAL' ) logger.debug( 'Checking $GUIEDITOR' ) return @@editor = ENV['GUIEDITOR'] if ENV.include?( 'GUIEDITOR' ) end logger.debug( 'Checking $EDITOR' ) return @@editor = ENV['EDITOR'] if ENV.include?( 'EDITOR' ) end # A class that puts the given content in a tmpfile, opens an editor for the user to play with # it. After the user closed the editor it will give back the new content. class TmpFileEditor def initialize( content ) @file = Tempfile.new( [ 'configuration', '.yaml' ] ) # First write the old content into the file @file.write( content ) @file.close() end def edit() # Check if there is a editor around here. editor = Tools::editor() if editor.nil? logger.error( 'Could not find a editor to use (xdg-open, $VISUAL, $GUIEDITOR, $EDITOR).' ) end # Execute the editor cmd = "#{editor} #{@file.path}" logger.trace( "> #{cmd}" ) if system( cmd ).nil? logger.error( "Failed to start editor #{editor}" ) end return 0 end def path() @file.path() end end end; end # module MJ::Tools
Version data entries
6 entries across 6 versions & 1 rubygems