Class: Terminal
- Inherits:
-
Object
- Object
- Terminal
- Defined in:
- lib/terminal/terminal.rb
Constant Summary collapse
- FORMATTING =
{ reset:"0", reset_bold:"21", reset_muted:"22", reset_italic:"22", reset_underline:"24", reset_blink:"25", reset_inverted:"27", reset_hidden:"28", black_background: "40", dark_gray_background: "40", red_background: "41", light_red_background: "41", green_background: "42", light_green_background: "42", brown_orange_background: "43", yellow_background: "43", blue_background: "44", light_blue_background: "44", purple_background: "45", light_purple_background: "45", cyan_background: "46", light_cyan_background: "46", light_gray_background: "47", white_background: "47", bold: "1", muted: "2", italic: "3", underline: "4", blink: "5", inverted: "7", hidden: "8", font1: "11", font2: "12", font3: "13", font4: "14", font5: "15", black: "30", dark_gray: "30", red: "31", light_red: "31", green: "32", light_green: "32", brown_orange: "33", yellow: "33", blue: "34", light_blue: "34", purple: "35", light_purple: "35", cyan: "36", light_cyan: "36", light_gray: "37", framed: "51", encircled: "52", }
Instance Method Summary collapse
- #add_formatting(text, name = :reset) ⇒ Object
- #build_formatting(name = :reset) ⇒ Object
- #clear_clipboard ⇒ Object
- #clear_terminal ⇒ Object
- #from_clipboard ⇒ Object
- #has_app?(app_name) ⇒ Boolean
- #height(line, col) ⇒ Object
-
#initialize(**args) ⇒ Terminal
constructor
A new instance of Terminal.
- #move_cursor_to(line, col) ⇒ Object
- #rbg(**args) ⇒ Object
- #to_clipboard(clip_string, capture = false) ⇒ Object
- #width(line, col) ⇒ Object
Constructor Details
#initialize(**args) ⇒ Terminal
Returns a new instance of Terminal.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/terminal/terminal.rb', line 57 def initialize(**args) if(args[:color]) @color = args[:color] else @color = :reset end if(args[:background]) @background = args[:background] else @background = :reset_background end end |
Instance Method Details
#add_formatting(text, name = :reset) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/terminal/terminal.rb', line 143 def add_formatting(text,name=:reset) if(!name.is_a?(Array)) name = [name] end name.map do |n| if(name.to_s.match(/_(\d*)_/)) # ex: "_2_" for color 2. "\e[38;5;#{name.to_s.match(/_(\d*)_/)[1]}m#{text}#{build_formatting(:reset)}" else if(FORMATTING.keys .select{|k| k.to_s.include?("reset_")}.map{|k| k.to_s.split('_')[1]} .include?(name.to_s)) reset_formatting = build_formatting("reset_#{name.to_s}".to_sym) else reset_formatting = build_formatting(:reset) end text = "#{build_formatting(name)}#{text}#{reset_formatting}" end end return text end |
#build_formatting(name = :reset) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/terminal/terminal.rb', line 112 def build_formatting(name=:reset) if(!name.is_a? Array) name = [name] end return("\e[#{name.map{|n| Terminal::FORMATTING[n] }.join(';')}m") end |
#clear_clipboard ⇒ Object
98 99 100 101 102 |
# File 'lib/terminal/terminal.rb', line 98 def clear_clipboard if(has_app?("xsel")) return `xsel -c` end end |
#clear_terminal ⇒ Object
104 105 106 |
# File 'lib/terminal/terminal.rb', line 104 def clear_terminal puts "\e[H\e[2J\e[3J" end |
#from_clipboard ⇒ Object
92 93 94 95 96 |
# File 'lib/terminal/terminal.rb', line 92 def from_clipboard if(has_app?("xsel")) return `xsel -o` end end |
#has_app?(app_name) ⇒ Boolean
108 109 110 |
# File 'lib/terminal/terminal.rb', line 108 def has_app?(app_name) return(`which #{app_name}` != "") end |
#height(line, col) ⇒ Object
74 75 76 |
# File 'lib/terminal/terminal.rb', line 74 def height(line,col) `tput lines` end |
#move_cursor_to(line, col) ⇒ Object
78 79 80 |
# File 'lib/terminal/terminal.rb', line 78 def move_cursor_to(line,col) puts "\e[#{line};#{col}H" end |
#rbg(**args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/terminal/terminal.rb', line 119 def rbg(**args) if(!!args[:red]) @red = args[:red] elsif(!!args[:r]) @red = args[:r] end if(!!args[:blue]) @blue = args[:blue] elsif(!!args[:b]) @blue = args[:b] end if(!!args[:green]) @green = args[:green] elsif(!!args[:g]) @green = args[:g] end color_code = 16+(@red*36)+(@blue*1)+(@green*6) if(!!args[:text]) return "\e[38;5;#{color_code}m#{args[:text]}#{build_formatting(:reset)}" else return color_code end end |
#to_clipboard(clip_string, capture = false) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/terminal/terminal.rb', line 82 def to_clipboard(clip_string,capture=false) if(has_app?("xsel")) if(!!capture) return `#{clip_string} | xsel -i` else return `echo #{clip_string} | xsel -i` end end end |
#width(line, col) ⇒ Object
70 71 72 |
# File 'lib/terminal/terminal.rb', line 70 def width(line,col) `tput cols` end |