Module: Terminitor::Runner

Included in:
Cli
Defined in:
lib/terminitor/runner.rb

Overview

This module contains all the helper methods for the Cli component.

Constant Summary collapse

TERM_PATH =

Terminitor Global Path

File.join(ENV['HOME'],'.config','terminitor')

Instance Method Summary collapse

Instance Method Details

#capture_core(platform) ⇒ Object

Defines how to capture terminal settings on the specified platform

Parameters:

  • the (String)

    ruby platform



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/terminitor/runner.rb', line 34

def capture_core(platform)
  core = case platform.downcase
  when %r{darwin} then 
    if ENV['TERM_PROGRAM'] == 'iTerm.app'
      Terminitor::ItermCapture
    else
      Terminitor::MacCapture
    end
  when %r{linux}  then
    if not `which terminator`.chomp.empty?
      Terminitor::TerminatorCapture
    else
      Terminitor::KonsoleCapture # TODO silly fallback, make better check
    end
  else nil
  end
end

#config_path(file, type = :yml) ⇒ Object

Return file in config_path

Examples:

config_path ‘/path/to’, :term

Parameters:

  • Termfile (String)

    path

  • Type (Symbol)

    of file



107
108
109
110
111
112
113
114
# File 'lib/terminitor/runner.rb', line 107

def config_path(file, type = :yml)
  return File.join(options[:root],"Termfile") if file.empty?
  if type == :yml
    File.join(TERM_PATH, "#{file.sub(/\.yml$/, '')}.yml")
  else
    File.join(TERM_PATH, "#{file.sub(/\.term$/, '')}.term")
  end
end

#execute_core(method, project) ⇒ Object

Execute the core with the given method.

Examples:

execute_core :process!, 'project'
execute_core :setup!, 'my_project'

Parameters:

  • symbol (Symbol)

    of method

  • Termfile (String)

    name



58
59
60
61
62
63
64
65
# File 'lib/terminitor/runner.rb', line 58

def execute_core(method, project)
  if path = resolve_path(project)
    core = find_core(RUBY_PLATFORM)
    core ? core.new(path).send(method) : say("No suitable core found!")
  else
    return_error_message(project)
  end
end

#find_core(platform) ⇒ Object

Finds the appropriate platform core, else say you don’t got it.

Examples:

find_core RUBY_PLATFORM

Parameters:

  • the (String)

    ruby platform



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/terminitor/runner.rb', line 12

def find_core(platform)
  core = case platform.downcase
  when %r{darwin} then 
    if ENV['TERM_PROGRAM'] == 'iTerm.app'
      Terminitor::ItermCore
    else
      Terminitor::MacCore
    end
  when %r{linux}  then
   if not `which terminator`.chomp.empty?
     Terminitor::TerminatorCore
   else
     Terminitor::KonsoleCore # TODO silly fallback, make better check
   end
  when %r{mswin|mingw} then
    Terminitor::CmdCore
  else nil
  end
end

#github_clone(username, project) ⇒ Object

This will clone a repo in the current directory. It will first try to clone via ssh(read/write), if not fall back to git-read only, else, fail.

Examples:

github_clone ‘achiu’, ‘terminitor’

Parameters:

  • Github (String)

    username

  • Github (String)

    project name



133
134
135
136
137
138
# File 'lib/terminitor/runner.rb', line 133

def github_clone(username, project)
  github = `which github`
  return false if github.empty?
  command = "github clone #{username} #{project}"
  system(command + " --ssh") || system(command)
end

#github_repo(username, project, options = {}) ⇒ Object

Fetch the git repo and run the setup block

Examples:

fetch_repo ‘achiu’, ‘terminitor’, :setup => true

Parameters:

  • Github (String)

    username

  • Github (String)

    project

  • options (Hash) (defaults to: {})

    hash



145
146
147
148
149
150
151
152
153
# File 'lib/terminitor/runner.rb', line 145

def github_repo(username, project, options ={})
  if github_clone(username, project)
    path = File.join(Dir.pwd, project)
    FileUtils.cd(path)
    invoke(:setup, []) if options[:setup]
  else
    say("could not fetch repo!")
  end
end

#grab_comment_for_file(file) ⇒ Object

returns first line of file

Examples:

grab_comment_for_file ‘/path/to’

Parameters:

  • Termfile (String)

    path



98
99
100
101
# File 'lib/terminitor/runner.rb', line 98

def grab_comment_for_file(file)
  first_line = File.readlines(file).first
  first_line =~ /^\s*?#/ ? ("-" + first_line.gsub("#","")) : "\n"
end

#open_in_editor(path, editor = nil) ⇒ Object

opens doc in system designated editor

Examples:

open_in_editor '/path/to', 'nano'

Parameters:

  • path (String)

    to termfile

  • editor (String) (defaults to: nil)


72
73
74
75
76
# File 'lib/terminitor/runner.rb', line 72

def open_in_editor(path, editor=nil)
  editor = editor || ENV['TERM_EDITOR'] || ENV['EDITOR']
  say "please set $EDITOR or $TERM_EDITOR in your .bash_profile." unless editor
  system("#{editor || 'open'} #{path}")
end

#resolve_path(project) ⇒ Object

returns path to file

Examples:

resolve_path ‘my_project’

Parameters:

  • Termfile (String)

    name



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/terminitor/runner.rb', line 81

def resolve_path(project)
  unless project.empty?
    path = config_path(project, :yml) # Give old yml path
    return path if File.exists?(path)
    path = config_path(project, :term) # Give new term path.
    return path if File.exists?(path)
    nil
  else
    path = File.join(options[:root],"Termfile")
    return path if File.exists?(path)
    nil
  end
end

#return_error_message(project) ⇒ Object

Returns error message depending if project is specified

Examples:

return_error_message ‘hi

Parameters:

  • Termfile (String)

    name



119
120
121
122
123
124
125
# File 'lib/terminitor/runner.rb', line 119

def return_error_message(project)
  unless project.empty?
    say "'#{project}' doesn't exist! Please run 'terminitor edit #{project.gsub(/\..*/,'')}'"
  else
    say "Termfile doesn't exist! Please run 'terminitor edit' in project directory"
  end
end