sortviz 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sortviz/canvas.rb +4 -4
- data/lib/sortviz/cursor.rb +12 -24
- data/lib/sortviz/plugins.rb +6 -5
- data/lib/sortviz/version.rb +1 -1
- data/lib/sortviz/visualizer.rb +13 -12
- data/sortviz.gemspec +3 -3
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ab0c658aff4dc772ecffcacb46cf611eabb3461
|
4
|
+
data.tar.gz: 22438a128feac8deb9c70aee7c33d580d5faed1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 142a64eec59079b2e6e8af322a593226ebdbd04163b69f6f964b2bbe856db9a043626ea61e3d7761533afa833b1c300509983275f78416ad37a2ae98054fe880
|
7
|
+
data.tar.gz: 76f9fed39164fbad24c24cef1891095e581eb551b8c967dfff3ed7b5973c8b561912525f3836da19a65b0fe0fd5499967992a27403a71ae37205be3ffef419c7
|
data/lib/sortviz/canvas.rb
CHANGED
@@ -9,8 +9,8 @@ module Sortviz
|
|
9
9
|
attr_reader :window
|
10
10
|
|
11
11
|
# Initializes a new instance with a title to display, the current cursor
|
12
|
-
# object (
|
13
|
-
# parent window/screen (standard screen created in
|
12
|
+
# object (<tt>Sortviz::Cursor</tt>) and the modified screen dimensions from the
|
13
|
+
# parent window/screen (standard screen created in <tt>Sortviz::Visualizer</tt>)
|
14
14
|
def initialize(title, cursor, screen_dim)
|
15
15
|
@screen_dim = screen_dim
|
16
16
|
@cursor = cursor
|
@@ -19,7 +19,7 @@ module Sortviz
|
|
19
19
|
end
|
20
20
|
|
21
21
|
# Does the initial setup of creating an actual curses window, adding a
|
22
|
-
# border to it and setting up non-blocking
|
22
|
+
# border to it and setting up non-blocking <tt>Curses::Window#getch</tt>
|
23
23
|
def setup
|
24
24
|
@window ||= Curses::Window.new(
|
25
25
|
@screen_dim[:lines] - MARGIN,
|
@@ -55,7 +55,7 @@ module Sortviz
|
|
55
55
|
end
|
56
56
|
|
57
57
|
private
|
58
|
-
|
58
|
+
|
59
59
|
def draw_bar(height, highlighted)
|
60
60
|
attr = highlighted ? @red_highlight : Curses::A_REVERSE
|
61
61
|
attron(attr)
|
data/lib/sortviz/cursor.rb
CHANGED
@@ -8,68 +8,52 @@ module Sortviz
|
|
8
8
|
attr_reader :y, :x, :window
|
9
9
|
# Initialize a new Cursor object, follows curses practice of passing
|
10
10
|
# the y before the x coords.
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# @return instance of Sortviz::Cursor
|
11
|
+
#
|
12
|
+
# Params:
|
13
|
+
# +window+:: <tt>Curses::Window</tt> The window that will receive input
|
14
|
+
# +origin+:: <tt>Hash</tt> Origins to start drawing at
|
16
15
|
def initialize(window, origin)
|
17
16
|
@window = window
|
18
17
|
move(origin[:y], origin[:x])
|
19
18
|
end
|
20
19
|
|
21
20
|
# Move to positions y, x
|
22
|
-
# @params
|
23
|
-
# => y (Int)
|
24
|
-
# => x (Int)
|
25
21
|
def move(y, x)
|
26
22
|
update(y, x)
|
27
23
|
apply_pos
|
28
24
|
end
|
29
25
|
|
30
26
|
# Move along the y-axis
|
31
|
-
# @params
|
32
|
-
# => val (Int) new Value of y
|
33
27
|
def move_y(val)
|
34
28
|
update(val, x)
|
35
29
|
apply_pos
|
36
30
|
end
|
37
31
|
|
38
32
|
# Move along the x-axis
|
39
|
-
# @params
|
40
|
-
# => val (Int) new Value of x
|
41
33
|
def move_x(val)
|
42
34
|
update(y, val)
|
43
35
|
apply_pos
|
44
36
|
end
|
45
37
|
|
46
|
-
# Increment the y value by val
|
47
|
-
# @params
|
48
|
-
# => val (Int) new Value of y, Default: 1
|
38
|
+
# Increment the y value by val (Default: 1)
|
49
39
|
def incr_y(val = 1)
|
50
40
|
update(y + val, x)
|
51
41
|
apply_pos
|
52
42
|
end
|
53
43
|
|
54
|
-
# Decrement the y value by val
|
55
|
-
# @params
|
56
|
-
# => val (Int) new Value of y, Default: 1
|
44
|
+
# Decrement the y value by val (Default: 1)
|
57
45
|
def decr_y(val = 1)
|
58
46
|
update((y - val).abs, x)
|
59
47
|
apply_pos
|
60
48
|
end
|
61
49
|
|
62
|
-
# Increment the x value by val
|
63
|
-
# @params
|
64
|
-
# => val (Int) new Value of x, Default: 1
|
50
|
+
# Increment the x value by val (Default: 1)
|
65
51
|
def incr_x(val = 1)
|
66
52
|
update(y, x + val)
|
67
53
|
apply_pos
|
68
54
|
end
|
69
55
|
|
70
|
-
# Decrement the x value by val
|
71
|
-
# @params
|
72
|
-
# => val (Int) new Value of x, Default: 1
|
56
|
+
# Decrement the x value by val (Default: 1)
|
73
57
|
def decr_x(val = 1)
|
74
58
|
update(y, (x - val).abs)
|
75
59
|
apply_pos
|
@@ -87,15 +71,19 @@ module Sortviz
|
|
87
71
|
@cached = nil
|
88
72
|
end
|
89
73
|
|
74
|
+
# Switch to a new window that will receive input and optionally move to
|
75
|
+
# new coordinates in the new window
|
90
76
|
def switch_window(new_window, coords: {})
|
91
77
|
@window = new_window
|
92
78
|
move(coords[:y], coords[:x]) unless coords.empty?
|
93
79
|
end
|
94
80
|
|
81
|
+
# Print to the current screen receiving input
|
95
82
|
def tprint(string)
|
96
83
|
@window.addstr(string)
|
97
84
|
end
|
98
85
|
|
86
|
+
# Increment y to simulate the addition of a new line
|
99
87
|
def newline
|
100
88
|
incr_y
|
101
89
|
end
|
data/lib/sortviz/plugins.rb
CHANGED
@@ -3,15 +3,16 @@ module Sortviz
|
|
3
3
|
ALGORITHMS = {}
|
4
4
|
|
5
5
|
# Defines a new algorithm for sorting by adding some meta data to
|
6
|
-
#
|
7
|
-
# Module function of
|
6
|
+
# <tt>Sortviz::ALGORITHMS</tt> and turning the sorting algorithm method into a
|
7
|
+
# Module function of <tt>Sortviz</tt>.
|
8
8
|
#
|
9
9
|
# It's a bad design, should be fixed soon with a proper plugin system as this
|
10
10
|
# way pollutes the Sortviz namespace.
|
11
|
+
#
|
11
12
|
# Params:
|
12
|
-
# +name+::
|
13
|
-
# +func_name+::
|
14
|
-
# the actual sorting. It's important that both match or else
|
13
|
+
# +name+:: <tt>String</tt> Name of the sorting algorithm for display purposes
|
14
|
+
# +func_name+:: <tt>Symbol</tt> A symbol representation of the actual method that does
|
15
|
+
# the actual sorting. It's important that both match or else <tt>Sortviz::Visualizer</tt>
|
15
16
|
# will not be able to find it and call it.
|
16
17
|
def define_algorithm(name, func_name)
|
17
18
|
ALGORITHMS[func_name] = name
|
data/lib/sortviz/version.rb
CHANGED
data/lib/sortviz/visualizer.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
module Sortviz
|
2
2
|
# Visualizer is the entry point of Sortviz, given an algorithm name it'll
|
3
|
-
# initialize Curses library, a
|
4
|
-
# and a
|
3
|
+
# initialize Curses library, a <tt>Sortviz::Canvas</tt> with proper screen dimensions
|
4
|
+
# and a <tt>Sortviz::Cursor</tt>.
|
5
5
|
#
|
6
6
|
# It also generates a shuffled list of numbers counting from 1 to n, n is
|
7
|
-
# decided by Sortviz::Visualizer#generate_list which calculates the number
|
7
|
+
# decided by <tt>Sortviz::Visualizer#generate_list</tt> which calculates the number
|
8
8
|
# of bar charts that can be drawn given the current screen size.
|
9
9
|
#
|
10
|
-
#
|
10
|
+
# <em>Notice</em>: Curses coordinates are treated in reverse, (y, x) and x represents
|
11
11
|
# the number of columns, y represents the number of lines so
|
12
|
-
#
|
12
|
+
# <em>(y, x)</em> is <em>(cols, lines)</em>. As a result of that we too, use <em>(y, x)</em>.
|
13
13
|
class Visualizer
|
14
|
-
# Origin starts at (1, 0) not
|
14
|
+
# Origin starts at (1, 0) not (0, 0)
|
15
15
|
ORIGIN = { y: 1, x: 5 }
|
16
16
|
SLEEP_INTERVAL = 0.05
|
17
17
|
|
18
18
|
# Initializes a new Visualizer for a sorting algorithm
|
19
|
+
#
|
19
20
|
# Params:
|
20
|
-
# +algo+::
|
21
|
+
# +algo+:: <tt>Symbol</tt> A symbol representing a sorting algorithm method
|
21
22
|
def initialize(algo)
|
22
23
|
setup_curses
|
23
24
|
@screen_dim = { cols: Curses.cols - ORIGIN[:x], lines: Curses.lines }
|
@@ -31,28 +32,28 @@ module Sortviz
|
|
31
32
|
|
32
33
|
# Starts the actual visualization
|
33
34
|
# * First prints the program's banner
|
34
|
-
# * Sets up the canvas (
|
35
|
+
# * Sets up the canvas (<tt>Sortviz::Canvas</tt>)
|
35
36
|
# * Caches the cursor location so we can return to our original location later
|
36
37
|
# * Refreshes the current standard screen first
|
37
38
|
# * Refreshes the canvas
|
38
|
-
# * Finally we switch input to the canvas through
|
39
|
+
# * Finally we switch input to the canvas through <tt>Sortviz::Cursor#switch_window</tt>
|
39
40
|
#
|
40
41
|
# The order is important because curses works in such a way that the last
|
41
42
|
# thing on display is the last thing refreshed, if we had refreshed canvas
|
42
43
|
# first, it would have been drawn normally, then we refresh the standard
|
43
44
|
# screen (in which the canvas is inside) it'll do just that refreshs and overrides the canvas.
|
44
45
|
#
|
45
|
-
#
|
46
|
+
# ==== The loop
|
46
47
|
# In order to draw every returned partially sorted list from the algorithm method
|
47
48
|
# we have to loop, clearing the canvas everytime and redrawing it then telling
|
48
49
|
# Curses to apply these drawn updates. This helps with flicker but I still
|
49
|
-
# seem to be getting some of it ever since I introduced the
|
50
|
+
# seem to be getting some of it ever since I introduced the <tt>Curses::A_REVERSE</tt>
|
50
51
|
# attribute.
|
51
52
|
#
|
52
53
|
# Durring the loop, we're trapped inside the sorting method, as a block of code
|
53
54
|
# that gets yielded to on every iteration of the sorting algorithm. To exit that
|
54
55
|
# at anytime during the sorting run, we capture the keyboard and return if any key is pressed,
|
55
|
-
# the canvas has
|
56
|
+
# the canvas has <tt>Curses::Window#getch</tt> on non-block mode so it doesn't block waiting for a character.
|
56
57
|
#
|
57
58
|
# Finally, we make sure both the canvas and the standard screen are closed.
|
58
59
|
# It's important to note that we also poll on keyboard after we exit the sorting method.
|
data/sortviz.gemspec
CHANGED
@@ -7,11 +7,11 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "sortviz"
|
8
8
|
spec.version = Sortviz::VERSION
|
9
9
|
spec.authors = ["Hady Ahmed"]
|
10
|
-
spec.email = ["
|
10
|
+
spec.email = ["me@hadyahmed.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Terminal based sorting algorithms visualizer}
|
13
13
|
spec.description = %q{Visualize sorting algorithms with Ruby and Curses lib}
|
14
|
-
spec.homepage = "
|
14
|
+
spec.homepage = "https://github.com/l0gicpath/sortviz"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.12"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
-
spec.add_dependency "curses"
|
25
|
+
spec.add_dependency "curses", "~> 1.0.2"
|
26
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sortviz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hady Ahmed
|
@@ -56,19 +56,19 @@ dependencies:
|
|
56
56
|
name: curses
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.0.2
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.0.2
|
69
69
|
description: Visualize sorting algorithms with Ruby and Curses lib
|
70
70
|
email:
|
71
|
-
-
|
71
|
+
- me@hadyahmed.com
|
72
72
|
executables:
|
73
73
|
- sortviz
|
74
74
|
extensions: []
|
@@ -98,7 +98,7 @@ files:
|
|
98
98
|
- lib/sortviz/visualizer.rb
|
99
99
|
- screenshot.png
|
100
100
|
- sortviz.gemspec
|
101
|
-
homepage:
|
101
|
+
homepage: https://github.com/l0gicpath/sortviz
|
102
102
|
licenses:
|
103
103
|
- MIT
|
104
104
|
metadata: {}
|