rbtext 0.0.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a729d44ed2a3ab04b78d02b0d0226f3630ed47d789c8534f4f84244eb658395
4
- data.tar.gz: 68e66d49346a58ed12ff5fa7bd7c44c9b6ce5a21f647f08be7b46d72acd06e2e
3
+ metadata.gz: c0e5451aa3736ffcedad724b7bac7ab81d041b0fdd6689dda38fc3f700bb7429
4
+ data.tar.gz: 4795dd7dde116fa9ae34f5da44d55238ac18e829b56de491f652c55a327c5623
5
5
  SHA512:
6
- metadata.gz: 418da7b0d089e55f49fd9de6695899e4aec6f19c86860410d6fe4cc900c94c74658253eaec9f6fa9f13814060415cceaade928a9feae04467d881a01d4625ed4
7
- data.tar.gz: db96c6606483f074eaf77990d64e18017b1e5e0f5867cd90f470ae07bb28aada188843ea19999000b310ecf569f41e4fce76919d9640b13fbfb4b869b03d7d90
6
+ metadata.gz: 6ea9d2ea58d11bef5ce07ca67c364e22411be909b56c1637f643a77691b48e5bb5f4f0609670859e1f07f0b86bd9b4a298b7f7cd11f333e51f245f3480c2a37f
7
+ data.tar.gz: 7ea1e21507a58eeee386e0c30dd0af7c4473b0d4a45716455aa33cd8209f0a6bab0d246341ab1dcc1047e219303f57579f3f2629f2963fbd98926ef7d3aa40e8
data/bin/ftext ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/ruby
2
+ require_relative '../lib/rbtext.rb'
3
+
4
+ if $stdin.tty? && ARGV[0]
5
+ txt = ARGV[0].to_s
6
+ elsif $stdin.tty?
7
+ return
8
+ else
9
+ if ARGV[0]
10
+ txt = []
11
+
12
+ args = ARGV[0].split(',')
13
+ lines = STDIN.each_line.to_a
14
+
15
+ if args.length < lines.length
16
+ x = lines.length/args.length
17
+ args = args * (x+1)
18
+ end
19
+
20
+ (0..lines.length-1).each do |l|
21
+ txt << "#{"_.f:reset._" if ARGV[1] != "--noreset"}_.#{args[l]}._"+lines[l]
22
+ end
23
+
24
+ txt = txt.join
25
+ else
26
+ txt = ARGF.readlines.join
27
+ end
28
+ end
29
+
30
+ print R::Ft.new(txt)
data/lib/rbtext/colors.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  module RBText
2
2
  module Colors
3
+ @@methods = [
4
+ :fg_color_codes,
5
+ :bg_color_codes,
6
+ :color,
7
+ :num_color
8
+ ]
9
+
3
10
  def fg_color_codes
4
11
  {
5
12
  black: "30",
@@ -17,7 +24,8 @@ module RBText
17
24
  light_blue: "94",
18
25
  light_magenta: "95",
19
26
  light_cyan: "96",
20
- white: "97"
27
+ white: "97",
28
+ reset: "39"
21
29
  }
22
30
  end
23
31
 
@@ -31,21 +39,36 @@ module RBText
31
39
  return bg_color_codes
32
40
  end
33
41
 
34
- def color(color, type: :fg)
42
+ def color(color, type: :fg, mode: :str)
43
+ return "\033[39m\033[49m" if type == :all && color == :reset
44
+
35
45
  if type == :fg
36
46
  color_code = self.fg_color_codes[color.to_sym]
37
47
  elsif type == :bg
38
48
  color_code = self.bg_color_codes[color.to_sym]
49
+ else
50
+ return nil
39
51
  end
40
52
 
41
- return "\033[#{color_code}m"
53
+ return "\033[#{color_code}m" if mode == :str
54
+ print "\033[#{color_code}m" if mode == :set
55
+ end
56
+
57
+ def num_color(num, type: :fg, mode: :str)
58
+ return "\033[#{type == :fg ? "38" : "48"};5;#{num}m" if mode == :str
59
+ print "\033[#{type == :fg ? "38" : "48"};5;#{num}m" if mode == :set
42
60
  end
43
61
 
44
- module_function :fg_color_codes, :bg_color_codes, :color
62
+ @@methods.each do |method|
63
+ module_function method
64
+ end
45
65
  end
46
66
 
47
67
  module C
48
68
  include RBText::Colors
49
- module_function :fg_color_codes, :bg_color_codes, :color
69
+
70
+ @@methods.each do |method|
71
+ module_function method
72
+ end
50
73
  end
