ansiterm 0.3.3 → 0.4

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: 594990b07d22ef5777b36348387c47a3061c4772f5e830a2240294c77574b3ea
4
- data.tar.gz: 772aa91059ffa9d4e59bd06b33fd607726befacd0ecfafa61db3a5df862897a9
3
+ metadata.gz: 92d53d94db9dcd9ed7d7369d4b7df040c5da231bfb2985399b783716f12c7fa0
4
+ data.tar.gz: 7a50dcdbadfe4cf828de6b98ac674f36052525e0fc5c390a4d018167ccffb4cb
5
5
  SHA512:
6
- metadata.gz: d3e0fba3540ea67e4126c9ccc6918b778d7fffd971f407c3d4045148dda5b5edf2ef00761ec2465eea55e1e930da15e8a111e62a214753cbfd8a2c6d77fd753b
7
- data.tar.gz: 59a9fa226a211ce692a446c7e895631bc19c30e729d3597a7f5f6f97e1de267ac16e4593fe363f460d4a37e3c02bbfb01112772d1cd7489e05cc6ae0406d471c
6
+ metadata.gz: a0c1b315496296dd0a9c6015c5907a0d08eb6d4c4a17756d8ce22f4929b6ff571e448bf9b83a902949be6b625706e9d796d9c59d742f90fb5b0f7b2ffec3c1a9
7
+ data.tar.gz: c595e0ca1516a21f033317f997f31981bc6899263ce8407065244d76f8bb41bfe339a5435df048be64a149da36e692bcfc04e695e95cc9664e379a5ed8e7e4e6
data/ansiterm.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "bundler"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  end
data/lib/ansiterm/attr.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  module AnsiTerm
3
2
 
4
3
  # # Attr #
@@ -25,10 +24,10 @@ module AnsiTerm
25
24
 
26
25
  attr_reader :fgcol, :bgcol, :flags
27
26
 
28
- def initialize(fgcol: nil, bgcol: nil, flags: 0)
27
+ def initialize(fgcol: nil, bgcol: nil, flags: nil)
29
28
  @fgcol = fgcol
30
29
  @bgcol = bgcol
31
- @flags = flags || 0
30
+ @flags = flags
32
31
  freeze
33
32
  end
34
33
 
@@ -39,20 +38,28 @@ module AnsiTerm
39
38
  flags == other.flags
40
39
  end
41
40
 
42
- def merge(attrs)
41
+ def to_h
42
+ attrs = {}
43
+ attrs[:bgcol] = @bgcol if @bgcol
44
+ attrs[:fgcol] = @fgcol if @fgcol
45
+ attrs[:flags] = @flags if @flags
46
+ attrs
47
+ end
48
+
49
+ def merge(attrs, ignore: nil)
43
50
  return self if self == attrs
44
- if attrs.kind_of?(self.class)
45
- old = attrs
46
- attrs = {}
47
- attrs[:bgcol] = old.bgcol if old.bgcol
48
- attrs[:fgcol] = old.fgcol if old.fgcol
49
- attrs[:flags] = old.flags if old.flags
51
+ if attrs.respond_to?(:to_h)
52
+ attrs = attrs.to_h
50
53
  end
51
- self.class.new({bgcol: @bgcol, fgcol: @fgcol, flags: @flags}.merge(attrs))
52
- end
54
+ if ignore
55
+ attrs.delete(ignore)
56
+ end
57
+ attrs = to_h.merge(attrs)
58
+ self.class.new(attrs)
59
+ end
53
60
 
54
- def add_flag(flags); merge({flags: @flags | flags}); end
55
- def clear_flag(flags); merge({flags: @flags & ~flags}); end
61
+ def add_flag(flags); merge({flags: @flags.to_i | flags.to_i}); end
62
+ def clear_flag(flags); merge({flags: @flags.to_i & ~flags.to_i}); end
56
63
 
57
64
  def reset; self.class.new; end
58
65
  def normal; clear_flag(BOLD); end
@@ -60,14 +67,14 @@ module AnsiTerm
60
67
  def underline; add_flag(UNDERLINE); end
61
68
  def crossed_out; add_flag(CROSSED_OUT); end
62
69
 
