Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Created January 6, 2016 18:33
Show Gist options
  • Save dalehamel/a34bf316b325c670bab2 to your computer and use it in GitHub Desktop.
Save dalehamel/a34bf316b325c670bab2 to your computer and use it in GitHub Desktop.
Control i3 from ruby
#!/usr/bin/env ruby
require 'json'
$displays={}
$active_display=nil
$max_workspace=-1
outputs=`i3-msg -t get_outputs`
JSON.parse(outputs).each do |output|
if output["active"] == true
$displays[output["name"]] = {:workspaces => [],
:current => output["current_workspace"].to_i
}
end
end
ws_out=`i3-msg -t get_workspaces`
JSON.parse(ws_out).each do | workspace |
$displays[workspace["output"]][:workspaces].push(workspace["num"])
if workspace["num"] > $max_workspace
$max_workspace = workspace["num"]
end
if workspace["focused"]
$active_display = workspace["output"]
end
end
$focused_display = $displays[$active_display]
$focused_index = $focused_display[:workspaces].index($focused_display[:current])
def workspace_container_left
left_workspace = $focused_display[:workspaces][$focused_index -1]
`i3-msg move container to workspace #{left_workspace}`
workspace_focus_left
end
def workspace_container_right
index = $focused_index + 1
if index == $focused_display[:workspaces].length
index = 0
end
right_workspace = $focused_display[:workspaces][index]
`i3-msg move container to workspace #{right_workspace}`
workspace_focus_right
end
def workspace_focus_left
left_workspace = $focused_display[:workspaces][$focused_index -1]
`i3-msg workspace #{left_workspace}`
end
def workspace_focus_right
index = $focused_index + 1
if index == $focused_display[:workspaces].length
index = 0
end
right_workspace = $focused_display[:workspaces][index]
`i3-msg workspace #{right_workspace}`
end
def workspace_create_with_focused
$max_workspace+=1
`i3-msg move container to workspace #{$max_workspace}`
`i3-msg workspace #{$max_workspace}`
end
def workspace_create
$max_workspace+=1
`i3-msg workspace #{$max_workspace}`
end
def display_focus_left
display_array = $displays.keys()
display_index = display_array.index($active_display) -1
display_left = display_array[display_index]
$focus_display = $displays[display_left]
puts $focus_left
$focus_workspace = $focus_display[:current]
puts $focus_workspace
`i3-msg workspace #{$focus_workspace}`
end
def display_focus_right
display_array = $displays.keys()
display_index = display_array.index($active_display) - + 1
if display_index == display_array.length
display_index = 0
end
display_right = display_array[display_index]
puts display_right
$focus_display = $displays[display_right]
$focus_workspace = $focus_display[:current]
puts $focus_workspace
`i3-msg workspace #{$focus_workspace}`
end
target = ARGV[0]
command = ARGV[1]
option = ARGV[2]
if target == 'workspace'
if command == 'focus'
if option == 'left'
workspace_focus_left
elsif option == 'right'
workspace_focus_right
end
elsif command == 'container'
if option == 'left'
workspace_container_left
elsif option == 'right'
workspace_container_right
end
elsif command == 'create'
if option == 'focused'
workspace_create_with_focused
elsif option == 'empty'
workspace_create
end
end
elsif target == 'display'
if command == 'focus'
if option == 'left'
display_focus_left
elsif option == 'right'
display_focus_right
end
end
end
# i3 config file (v4)
#
# Please see http://i3wm.org/docs/userguide.html for a complete reference!
#
# This config file uses keycodes (bindsym) and was written for the QWERTY
# layout.
#
# To get a config file with the same key positions, but for your current
# layout, use the i3-config-wizard
#
# Font for window titles. Will also be used by the bar unless a different font
# is used in the bar {} block below. ISO 10646 = Unicode
#font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
#font pango:DejaVu Sans Mono, Terminus Bold Semi-Condensed 11
font pango:DejaVu Sans Mono 10
# The font above is very space-efficient, that is, it looks good, sharp and
# clear in small sizes. However, if you need a lot of unicode glyphs or
# right-to-left text rendering, you should instead use pango for rendering and
# chose a FreeType font, such as:
# font pango:DejaVu Sans Mono 10
# use Mouse+Mod4 to drag floating windows to their wanted position
floating_modifier Mod4
set $control ~/.i3/control.rb
# start a terminal
bindsym Mod4+s exec i3-sensible-terminal
# kill focused window
bindsym Mod4+q kill
#bindsym Mod4+p exec ~/bin/autolock #xscreensaver-command -lock
# start dmenu (a program launcher)
bindsym Mod4+e exec dmenu_run -fn DejaVuSansMono-10
# There also is the (new) i3-dmenu-desktop which only displays applications
# shipping a .desktop file. It is a wrapper around dmenu, so you need that
# installed.
# bindsym Mod4+d exec --no-startup-id i3-dmenu-desktop
# change focus
bindsym Mod4+h focus left
bindsym Mod4+l focus right
bindsym Mod4+j focus down
bindsym Mod4+k focus up
# alternatively, you can use the cursor keys:
bindsym Mod4+Left focus left
bindsym Mod4+Down focus down
bindsym Mod4+Up focus up
bindsym Mod4+Right focus right
#bindsym Mod4+Shift+comma exec $control workspace focus left
#bindsym Mod4+Shift+period exec $control workspace focus right
#bindsym Mod4+w exec $control workspace create
# move focused window
bindsym Mod4+Shift+h move left
bindsym Mod4+Shift+l move right
bindsym Mod4+Shift+j move down
bindsym Mod4+Shift+k move up
## alternatively, you can use the cursor keys:
#bindsym Mod4+Shift+Left move left
#bindsym Mod4+Shift+Down move down
#bindsym Mod4+Shift+Up move up
#bindsym Mod4+Shift+Right move right
# split in horizontal orientation
bindsym Mod4+m split h
# split in vertical orientation
bindsym Mod4+v split v
# enter fullscreen mode for the focused container
bindsym Mod4+f fullscreen
# change container layout (stacked, tabbed, toggle split)
#bindsym Mod4+s layout stacking
#bindsym Mod4+w layout tabbed
#bindsym Mod4+e layout toggle split
# toggle tiling / floating
#bindsym Mod4+t floating toggle
#
# change focus between tiling / floating windows
#bindsym Mod4+space focus mode_toggle
# focus the parent container
#bindsym Mod4+a focus parent
# focus the child container
#bindsym Mod4+d focus child
# move the currently focused window to the scratchpad
bindsym Mod4+Shift+space move scratchpad
# Show the next scratchpad window or hide the focused scratchpad window.
# If there are multiple scratchpad windows, this command cycles through them.
bindsym Mod4+space scratchpad show
# switch to workspace
#bindsym Mod4+1 workspace 1
#bindsym Mod4+2 workspace 2
#bindsym Mod4+3 workspace 3
#bindsym Mod4+4 workspace 4
#bindsym Mod4+5 workspace 5
#bindsym Mod4+6 workspace 6
#bindsym Mod4+7 workspace 7
#bindsym Mod4+8 workspace 8
#bindsym Mod4+9 workspace 9
#bindsym Mod4+0 workspace 10
#
## move focused container to workspace
#bindsym Mod4+Shift+1 move container to workspace 1
#bindsym Mod4+Shift+2 move container to workspace 2
#bindsym Mod4+Shift+3 move container to workspace 3
#bindsym Mod4+Shift+4 move container to workspace 4
#bindsym Mod4+Shift+5 move container to workspace 5
#bindsym Mod4+Shift+6 move container to workspace 6
#bindsym Mod4+Shift+7 move container to workspace 7
#bindsym Mod4+Shift+8 move container to workspace 8
#bindsym Mod4+Shift+9 move container to workspace 9
#bindsym Mod4+Shift+0 move container to workspace 10
bindsym Mod4+a exec $control display focus left
bindsym Mod4+o exec $control display focus right
bindsym Mod4+comma exec $control workspace focus left
bindsym Mod4+period exec $control workspace focus right
bindsym Mod4+Shift+comma exec $control workspace container left
bindsym Mod4+Shift+period exec $control workspace container right
bindsym Mod4+w exec $control workspace create empty
bindsym Mod4+Shift+w exec $control workspace create focused
# reload the configuration file
bindsym Mod4+Shift+c reload
# restart i3 inplace (preserves your layout/session, can be used to upgrade i3)
bindsym Mod4+Shift+r restart
# exit i3 (logs you out of your X session)
bindsym Mod4+Shift+e exec "i3-nagbar -t warning -m 'You pressed the exit shortcut. Do you really want to exit i3? This will end your X session.' -b 'Yes, exit i3' 'i3-msg exit'"
# resize window (you can also use the mouse for that)
mode "resize" {
# These bindings trigger as soon as you enter the resize mode
# Pressing left will shrink the window’s width.
# Pressing right will grow the window’s width.
# Pressing up will shrink the window’s height.
# Pressing down will grow the window’s height.
bindsym h resize grow width 10 px or 10 ppt
bindsym l resize shrink width 10 px or 10 ppt
bindsym k resize shrink height 10 px or 10 ppt
bindsym j resize grow height 10 px or 10 ppt
# same bindings, but for the arrow keys
bindsym Left resize shrink width 10 px or 10 ppt
bindsym Down resize grow height 10 px or 10 ppt
bindsym Up resize shrink height 10 px or 10 ppt
bindsym Right resize grow width 10 px or 10 ppt
# back to normal: Enter or Escape
bindsym Return mode "default"
bindsym Escape mode "default"
}
bindsym Mod4+r mode "resize"
# Start i3bar to display a workspace bar (plus the system information i3status
# finds out, if available)
bar {
status_command i3status
}
#bar {
# i3bar_command i3bar
#
# status_command python /usr/lib64/python2.7/site-packages/powerline/bindings/i3/powerline-i3.py
# font pango:PowerlineFont 12
#}
#######################################################################
# automatically start i3-config-wizard to offer the user to create a
# keysym-based config which used his favorite modifier (alt or windows)
#
# i3-config-wizard will not launch if there already is a config file
# in ~/.i3/config.
#
# Please remove the following exec line:
#######################################################################
exec i3-config-wizard
exec --no-startup-id feh --bg-scale ~/.i3/cosmosbg.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment