christmas_tree 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +36 -0
- data/Rakefile +4 -0
- data/christmas_tree.gemspec +38 -0
- data/christmas_tree.rb +0 -0
- data/demo.png +0 -0
- data/exe/christmas_tree +18 -0
- data/lib/christmas_tree/version.rb +5 -0
- data/lib/christmas_tree.rb +184 -0
- data/sig/christmas_tree.rbs +4 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6cb268f381e6f5ba32946a2d8a4e2cb08143c6a14fe0da10712657d822571915
|
4
|
+
data.tar.gz: '086a429650087299acd5b3057b169699f9018399de7d0b4b0bd1221344626efb'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9998e5cbfdfacd6b244ee4aa264b1bbf84ec38948991152718fe202bf81b900c05a3234a31a9d7f48dae88396d8dfb97163be5b3a3383c35e64c950972d7f505
|
7
|
+
data.tar.gz: 1c9861ea92dbd39530818009ebd7eff9b9af33150de7a6e6e31a9ad72ab63b03aee13230a28d4305539000e334231ebd0f2acba165cfcce8ae5a06452e0612a4
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Mark24
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# ChristmasTree
|
2
|
+
|
3
|
+
christmas tree cli
|
4
|
+
|
5
|
+
# dependency
|
6
|
+
|
7
|
+
System dependencies:
|
8
|
+
|
9
|
+
- Ruby 3.0+
|
10
|
+
- Ncurses: macos: `brew install ncurses`
|
11
|
+
|
12
|
+
Ruby Gem:
|
13
|
+
|
14
|
+
- Curses: `gem install curses``
|
15
|
+
|
16
|
+
# local run
|
17
|
+
|
18
|
+
`ruby christmas_tree.rb`
|
19
|
+
|
20
|
+
`./christmas_tree.rb`
|
21
|
+
|
22
|
+
## add your name
|
23
|
+
|
24
|
+
`./christmas_tree.rb --name <your name>`
|
25
|
+
|
26
|
+
# remote run
|
27
|
+
|
28
|
+
`ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Mark24Code/christmas_tree/main/christmas_tree.rb)"`
|
29
|
+
|
30
|
+
## add your name
|
31
|
+
|
32
|
+
`ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Mark24Code/christmas_tree/main/christmas_tree.rb)" -- -n <your name>`
|
33
|
+
|
34
|
+
# preview
|
35
|
+
|
36
|
+

