add `0` key for beginning-of-line #15

Merged
merged 2 commits into from Aug 17, 2014

Projects

None yet

2 participants

@nathantypanski
Contributor

This adds a shortcut for the 0 key to take the user to the beginning
of a given line, like Vim. It simply calls line_home like ^ does,
but this one is in my muscle memory from Vim usage.

By the way, thanks for developing this. I was looking for a dead-simple
command-line CSV viewer, and this fits the bill.

@nathantypanski nathantypanski add `0` key for beginning-of-line
This adds a shortcut for the `0` key to take the user to the beginning
of a given line, like Vim. It simply calls `line_home` like `^` does,
but this one is in my muscle memory from Vim usage.
d9784bf
@nathantypanski
Contributor

Ugh. This breaks the modifier key handling for 0. I'll update this PR if I fix it.

@nathantypanski nathantypanski Stop clobbering modifiers for digit keypresses
Previously, when `0` was used to call `line_home`, this would clobber
the modifiers even if a digit 1-9 was pressed before `0`, which should
signify the beginning of a modifier key sequence and cause `0` to behave
as a modifier until the next command is pressed.

This checks if a current modifier is in effect, and if so it does not
treat any digit keypresses as commands until the modifier is cleared.
65ebb38
@nathantypanski
Contributor

Well, I've fixed that issue. Behavior for 0 is now the same as Vim:

  • Pressing 0 without any modifier present goes to the beginning of the current line.
  • If a modifier is already present, 0 is considered a part of that modifier sequence.

There's some jumping around required because of the fact that .isdigit() fails if c > 256, but it's otherwise straightforward.

Note: It occurs to me that because of this behavior, even in your current upstream pressing any key c > 256 that is not assigned in the Viewer.keys attribute will crash the program. For example, pressing F2 will trigger a crash.

@firecat53
Owner

Thanks for doing that!! I've been hitting 0 too and been meaning to fix that. Haven't touched this for awhile though. Gimme a day or two to look at this (in between other stuff :) )

Scott

@nathantypanski
Contributor

Of course!

One other thing I noticed when working on this/reviewing it:

Line 121 is probably unnecessary. But I have something of a habit of changing this kind of "boolean context" usage of regular objects into boolan expressions, since Python's behavior isn't always what you mean to say. For example, even though it works fine for strings:

>>> not ''
True
>>> not '0'
False
>>> not 'False'
False

it gets stranger when you start mixing numerics:

>>> not 0
True
>>> not 99
False
>>> modifier = '{}{}'.format(modifier, '0')
>>> not int(modifier)
True

So I just try to avoid these weird bugs altogether by specifying booleans explicitly when it doesn't hurt the duck typing.

@firecat53 firecat53 merged commit 65ebb38 into firecat53:master Aug 17, 2014
@firecat53
Owner

Thanks! I added one more commit that looks it fixes the crash on unhandled characters.

@nathantypanski
Contributor

Good job fixing the crash. Thanks for merging. 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment