Class: Fusuma::Plugin::Wmctrl::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/fusuma/plugin/wmctrl/workspace.rb

Overview

Manage workspace

Defined Under Namespace

Classes: MissingMatrixOption

Instance Method Summary collapse

Constructor Details

#initialize(wrap_navigation: nil, matrix_col_size: nil) ⇒ Workspace

Returns a new instance of Workspace.



10
11
12
13
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 10

def initialize(wrap_navigation: nil, matrix_col_size: nil)
  @wrap_navigation = wrap_navigation
  @matrix_col_size = matrix_col_size
end

Instance Method Details

#matrix_size(total_workspace_num) ⇒ Array<Integer>

Returns:



35
36
37
38
39
40
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 35

def matrix_size(total_workspace_num)
  must_have_matrix_option!
  col_size = @matrix_col_size
  row_size = (total_workspace_num / col_size)
  [row_size.to_i, col_size.to_i]
end

#move_command(direction:) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 105

def move_command(direction:)
  workspace_num = case direction
                  when 'next'
                    next_workspace_num(step: 1)
                  when 'prev'
                    next_workspace_num(step: -1)
                  else
                    raise "#{direction} is invalid key"
                  end
  "wmctrl -s #{workspace_num}"
end

#move_command_for_matrix(direction:) ⇒ Object



117
118
119
120
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 117

def move_command_for_matrix(direction:)
  workspace_num = next_workspace_num_for_matrix(direction: direction)
  "wmctrl -s #{workspace_num}"
end

#move_window_command(direction:) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 122

def move_window_command(direction:)
  workspace_num = case direction
                  when 'next'
                    next_workspace_num(step: 1)
                  when 'prev'
                    next_workspace_num(step: -1)
                  else
                    raise "#{direction} is invalid key"
                  end
  "wmctrl -r :ACTIVE: -t #{workspace_num} ; wmctrl -s #{workspace_num}"
end

#move_window_command_for_matrix(direction:) ⇒ Object



134
135
136
137
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 134

def move_window_command_for_matrix(direction:)
  workspace_num = next_workspace_num_for_matrix(direction: direction)
  "wmctrl -r :ACTIVE: -t #{workspace_num} ; wmctrl -s #{workspace_num}"
end

#must_have_matrix_option!Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 42

def must_have_matrix_option!
  return if @matrix_col_size

  # FIXME: move to executor
  warn("    Please set matrix-col-size to config.yml\n\n    ```config.yaml\n    plugin:\n      executors:\n        wmctrl_executor:\n          matrix-col-size: 2\n    ```\n  ERRRORMESSAGE\n  raise MissingMatrixOption, 'You need to set matrix option to config.yml'\nend\n")

#next_workspace_num(step:) ⇒ Integer

get next workspace number

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 17

def next_workspace_num(step:)
  current_workspace_num, total_workspace_num = workspace_values

  next_workspace_num = current_workspace_num + step

  return next_workspace_num unless @wrap_navigation

  if next_workspace_num.negative?
    next_workspace_num = total_workspace_num - 1
  elsif next_workspace_num >= total_workspace_num
    next_workspace_num = 0
  else
    next_workspace_num
  end
  next_workspace_num
end

#next_workspace_num_for_matrix(direction:) ⇒ Integer

Returns:

Raises:

  • RuntimeError



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fusuma/plugin/wmctrl/workspace.rb', line 61

def next_workspace_num_for_matrix(direction:)
  must_have_matrix_option!
  current_workspace_num, total_workspace_num = workspace_values
  row_size, col_size = matrix_size(total_workspace_num)
  x = current_workspace_num % col_size
  y = current_workspace_num / col_size
  case direction
  when 'right'
    if x < col_size - 1
      current_workspace_num + 1
    elsif @wrap_navigation
      current_workspace_num - (col_size - 1)
    else
      current_workspace_num
    end
  when 'left'
    if x.positive?
      current_workspace_num - 1
    elsif @wrap_navigation
      current_workspace_num + (col_size - 1)
    else
      current_workspace_num
    end
  when 'down'
    if y < row_size - 1
      current_workspace_num + col_size
    elsif @wrap_navigation
      (current_workspace_num + col_size) - total_workspace_num
    else
      current_workspace_num
    end
  when 'up'
    if y.positive?
      current_workspace_num - col_size
    elsif @wrap_navigation
      total_workspace_num + (current_workspace_num - col_size)
    else
      current_workspace_num
    end
  else
    raise "#{direction} is invalid key"
  end
end