63
- def bold?; (@flags & BOLD) != 0; end
64
- def underline?; (@flags & UNDERLINE) != 0; end
65
- def crossed_out?; (@flags & CROSSED_OUT) != 0; end
70
+ def bold?; (@flags.to_i & BOLD) != 0; end
71
+ def underline?; (@flags.to_i & UNDERLINE) != 0; end
72
+ def crossed_out?; (@flags.to_i & CROSSED_OUT) != 0; end
66
73
 
67
74
  def normal?
68
- @flags == NORMAL &&
69
- @fgcol.nil? &&
70
- @bgcol.nil?
75
+ (@flags == NORMAL || @flags.nil?) &&
76
+ (@fgcol.nil? || @fgcol == 39) &&
77
+ (@bgcol.nil? || @bgcol == 49)
71
78
  end
72
79
 
73
80
  def transition_to(other)
@@ -92,7 +99,7 @@ module AnsiTerm
92
99
  if t.empty?
93
100
  ""
94
101
  else
95
- "\e[#{t.join(";")}m"
102
+ "\e[#{t.flatten.join(";")}m"
96
103
  end
97
104
  end
98
105
  end
@@ -0,0 +1,108 @@
1
+
2
+ module AnsiTerm
3
+
4
+ # # AnsiTerm::Buffer #
5
+ #
6
+ # A terminal buffer that will eventually handle ANSI style strings
7
+ # fully. For now it will handle the sequences AnsiTerm::String handles
8
+ # but e.g. cursor movement etc. needs to be explicitly handled.
9
+ #
10
+ # Extracted out of https://github.com/vidarh/re
11
+ #
12
+ # FIXME: Provide method of setting default background color
13
+ #
14
+ class Buffer
15
+ attr_reader :w,:h, :lines
16
+
17
+ def initialize(w=80, h=25)
18
+ @lines = []
19
+ @x = 0
20
+ @y = 0
21
+ @w = w
22
+ @h = h
23
+ @cache = []
24
+ end
25
+
26
+ def cls
27
+ @lines = (1..@h).map { nil } #AnsiTerm::String.new } #AnsiTerm::String.new("\e[37;40;0m"+(" "*@w)) }
28
+ end
29
+
30
+ def reset
31
+ cls
32
+ @cache=[]
33
+ end
34
+
35
+ def move_cursor(x,y)
36
+ @x = x
37
+ @y = y
38
+ @x = @w-1 if @x >= @w
39
+ @y = @y-1 if @y >= @h
40
+ end
41
+
42
+ def resize(w,h)
43
+ if @w != w || @h != h
44
+ @w, @h = w,h
45
+ @cache = []
46
+ end
47
+ end
48
+
49
+ def scroll
50
+ while @y >= @h
51
+ @lines.shift
52
+ @lines << nil #AnsiTerm::String.new #"" #AnsiTerm::String.new("\e[37;40;0m"+(" "*@w))
53
+ @y -= 1
54
+ end
55
+ true
56
+ end
57
+
58
+ def print *args
59
+ args.each do |str|
60
+ @lines[@y] ||= AnsiTerm::String.new("\e[0m")
61
+ @dirty << @y
62
+ l = @lines[@y]
63
+
64
+ if l.length < @x
65
+ l << (" "*(@x - l.length))
66
+ end
67
+ l[@x..@x+str.length] = str
68
+ # l[@x] << str
69
+ # if @x + s.length > @w
70
+ # l[@x .. @w-1] = s[0 .. @w - @x]
71
+ # @x = 0
72
+ # @y += 1
73
+ # scroll if @y >= @h
74
+ # end
75
+ end
76
+ end
77
+
78
+ def to_s
79
+ out = ""
80
+ cachehit=0
81
+ cachemiss=0
82
+ @lines.each_with_index do |line,y|
83
+ line ||= ""
84
+ line = line[0..(@w-1)]
85
+ l = line.length
86
+ #if l > @w
87
+ # $editor&.log("WARNING: #{l} > @w #{@w}")
88
+ #end
89
+ s = line.to_str
90
+ if @cache[y] != s
91
+ out << ANSI.cup(y,0) << s << ANSI.sgr(:reset) << ANSI.el #<< X\n"
92
+ cachemiss += s.length
93
+ old = @cache[y]
94
+ @cache[y] = s
95
+ #$editor.pry([y,old, s])
96
+ else
97
+ cachehit += @cache[y].length
98
+ end
99
+ # FIXME: This is only worthwhile if a background colour is set
100
+ #if l < @w
101
+ # out << ANSI.csi("m",0,38,48,2,48,48,64) << "-"*(@w-l)
102
+ #end
103
+ end
104
+ @dirty = Set[]
105
+ out
106
+ end
107
+ end
108
+ end
@@ -56,16 +56,19 @@ module AnsiTerm
56
56
  def set_attr(range, attr)
