From a65160a232856cea2d1734fbbcc942329191e2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mon=20ou=C3=AFe?= Date: Fri, 31 Aug 2012 20:32:33 +0200 Subject: [PATCH] Made C-u remove only the beginning of the line by default, as it does in Readline --- lib/coolline/coolline.rb | 2 +- lib/coolline/editor.rb | 6 +++++ test/editor_test.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/coolline/coolline.rb b/lib/coolline/coolline.rb index 214677d..903778c 100644 --- a/lib/coolline/coolline.rb +++ b/lib/coolline/coolline.rb @@ -34,7 +34,7 @@ class Coolline Handler.new(?\C-a, &:beginning_of_line), Handler.new(?\C-e, &:end_of_line), Handler.new(?\C-k, &:kill_line), - Handler.new(?\C-u, &:clear_line), + Handler.new(?\C-u, &:kill_beginning_of_line), Handler.new(?\C-f, &:forward_char), Handler.new(?\C-b, &:backward_char), Handler.new(?\C-d, &:kill_current_char_or_leave), diff --git a/lib/coolline/editor.rb b/lib/coolline/editor.rb index 377a585..58968b0 100644 --- a/lib/coolline/editor.rb +++ b/lib/coolline/editor.rb @@ -128,6 +128,12 @@ def kill_line line[pos..-1] = "" end + # Removes everything up to the current character + def kill_beginning_of_line + line[0...pos] = "" + self.pos = 0 + end + # Removes all the characters in the line def clear_line line.clear diff --git a/test/editor_test.rb b/test/editor_test.rb index 3eb426c..95e8abc 100644 --- a/test/editor_test.rb +++ b/test/editor_test.rb @@ -63,6 +63,13 @@ asserts(:line).equals "" end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals "" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -142,6 +149,13 @@ asserts(:line).equals "" end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals "a lovely dragon" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -216,6 +230,13 @@ asserts(:line).equals "foo " end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals " bar" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -318,6 +339,13 @@ asserts(:line).equals "foo bar" end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals " baz" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -425,6 +453,13 @@ asserts(:line).equals "a lovely dragon" end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals "" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -537,6 +572,13 @@ asserts(:line).equals "foo bar " end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals "baz qux" + end + context "after transposing words" do hookup { topic.transpose_words } @@ -649,6 +691,13 @@ asserts(:line).equals "foo bar b" end + context "after killing the beginning of the line" do + hookup { topic.kill_beginning_of_line } + + asserts(:pos).equals 0 + asserts(:line).equals "az qux" + end + context "after transposing words" do hookup { topic.transpose_words }