51
74
  end
data/lib/rbtext/cursor.rb CHANGED
@@ -1,5 +1,15 @@
1
1
  module RBText
2
2
  module Cursor
3
+ @@methods = [
4
+ :up,
5
+ :down,
6
+ :left,
7
+ :right,
8
+ :beginning_of_line,
9
+ :go_to_pos,
10
+ :pos
11
+ ]
12
+
3
13
  def up(num=1)
4
14
  print "\033[#{num.to_i}A"
5
15
  end
@@ -16,20 +26,42 @@ module RBText
16
26
  print "\033[#{num.to_i}C"
17
27
  end
18
28
 
19
- def go_to_pos(x, y)
29
+ def go_to_pos(x, y=nil)
30
+ if x.class == Array && !y
31
+ y = x[1]
32
+ x = x[0]
33
+ end
34
+
20
35
  print "\033[#{y};#{x}H"
21
36
  print "\033[#{y};#{x}f"
22
37
  end
23
38
 
39
+ def pos
40
+ res = ''
41
+ $stdin.raw do |stdin|
42
+ $stdout << "\e[6n"
43
+ $stdout.flush
44
+ while (c = stdin.getc) != 'R'
45
+ res << c if c
46
+ end
47
+ end
48
+ m = res.match /(?<row>\d+);(?<column>\d+)/
49
+ return [Integer(m[:column]), Integer(m[:row])]
50
+ end
51
+
24
52
  def beginning_of_line
25
53
  print "\r"
26
54
  end
27
55
 
28
- module_function :up, :down, :left, :right, :beginning_of_line, :go_to_pos
56
+ @@methods.each do |method|
57
+ module_function method
58
+ end
29
59
  end
30
60
 
31
61
  module Cr
32
62
  include RBText::Cursor
33
- module_function :up, :down, :left, :right, :beginning_of_line, :go_to_pos
63
+ @@methods.each do |method|
64
+ module_function method
65
+ end
34
66
  end
35
67
  end
@@ -1,30 +1,73 @@
1
1
  module RBText
2
2
  module Formatting
3
- def reset
4
- "\033[0m"
3
+ @@methods = [
4
+ :reset,
5
+ :bold,
6
+ :faint,
7
+ :italic,
8
+ :underline,
9
+ :blinking,
10
+ :strikethrough
11
+ ]
12
+
13
+ def reset(f_opt = nil, mode: :str)
14
+ if f_opt
15
+ r="\033[#{
16
+ f_opt =~ /^\033\[(1|2)m$/ ? "\033[22m" : "\033[2#{f_opt.gsub(/[^0-9]/, "")}m"
17
+ }"
18
+ else
19
+ r="\033[0m"
20
+ end
21
+ return r if mode == :str
22
+ print r if mode == :set
23
+ end
24
+
25
+ def bold(mode: :str)
26
+ r="\033[1m"
27
+ return r if mode == :str
28
+ print r if mode == :set
5
29
  end
6
30
 
7
- def bold
8
- "\033[1m"
31
+ def faint(mode: :str)
32
+ r="\033[2m"
33
+ return r if mode == :str
34
+ print r if mode == :set
9
35
  end
10
36
 
11
- def faint
12
- "\033[2m"
37
+ def italic(mode: :str)
38
+ r="\033[3m"
39
+ return r if mode == :str
40
+ print r if mode == :set
13
41
  end
14
42
 
15
- def italic
16
- "\033[3m"
43
+ def underline(mode: :str)
44
+ r="\033[4m"
45
+ return r if mode == :str
46
+ print r if mode == :set
17
47
  end
18
48
 
19
- def underline
20
- "\033[4m"
49
+ def blinking(mode: :str)
50
+ r="\033[5m"
51
+ return r if mode == :str
52
+ print r if mode == :set
21
53
  end
22
54
 
23
- module_function :reset, :bold, :faint, :italic, :underline
55
+ def strikethrough(mode: :str)
56
+ r="\033[9m"
57
+ return r if mode == :str
58
+ print r if mode == :set
59
+ end
60
+
61
+ module_function
62
+ @@methods.each do |method|
63
+ module_function method
64
+ end
24
65
  end
25
66
 
26
67
  module F
27
68
  include RBText::Formatting
28
- module_function :reset, :bold, :faint, :italic, :underline
69
+ @@methods.each do |method|
70
+ module_function method
71
+ end
29
72
  end
30
73
  end
data/lib/rbtext/ftext.rb CHANGED
@@ -15,6 +15,10 @@ module RBText
15
15
  @text << RBText::Colors.color(x[1].to_sym)
