textselect

Interactively select lines and pipe it to a command
Log | Files | Refs | README | LICENSE

commit 6a6b66890913181f59d9e1db5461b3d139416167
parent 9005c2eaffbd3438cb94ccda7dbbd401a4beef7c
Author: Friedel Schön <[email protected]>
Date:   Tue,  6 Aug 2024 15:08:45 +0200

add readme, update manpage

Diffstat:
AREADME.md | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtextselect.1 | 6++----
Mtextselect.c | 37++++++++++++++++++++++++++++++++-----
3 files changed, 132 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,98 @@ +# textselect + +`textselect` is a command-line utility for interactively selecting lines from a text file using an ncurses interface. Selected lines can be saved to a file or passed as arguments to a command. + +## Features + +- **Interactive Selection**: Navigate through lines of a text file and select or deselect lines using an ncurses interface. +- **Customizable Output**: Save selected lines to a file or pass them as arguments to another command. +- **Invert Selection**: Toggle the inversion of selection to easily change which lines are selected. +- **Use with `xargs`**: Pass selected lines as arguments to a command. + +## Installation + +To build and install `textselect`, follow these steps: + +1. **Clone the Repository**: + + ```sh + git clone https://github.com/friedelschoen/textselect + cd textselect + ``` + +2. **Compile the Program**: + + ```sh + make + ``` + +3. **Install the Program** (optional): + + ```sh + sudo make install + ``` + +## Usage + +```sh +textselect [-hvx] [-o output] <input> [command [args...]] +``` + +### Options + +- `-h` + Display help information and exit. + +- `-v` + Invert the selection of lines. + +- `-x` + Pass the selected lines as arguments to the specified command. + +- `-o output` + Specify an output file to save the selected lines. If not specified, the selected lines are printed to stdout. + +### Navigation and Selection Keys + +- `UP`, `LEFT` + Move the cursor up. + +- `DOWN`, `RIGHT` + Move the cursor down. + +- `v` + Invert the selection of lines. + +- `SPACE` + Select or deselect the current line. + +- `ENTER`, `q` + Quit the selection interface. + +### Examples + +- **Select lines from `input.txt` and save them to `output.txt`**: + + ```sh + textselect -o output.txt input.txt + ``` + +- **Select lines from `input.txt` and pass them as arguments to the `sort` command**: + + ```sh + textselect input.txt sort + ``` + +- **Select lines from `input.txt` and pass them as arguments to a command using `xargs`**: + + ```sh + textselect -x input.txt ls + ``` + +## Contributing + +Contributions are welcome! Please open an issue or submit a pull request with any improvements or fixes. + +## License + +`textselect` is licensed under the Zlib License. See the [LICENSE](LICENSE) file for details. diff --git a/textselect.1 b/textselect.1 @@ -57,7 +57,4 @@ Select lines from "input.txt" and pass them as input to the "sort" command: textselect input.txt sort .SH AUTHOR -Written by Your Name. - -.SH COPYRIGHT -This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +Written by Friedel Schon. +\ No newline at end of file diff --git a/textselect.c b/textselect.c @@ -157,6 +157,30 @@ NORETURN void usage(int exitcode) { exit(exitcode); } +void help(void) { + fprintf(stderr, + "Usage: %s [-hvx] [-o output] <input> [command [args...]]\n" + "Interactively select lines from a text file and optionally execute a command with the selected lines.\n" + "\n" + "Options:\n" + " -h Display this help message and exit\n" + " -v Invert the selection of lines\n" + " -x Call command with selected lines as arguement\n" + " -o output Specify an output file to save the selected lines\n" + "\n" + "Navigation and selection keys:\n" + " UP, LEFT Move the cursor up\n" + " DOWN, RIGHT Move the cursor down\n" + " v Invert the selection of lines\n" + " SPACE Select or deselect the current line\n" + " ENTER, q Quit the selection interface\n" + "\n" + "Examples:\n" + " textselect -o output.txt input.txt\n" + " textselect input.txt sort\n", + argv0); +} + /** * @brief Initializes and handles the ncurses window for line selection. */ @@ -168,13 +192,13 @@ void display_window(void) { int current_line = 0; int start_line = 0; - int ch; int max_y = getmaxy(stdscr); + bool quit = false; draw_lines(stdscr, current_line, start_line, max_y); - while ((ch = getch()) != 'q') { - switch (ch) { + while (!quit) { + switch (getch()) { case KEY_UP: case KEY_LEFT: if (current_line > 0) { @@ -194,10 +218,12 @@ void display_window(void) { case 'v': invert_chosen = !invert_chosen; break; - case KEY_ENTER: case ' ': chosen_lines[current_line] = !chosen_lines[current_line]; break; + case KEY_ENTER: + case 'q': + quit = true; } draw_lines(stdscr, current_line, start_line, max_y); } @@ -275,7 +301,8 @@ int main(int argc, char* argv[]) { ARGBEGIN switch (OPT) { case 'h': - usage(0); + help(); + exit(0); case 'v': invert_chosen = true; break;