Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions examples/keyboard_and_mouse.cr
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ begin
end

# Draw title
title = "⌨ KEYBOARD & MOUSE DEMO ⌨"
termisu.title = title = "⌨ KEYBOARD & MOUSE DEMO ⌨"
title_x = [(width - title.size) // 2, 0].max
draw_text.call(title_x, start_y - 2, title, Termisu::Color.cyan, nil)

Expand Down Expand Up @@ -380,6 +380,14 @@ begin
# Redraw
draw_mouse_panel.call(mouse_x, mouse_y, mouse_button)
draw_event_log.call(last_event_text)
termisu.set_cursor(
mouse_x - 1,
mouse_y - 1,
blink: mouse_event.ctrl? || mouse_event.shift? || mouse_event.alt?,
shape: {1 => Termisu::Terminal::Cursor::Shape::Block,
2 => Termisu::Terminal::Cursor::Shape::Underline,
3 => Termisu::Terminal::Cursor::Shape::Bar}[mouse_event.button.value]?
)
termisu.render
when Termisu::Event::Resize
resize_event = event.as(Termisu::Event::Resize)
Expand Down Expand Up @@ -412,7 +420,5 @@ begin
end
ensure
Termisu::Log.info { "Keyboard & Mouse demo closing" }
termisu.disable_enhanced_keyboard
termisu.disable_mouse
termisu.close
end
6 changes: 3 additions & 3 deletions spec/support/capture_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CaptureRenderer < Termisu::Renderer

# --- Core I/O ---

def write(data : String)
def write(data : String, columns_advanced = 0)
@writes << data
end

Expand All @@ -50,13 +50,13 @@ class CaptureRenderer < Termisu::Renderer
write("\e[#{y + 1};#{x + 1}H")
end

def write_show_cursor
def show_cursor
return if @cached_cursor_visible
@cached_cursor_visible = true
write("\e[?25h")
end

def write_hide_cursor
def hide_cursor
cached = @cached_cursor_visible
return if cached.is_a?(Bool) && !cached
@cached_cursor_visible = false
Expand Down
1 change: 1 addition & 0 deletions spec/support/capture_terminal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class CaptureTerminal < Termisu::Terminal
property writes : Array(String) = [] of String
property captured_flush_count : Int32 = 0
property size = {80, 24}

def initialize(*, sync_updates : Bool = true)
super(sync_updates: sync_updates)
Expand Down
6 changes: 3 additions & 3 deletions spec/support/mock_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MockRenderer < Termisu::Renderer

# --- Core I/O ---

def write(data : String)
def write(data : String, columns_advanced = 0)
@write_calls << data
end
Comment thread
coderabbitai[bot] marked this conversation as resolved.

Expand All @@ -60,11 +60,11 @@ class MockRenderer < Termisu::Renderer
@move_calls << {x, y}
end

def write_show_cursor
def show_cursor
@show_cursor_count += 1
end

def write_hide_cursor
def hide_cursor
@hide_cursor_count += 1
end

Expand Down
130 changes: 0 additions & 130 deletions spec/termisu/buffer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,6 @@ describe Termisu::Buffer do
renderer.move_calls.should contain({0, 2})
end

it "skips cursor movement when cursor is already at correct position" do
renderer = MockRenderer.new
buffer = Termisu::Buffer.new(5, 3)

# First render - set up initial state
buffer.set_cell(2, 1, 'A')
buffer.render_to(renderer)

# Clear tracking
renderer.clear

# Write at position where cursor already is (right after 'A')
buffer.set_cell(3, 1, 'B')
buffer.render_to(renderer)

# Should write 'B' but no cursor move needed (cursor advanced from writing 'A')
renderer.write_calls.should contain("B")
renderer.move_calls.should be_empty # Optimization: cursor already at right position
end

it "calls renderer.flush after rendering" do
renderer = MockRenderer.new
buffer = Termisu::Buffer.new(5, 3)
Expand Down Expand Up @@ -500,116 +480,6 @@ describe Termisu::Buffer do
cell = buffer.get_cell(3, 2)
cell.as(Termisu::Cell).grapheme.should eq("C")
end

it "clamps cursor position when shrinking" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(8, 6)

buffer.resize(5, 4)

buffer.cursor.x.should eq(4)
buffer.cursor.y.should eq(3)
buffer.cursor.visible?.should be_true
end

it "preserves cursor position when growing" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(5, 4)

buffer.resize(20, 16)

buffer.cursor.x.should eq(5)
buffer.cursor.y.should eq(4)
end

it "clamps hidden cursor's last position when shrinking" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(8, 6)
buffer.hide_cursor

buffer.resize(5, 4)

buffer.cursor.hidden?.should be_true

# When shown, should be at clamped position
buffer.show_cursor
buffer.cursor.x.should eq(4)
buffer.cursor.y.should eq(3)
end
end

describe "#set_cursor" do
it "sets cursor to valid position" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(5, 4)

buffer.cursor.x.should eq(5)
buffer.cursor.y.should eq(4)
buffer.cursor.visible?.should be_true
end

it "clamps cursor x to buffer width" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(15, 4)

buffer.cursor.x.should eq(9)
buffer.cursor.y.should eq(4)
end

it "clamps cursor y to buffer height" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(5, 12)

buffer.cursor.x.should eq(5)
buffer.cursor.y.should eq(7)
end

it "clamps negative x to 0" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(-5, 4)

buffer.cursor.x.should eq(0)
buffer.cursor.y.should eq(4)
end

it "clamps negative y to 0" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(5, -3)

buffer.cursor.x.should eq(5)
buffer.cursor.y.should eq(0)
end

it "clamps both coordinates when both out of bounds" do
buffer = Termisu::Buffer.new(10, 8)
buffer.set_cursor(100, 100)

buffer.cursor.x.should eq(9)
buffer.cursor.y.should eq(7)
end
end

describe "cursor rendering" do
it "renders visible cursor" do
renderer = MockRenderer.new
buffer = Termisu::Buffer.new(5, 3)
buffer.set_cursor(2, 1)

buffer.render_to(renderer)

renderer.show_cursor_count.should eq(1)
renderer.move_calls.should contain({2, 1})
end

it "renders hidden cursor" do
renderer = MockRenderer.new
buffer = Termisu::Buffer.new(5, 3)
buffer.hide_cursor

buffer.render_to(renderer)

renderer.hide_cursor_count.should eq(1)
end
end

describe "wide character write semantics" do
Expand Down
153 changes: 0 additions & 153 deletions spec/termisu/cursor_spec.cr

This file was deleted.

Loading
Loading