16
16
  elsif x[0] == "cb"
17
17
  @text << RBText::Colors.color(x[1].to_sym, type: :bg)
18
+ elsif x[0] == "cfn" || x[0] == "cn"
19
+ @text << RBText::Colors.num_color(x[1])
20
+ elsif x[0] == "cbn"
21
+ @text << RBText::Colors.num_color(x[1], type: :bg)
18
22
  elsif x[0] == "f"
19
23
  if x[1] == "reset"
20
24
  @text << RBText::Formatting.reset
@@ -26,6 +30,24 @@ module RBText
26
30
  @text << RBText::Formatting.italic
27
31
  elsif x[1] == "underline"
28
32
  @text << RBText::Formatting.underline
33
+ elsif x[1] == "strikethrough"
34
+ @text << RBText::Formatting.strikethrough
35
+ elsif x[1] == "blinking"
36
+ @text << RBText::Formatting.blinking
37
+ end
38
+ elsif x[0] == "rf"
39
+ if x[1] == "bold"
40
+ @text << RBText::Formatting.reset(R::F.bold)
41
+ elsif x[1] == "faint"
42
+ @text << RBText::Formatting.reset(R::F.faint)
43
+ elsif x[1] == "italic"
44
+ @text << RBText::Formatting.reset(R::F.italic)
45
+ elsif x[1] == "underline"
46
+ @text << RBText::Formatting.reset(R::F.underline)
47
+ elsif x[1] == "strikethrough"
48
+ @text << RBText::Formatting.reset(R::F.strikethrough)
49
+ elsif x[1] == "blinking"
50
+ @text << RBText::Formatting.reset(R::F.blinking)
29
51
  end
30
52
  end
31
53
  else
@@ -47,6 +69,14 @@ module RBText
47
69
  def original_text
48
70
  @original_text
49
71
  end
72
+
73
+ def +(str)
74
+ self.to_s + str
75
+ end
76
+
77
+ def [](num)
78
+ self.to_s[num]
79
+ end
50
80
  end
51
81
 
52
82
  class Ft < RBText::Ftext
data/lib/rbtext/screen.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  module RBText
2
2
  module Screen
3
+ @@methods = [
4
+ :clear,
5
+ :clear_line,
6
+ :size,
7
+ :height,
8
+ :width
9
+ ]
10
+
3
11
  def clear
4
12
  print "\033[2J"
5
13
  end
@@ -8,11 +16,27 @@ module RBText
8
16
  print "\033[2K"
9
17
  end
10
18
 
11
- module_function :clear, :clear_line
19
+ def size
20
+ IO.console.winsize
21
+ end
22
+
23
+ def height
24
+ self.size[0]
25
+ end
26
+
27
+ def width
28
+ self.size[1]
29
+ end
30
+
31
+ @@methods.each do |method|
32
+ module_function method
33
+ end
12
34
  end
13
35
 
14
36
  module S
15
37
  include RBText::Screen
16
- module_function :clear, :clear_line
38
+ @@methods.each do |method|
39
+ module_function method
40
+ end
17
41
  end
18
42
  end
data/lib/rbtext.rb CHANGED
@@ -3,14 +3,16 @@ require_relative "rbtext/colors.rb"
3
3
  require_relative "rbtext/formatting.rb"
4
4
  require_relative "rbtext/ftext.rb"
5
5
  require_relative "rbtext/screen.rb"
6
+ require "io/console"
6
7
 
7
8
  module RBText
8
9
  @ver_1 = 0
9
- @ver_2 = 0
10
- @ver_3 = 6
10
+ @ver_2 = 2
11
+ @ver_3 = 0
12
+ @ver_4 = ""
11
13
 
12
14
  def version
13
- "#{@ver_1}.#{@ver_2}.#{@ver_3}"
15
+ "#{@ver_1}.#{@ver_2}.#{@ver_3}#{".#{@ver_4}" if @ver_4.length > 1}"
14
16
  end
15
17
 
16
18
  module_function :version
metadata CHANGED
@@ -1,21 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbtext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-19 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: io
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
13
27
  description: A gem for printing formatted text
14
28
  email: matthias@matthiasclee.com
15
- executables: []
29
+ executables:
30
+ - ftext
16
31
  extensions: []
17
32
  extra_rdoc_files: []
18
33
  files:
34
+ - bin/ftext
19
35
  - lib/rbtext.rb
20
36
  - lib/rbtext/colors.rb
21
37
  - lib/rbtext/cursor.rb