I'm using pyserial to create a serial application. I am looking for the ASCII or ANSI escape sequence for CTRL+A and CTRL+E (go to beginning and end of the line, respectively). I cannot seem to find the escape codes anywhere. Does anyone know of a resource that lists the codes? CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys. I have to believe that an escape sequence exists.
Any suggestions would be greatly appreciated!
Neither ASCII characters nor ANSI control codes really quite define a thing called CTRL+A.
However, the ASCII control characters 0-31 (which officially have names from NUL through US) are traditionally mapped as the "control version" of the printable characters 64-95. So, 0 is ^#, 1 is ^A, 2 is ^B, etc.
The Wikipedia pages on ASCII and control character give further information.
So, ^A is 1, and ^E is 5.
Meanwhile:
CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys.
They're not that universal. These are part of the "basic emacs control keys", which a number of Unix terminal emulators and some Unix-based GUI apps (and things like the libreadline CLI editing library and the Qt text widgets) support by convention. However, try hitting ^A in, say, a DOS prompt or Microsoft Word and it won't go to the start of the line.
And, just sending ^A or ^E to an ANSI-compatible terminal won't necessarily move the cursor to the start or end of the line. To do that properly, you want to actually send the corresponding ANSI control sequences. Any list of ANSI escape codes, like Wikipedia's, will show you what's available.
Unfortunately, there is no right answer to what you're trying to do. ANSI treats the entire 80x25 (or whatever) screen as accessible, and doesn't distinguish between character positions that have something in them and those that are blank. So, you can't move to the "end of the line", unless by that you mean "column 79".
And, if what you're looking for is moving to columns 0 and 79, that's easy with the CHA command ('\x1b[0G' and '\x1b[79G')—but not all ANSI terminals support that; in particular, DOS ANSI.SYS and anything built to be compatible with it will ignore it.
Related
How can I implement a Python function to run on Unix systems (minimally, just Linux) that is similar to builtin input(), but supports using the escape key to cancel inputting text? So:
Enter a single line of text at the command line (multiple lines would be OK also)
with simple line editing (left/right arrow keys, backspace to delete a character, control-a to jump to start of line, control-e to jump to end of line, control-w to delete a word)
(and allowing pastes from primary or clipboard selection in X/wayland -- I guess this requires no special support)
submit the text by hitting the return key
or hit the escape key to exit and cancel (it would be OK but not ideal if I couldn't tell the difference between this and the user entering an empty string and hitting return)
How can I achieve that?
I've tried curses, but that is focused on whole-screen input -- it seems hard to implement something that does not clear the screen.
I've tried termios, but I don't know how to implement the backspace key for example, and I suspect I might end up wanting slightly more functionality than is immediately obvious -- for example control-w, which on my linux machine input() implements to delete a whole word. I don't need to reproduce every little detail of input()'s behaviour, but a few things like that (see the list above).
You can use readline, for example
#! /usr/bin/env python3
import readline
input('hello: ')
This takes your ~/.inputrc settings into account. You can configure editing-mode that controls which default set of key bindings is used. By default, Readline starts up in Emacs editing mode, where the keystrokes are most similar to Emacs. This variable can be set to either emacs or vi.
I'd rather avoid using curses for this, largely because of how much curses screws with the terminal by default. Frankly, using window.inch() in curses has not worked correctly in most situations, anyway, so it doesn't seem worth working through all the echo, newline, and other issues with curses to get that function.
I need to be able to read the value of a character at some position on the terminal. I don't particularly care if unicode characters work or if I get info about the character attributes (like underline, bold, color, etc.) though it would be nice.
Thanks...
I obfuscated my python 3.5 code and now I am left with these strange black boxes. I am not able to copy them and "search and replace" with some character. How am I supposed to get rid of them? The code won't run, it keeps raising "syntaxError: invalid character in identifier"
This is very frustrating I have been stuck with this issue for hours.
If you use Notepad++, you can use the HEX-Editor plugin and view the raw hex of your source, and then use Ctrl+H to find and replace "A6" with whatever you desire.
If you do not have that plugin, it's very easy to download it using Notepad++'s built in plugin manager.
The extended ASCII Tables tell me that 0xA6 is a broken vertical bar, which certainly doesn't seem like a valid character for an identifier, so no questions there.
I know I can use either print \b to replace a single character or sys.stdout.write(\rSomething) to replace an entire line of text (like in this example), but is there a way replace the top line in the console window using Python while letting text continue to print below?
For example:
[---- ] <-- a progress bar that updates at the top...
Ongoing info <-- while other stuff prints down below
foo
More ongoing info
It's more complex that just using characters, as you need more powerful control over terminal.
This may sources for help:
Curses Programming with Python
curses — Terminal handling for character-cell displays
If you're really want to go hard way and do it using RAW control, you have to learn about ANSI control code (well known as Escape sequences). However - you have been warned - that way is not portable as different terminal types use in fact slightly different sequences to control what and how is displayed in terminal and some terminal doesn't support some codes and then certain operations are handled different way. Using curses solve these problems for you.
I have a Logitech 360 keyboard, with which I am trying to code Python on the Raspberry Pi B. The apostrophe key produces a slanted quote, instead of the 'vertical' single quote, and this causes syntax errors in code (the same code runs perfectly when I paste in a snippet from the browser, which is the only way I can find to produce the correct flavor of apostrophe).
The syntax error is "Non-ASCII character '\xc2' in file '---' on line X, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details." The webpage suggests declaring a character encoding at the beginning of the script, but it didn't work for me, and in any case, I would rather not have to have it at the beginning of every script - I just want the keyboard to produce the correct character to begin with. I have fiddled with the keyboard config international settings; nothing works. It's driving me nuts.
you cannot use "\xc2" as a quote character without redefining the quote character in python source grammar(really your problems extend beyond even this) and recompiling python ....
you probably can change what character your logitech uses as a quote
You may want to check your Internationalisation Options by running
sudo raspi-config
Choose Option 4 - Internationalization Options
and then Option I3 - Change Keyboard Layout
Go through and check your settings and then try your keyboard again.