57
57
  min = [range.first - 1,0].max
58
58
  fattr = @attrs[min]
59
- attr = fattr.merge(attr)
59
+ #attr = fattr.merge(attr)
60
60
  r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually"
61
61
  last = nil
62
- @attrs[range] = @attrs[range].map do |a|
63
- n = a.merge(attr)
62
+ @attrs[range] = @attrs[range]&.map do |a|
63
+ n = a ? a.merge(attr) : attr
64
64
  last == n ? last : n
65
- end
66
- rm = range.last
65
+ end || []
66
+ rm = range.last+1
67
67
  if a = @attrs[rm]
68
- @attrs[rm] = Attr.new(bgcol:40).merge(fattr).merge(a)
68
+ @attrs[rm] = Attr.new.merge(fattr).merge(a)
69
+ if !a.bgcol
70
+ @attrs[rm] = @attrs[rm].merge({bgcol: 49})
71
+ end
69
72
  end
70
73
  end
71
74
 
@@ -76,10 +79,11 @@ module AnsiTerm
76
79
  last = nil
77
80
  @attrs[range] = @attrs[range].map do |a|
78
81
  if a.bgcol == 49
79
- a.merge(attr)
82
+ n = attr.merge(a, ignore: :bgcol)
80
83
  else
81
- attr.merge(a)
84
+ n = attr.merge(a)
82
85
  end
86
+ last == n ? last : n
83
87
  end
84
88
  #fattr #@attrs[min+r].merge(fattr)
85
89
  end
@@ -91,7 +95,7 @@ module AnsiTerm
91
95
  @str = s[0..(range.min-1)].to_s + @str + s[(range.max)..-1].to_s
92
96
  @attrs = a[0..(range.min-1)].to_a + @attrs + a[(range.max)..-1].to_a
93
97
  end
94
-
98
+
95
99
  def[] i
96
100
  str = @str[i]
97
101
  if str
@@ -106,7 +110,7 @@ module AnsiTerm
106
110
  def char_at(index)
107
111
  @str[index]
108
112
  end
109
-
113
+
110
114
  def attr_at(index)
111
115
  @attrs[index]
112
116
  end
@@ -133,13 +137,13 @@ module AnsiTerm
133
137
  if par == "5"
134
138
  col = [col,5,params.shift].join(";")
135
139
  elsif par == "2"
136
- col = ([col,2] << params.slice!(0..2)).join(";")
140
+ col = [col,2,*params.slice!(0..2)].join(";")
137
141
  # ,params.shift,params.shift, params.shift].join(";")
138
142
  end
139
143
  end
140
144
  a.merge(attr_name => col)
141
145
  end
142
-
146
+
143
147
  def parse(str)
144
148
  @str = ""
145
149
  @attrs = []
@@ -147,7 +151,7 @@ module AnsiTerm
147
151
 
148
152
  max = str.length
149
153
  i = 0
150
-
154
+
151
155
  while i < max
152
156
  c = str[i]
153
157
  if c == "\e" && str[i+1] == "[" # CSI
@@ -1,3 +1,3 @@
1
1
  module AnsiTerm
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4"
3
3
  end
data/lib/ansiterm.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require "ansiterm/version"
2
2
  require "ansiterm/attr"
3
3
  require "ansiterm/string"
4
-
4
+ require "ansiterm/buffer"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansiterm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vidar Hokstad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-05 00:00:00.000000000 Z
11
+ date: 2021-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +70,7 @@ files:
70
70
  - bin/setup
71
71
  - lib/ansiterm.rb
72
72
  - lib/ansiterm/attr.rb
73
+ - lib/ansiterm/buffer.rb
73
74
  - lib/ansiterm/string.rb
74
75
  - lib/ansiterm/version.rb
75
76
  homepage: https://github.com/vidarh/ansiterm