|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/christmas_tree/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "christmas_tree"
|
7
|
+
spec.version = ChristmasTree::VERSION
|
8
|
+
spec.authors = ["Mark24Code"]
|
9
|
+
spec.email = ["mark.zhangyoung@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Christmas tree cli "
|
12
|
+
spec.description = "Christmas tree cli "
|
13
|
+
spec.homepage = "https://github.com/Mark24Code/christmas_tree"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/Mark24Code/christmas_tree"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/Mark24Code/christmas_tree"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
26
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
spec.add_dependency "curses", "~> 1.4.4"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
data/christmas_tree.rb
ADDED
File without changes
|
data/demo.png
ADDED
Binary file
|
data/exe/christmas_tree
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'curses'
|
3
|
+
require_relative '../lib/christmas_tree'
|
4
|
+
|
5
|
+
begin
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: example.rb [options]"
|
9
|
+
|
10
|
+
opts.on("-nNAME", "--name=NAME", "name") do |name|
|
11
|
+
options[:name] = name
|
12
|
+
end
|
13
|
+
end.parse!
|
14
|
+
|
15
|
+
ChristmasTree::Tree.new(options[:name]).draw
|
16
|
+
ensure
|
17
|
+
Curses.close_screen
|
18
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "christmas_tree/version"
|
4
|
+
|
5
|
+
require 'optparse'
|
6
|
+
require 'curses'
|
7
|
+
|
8
|
+
|
9
|
+
Curses.init_screen
|
10
|
+
Curses.noecho
|
11
|
+
Curses.curs_set(0)
|
12
|
+
|
13
|
+
COLORS_ORDER = [
|
14
|
+
:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white,
|
15
|
+
:bright_black, :bright_red, :bright_green, :bright_yellow,
|
16
|
+
:bright_blue, :bright_magenta, :bright_cyan, :bright_white
|
17
|
+
]
|
18
|
+
|
19
|
+
COLORS_MAP = {}
|
20
|
+
Curses.start_color
|
21
|
+
COLORS_ORDER.length.times { |i|
|
22
|
+
Curses.init_pair(i, i, 0)
|
23
|
+
COLORS_MAP[COLORS_ORDER[i]] = Curses.color_pair(i)
|
24
|
+
}
|
25
|
+
|
26
|
+
FONT_WEIGHT_MAP = {
|
27
|
+
normal: Curses::A_NORMAL,
|
28
|
+
bold: Curses::A_BOLD,
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
module ChristmasTree
|
33
|
+
class Token
|
34
|
+
attr_accessor :content, :font_weight, :color_pair
|
35
|
+
def initialize(content, color_pair = nil, font_weight = nil)
|
36
|
+
@content = content
|
37
|
+
@font_weight = font_weight || :normal
|
38
|
+
@color_pair = color_pair || :white
|
39
|
+
end
|
40
|
+
|
41
|
+
def length
|
42
|
+
@content.length
|
43
|
+
end
|
44
|
+
|
45
|
+
def draw(&block)
|
46
|
+
Curses.attron(COLORS_MAP[@color_pair] | FONT_WEIGHT_MAP[@font_weight] ){
|
47
|
+
yield @content
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Error < StandardError; end
|
53
|
+
|
54
|
+
class Tree
|
55
|
+
CHAR_SPACE = ' '
|
56
|
+
def initialize(name = nil, layer_count = 10)
|
57
|
+
@name = name
|
58
|
+
@buffer_count = layer_count
|
59
|
+
@scale = 3
|
60
|
+
@buffer = []
|
61
|
+
@light_char = 'o'
|
62
|
+
@snow_char = '*'
|
63
|
+
@light_colors = COLORS_ORDER
|
64
|
+
@speed = 3 # 3 times per second
|
65
|
+
@width = nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def tree_crown
|
69
|
+
(1..@buffer_count).each do |count|
|
70
|
+
total = 2*count-1
|
71
|
+
index_arr = []
|
72
|
+
line = []
|
73
|
+
|
74
|
+
total.times.each do |i|
|
75
|
+
index_arr.push(i)
|
76
|
+
line << Token.new("*", :green)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# snow
|
82
|
+
snow_random_count = (total * 0.3).to_i
|
83
|
+
snow_random_count.times {
|
84
|
+
choose_index = index_arr.sample
|
85
|
+
line[choose_index] = Token.new(@snow_char, :white)
|
86
|
+
}
|
87
|
+
|
88
|
+
# light
|
89
|
+
light_random_count = (total * 0.3).to_i
|
90
|
+
light_random_count.times {
|
91
|
+
choose_index = index_arr.sample
|
92
|
+
line[choose_index] = Token.new(@light_char, @light_colors.sample)
|
93
|
+
}
|
94
|
+
@buffer << line
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def tree_trunk(height=2)
|
99
|
+
height.times {
|
100
|
+
@buffer << [Token.new("mWm", :white)]
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
def color_tokens(text)
|
106
|
+
text.split("").map {|c| Token.new(c, @light_colors.sample, :bold)}
|
107
|
+
end
|
108
|
+
|
109
|
+
def footer_text
|
110
|
+
@buffer << [Token.new("MARRY CHRISTMAS", :yellow)]
|
111
|
+
|
112
|
+
# name
|
113
|
+
if @name
|
114
|
+
name = "@"+@name.strip
|
115
|
+
line = []
|
116
|
+
color_tokens(name).each do |code|
|
117
|
+
line << code
|
118
|
+
end
|
119
|
+
|
120
|
+
@buffer << line
|
121
|
+
end
|
122
|
+
|
123
|
+
# code
|
124
|
+
line = [Token.new("And lots of ", :yellow)]
|
125
|
+
color_tokens("CODE").each do |code|
|
126
|
+
line << code
|
127
|
+
end
|
128
|
+
line << Token.new(" in #{Time.now.year + 1}", :yellow)
|
129
|
+
@buffer << line
|
130
|
+
end
|
131
|
+
|
132
|
+
def border(height=4)
|
133
|
+
height.times {
|
134
|
+
@buffer << [Token.new(CHAR_SPACE)]
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
def draw_canvas
|
139
|
+
@buffer = []
|
140
|
+
border(3)
|
141
|
+
tree_crown
|
142
|
+
tree_trunk
|
143
|
+
footer_text
|
144
|
+
border(3)
|
145
|
+
|
146
|
+
if !@width
|
147
|
+
@width = (@buffer.map {|l| real_width(l)}).max * @scale
|
148
|
+
end
|
149
|
+
|
150
|
+
@buffer.each_with_index do |buffer_line,y|
|
151
|
+
x = (@width - real_width(buffer_line)) / 2
|
152
|
+
Curses.setpos(y, x)
|
153
|
+
buffer_line.each do |token|
|
154
|
+
token.draw do |text|
|
155
|
+
Curses.addstr(text)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def line_process(line)
|
162
|
+
half_space = CHAR_SPACE * ((@width - real_width(line)) / 2)
|
163
|
+
"#{half_space}#{line}#{half_space}"
|
164
|
+
end
|
165
|
+
|
166
|
+
def real_width(line)
|
167
|
+
line.map {|token| token.length }.sum
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
def clean_screen
|
172
|
+
Curses.clear
|
173
|
+
end
|
174
|
+
|
175
|
+
def draw
|
176
|
+
clean_screen
|
177
|
+
while true
|
178
|
+
draw_canvas
|
179
|
+
Curses.refresh
|
180
|
+
sleep 1.0/@speed
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: christmas_tree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark24Code
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: curses
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.4
|
27
|
+
description: 'Christmas tree cli '
|
28
|
+
email:
|
29
|
+
- mark.zhangyoung@gmail.com
|
30
|
+
executables:
|
31
|
+
- christmas_tree
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- christmas_tree.gemspec
|
39
|
+
- christmas_tree.rb
|
40
|
+
- demo.png
|
41
|
+
- exe/christmas_tree
|
42
|
+
- lib/christmas_tree.rb
|
43
|
+
- lib/christmas_tree/version.rb
|
44
|
+
- sig/christmas_tree.rbs
|
45
|
+
homepage: https://github.com/Mark24Code/christmas_tree
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/Mark24Code/christmas_tree
|
50
|
+
source_code_uri: https://github.com/Mark24Code/christmas_tree
|
51
|
+
changelog_uri: https://github.com/Mark24Code/christmas_tree
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.6.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.4.22
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Christmas tree cli
|
71
|
+
test_files: []
|