require 'tempfile'
module Scide
# Configuration of a GNU Screen session (windows for a specific project).
#
# The configuration will disable the startup message and display a hardstatus line.
# It will also display the windows of the given project.
class Screen
# The default screen hardstatus line.
DEFAULT_HARDSTATUS = '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
# Returns a screen configuration for the given project.
#
# ==== Arguments
# * project - The project.
# * options - Screen-specific options (see below).
#
# ==== Options
# * binary - Screen binary (defaults to screen).
# * args - Command-line arguments that will be given to screen (e.g. -U for unicode).
# * hardstatus - Hardstatus line configuration (defaults to #DEFAULT_HARDSTATUS).
def initialize project, options
raise ArgumentError, 'screen configuration must be a hash' unless options.nil? or options.kind_of?(Hash)
@project = project
@options = options || {}
end
# Runs screen with this configuration.
#
# The configuration is saved to a temporary file, then removed.
def run
file = Tempfile.new 'scide'
save file.path
system to_command(file.path)
file.unlink
end
# Returns the command that will be used to run screen with this configuration.
#
# ==== Arguments
# * tmp_file - The temporary file in which the configuration will be stored.
# (Optional for dry-run.)
def to_command tmp_file = 'TEMPORARY_FILE'
"cd #{@project.path} && #{binary} #{args} -c #{tmp_file}"
end
# Verifies that the screen binary is there. If not, causes scide
# to fail with a screen_not_found error (see Scide#fail}.
def check_binary
Scide.fail :screen_not_found, "ERROR: #{binary} not found" unless system("which #{binary}", { [ :out, :err ] => :close })
end
# Returns a representation of this configuration as a string.
def to_s
String.new.tap do |s|
s << "startup_message off\n"
s << "hardstatus on\n"
s << "hardstatus alwayslastline\n"
s << "hardstatus string '#{hardstatus}'\n\n"
s << @project.to_screen
end
end
private
# Returns the screen hardstatus line given as option, or
# the default #DEFAULT_HARDSTATUS.
def hardstatus
@options[:hardstatus] || DEFAULT_HARDSTATUS
end
# Returns the screen binary given as option, or the
# default (screen).
def binary
@options[:binary] || 'screen'
end
# Returns the screen command-line arguments given as options.
def args
@options[:args]
end
# Saves this configuration to the file at the given path.
def save file
File.open(file, 'w'){ |f| f.write to_s }
end
end
end