Class: Hiiro::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/tasks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: Dir.pwd, config: nil) ⇒ Environment

Returns a new instance of Environment.



1605
1606
1607
1608
# File 'lib/hiiro/tasks.rb', line 1605

def initialize(path: Dir.pwd, config: nil)
  @path = path
  @config = config
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



1599
1600
1601
# File 'lib/hiiro/tasks.rb', line 1599

def path
  @path
end

Class Method Details

.currentObject



1601
1602
1603
# File 'lib/hiiro/tasks.rb', line 1601

def self.current
  new(path: Dir.pwd)
end

Instance Method Details

#all_appsObject



1626
1627
1628
# File 'lib/hiiro/tasks.rb', line 1626

def all_apps
  @all_apps ||= config.apps
end

#all_sessionsObject



1618
1619
1620
# File 'lib/hiiro/tasks.rb', line 1618

def all_sessions
  @all_sessions ||= Hiiro::Tmux::Session.all
end

#all_tasksObject



1614
1615
1616
# File 'lib/hiiro/tasks.rb', line 1614

def all_tasks
  @all_tasks ||= config.tasks
end

#all_treesObject



1622
1623
1624
# File 'lib/hiiro/tasks.rb', line 1622

def all_trees
  @all_trees ||= Tree.all
end

#app_matcherObject



1638
1639
1640
# File 'lib/hiiro/tasks.rb', line 1638

def app_matcher
  @app_matcher ||= Hiiro::Matcher.new(all_apps, :name)
end

#configObject



1610
1611
1612
# File 'lib/hiiro/tasks.rb', line 1610

def config
  @config ||= TaskManager::Config.new
end

#find_app(abbreviated) ⇒ Object



1693
1694
1695
1696
# File 'lib/hiiro/tasks.rb', line 1693

def find_app(abbreviated)
  return nil if abbreviated.nil?
  app_matcher.find(abbreviated).first&.item
end

#find_session(abbreviated) ⇒ Object



1688
1689
1690
1691
# File 'lib/hiiro/tasks.rb', line 1688

def find_session(abbreviated)
  return nil if abbreviated.nil?
  session_matcher.find(abbreviated).resolved&.item
end

#find_task(abbreviated) ⇒ Object



1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
# File 'lib/hiiro/tasks.rb', line 1665

def find_task(abbreviated)
  return nil if abbreviated.nil?

  if abbreviated.include?('/')
    result = task_matcher.resolve_path(abbreviated)
    return result.resolved&.item if result.match?

    parent_prefix, child_prefix = abbreviated.split('/', 2)
    if 'main'.start_with?(child_prefix)
      return task_matcher.find(parent_prefix).first&.item
    end

    nil
  else
    task_matcher.find(abbreviated).first&.item
  end
end

#find_tree(abbreviated) ⇒ Object



1683
1684
1685
1686
# File 'lib/hiiro/tasks.rb', line 1683

def find_tree(abbreviated)
  return nil if abbreviated.nil?
  tree_matcher.find(abbreviated).first&.item
end

#sessionObject



1657
1658
1659
# File 'lib/hiiro/tasks.rb', line 1657

def session
  @session ||= Hiiro::Tmux::Session.current
end

#session_matcherObject



1634
1635
1636
# File 'lib/hiiro/tasks.rb', line 1634

def session_matcher
  @session_matcher ||= Hiiro::Matcher.new(all_sessions, :name)
end

#taskObject



1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/hiiro/tasks.rb', line 1646

def task
  @task ||= begin
    s = session
    t = tree
    all_tasks.find { |task|
      (s && task.session_name == s.name) ||
        (t && task.tree_name == t.name)
    }
  end
end

#task_matcherObject



1642
1643
1644
# File 'lib/hiiro/tasks.rb', line 1642

def task_matcher
  @task_matcher ||= Hiiro::Matcher.new(all_tasks, :name)
end

#treeObject



1661
1662
1663
# File 'lib/hiiro/tasks.rb', line 1661

def tree
  @tree ||= all_trees.find { |t| t.match?(path) }
end

#tree_matcherObject



1630
1631
1632
# File 'lib/hiiro/tasks.rb', line 1630

def tree_matcher
  @tree_matcher ||= Hiiro::Matcher.new(all_trees, :name)
end