Giter Site home page Giter Site logo

textualize / rich-cli Goto Github PK

View Code? Open in Web Editor NEW
2.9K 29.0 76.0 8.8 MB

Rich-cli is a command line toolbox for fancy output in the terminal

Home Page: https://www.textualize.io

License: MIT License

Python 91.04% Jupyter Notebook 8.96%
rich terminal terminal-colors syntax-highlighting markdown

rich-cli's Introduction

Rich-CLI

Rich-CLI is a command line toolbox for fancy output in the terminal, built with Rich.

Use the rich command to highlight a variety of file types in the terminal, with specialized rendering for Markdown and JSON files. Additionally you can markup and format text from the command line.

splash image

Installation

There are a few options for installing Rich-CLI.

Windows / Linux

You can install Rich-CLI with pipx.

pipx install rich-cli

MacOS

You can install Rich-CLI with Homebrew.

brew install rich

Universal

Rich-CLI may be installed as a Python package, either using pip:

python -m pip install rich-cli

Or using conda or mamba:

mamba install -c conda-forge rich-cli

Rich command

Once installed you should have the rich command in your path. Run the following to see usage / help:

rich --help

Syntax highlighting

To syntax highlight a file enter rich followed by a path. Many file formats are supported.

rich loop.py

syntax1

Add the --line-number or -n switch to enable line numbers. Add --guides or -g to enable indentation guides.

rich loop.py -n -g

syntax2

You can specify a theme with --theme.

rich loop.py --theme dracula

You can set the default theme via the RICH_THEME environment variable. So the following is equivalent to the above command:

RICH_THEME=dracula rich loop.py

syntax3

By default, rich will wrap lines if they don't fit within the available width. You can disable this behavior with --no-wrap.

Rich will try to deduce the format of the via from the filename. If you want to override the auto-detected lexer you can explicitly set it with the --lexer or -x switch.

Markdown

You can request markdown rendering by adding the --markdown switch or -m. If the file ends with .md markdown will be auto-detected.

rich README.md

markdown1

If your terminal supports hyperlinks, you can add --hyperlinks or -y which will output hyperlinks rather than full URLs.

rich README.md --hyperlinks

Jupyter notebook

You can request Jupyter notebook rendering by adding the --ipynb switch. If the file ends with .ipynb Jupyter notebook will be auto-detected.

rich notebook.ipynb

All options that apply to syntax highlighting can be applied to code cells, and all options that apply to Markdown can be applied to Markdown cells.

JSON

You can request JSON pretty formatting and highlighting with the --json or -j switches. If the file ends with .json then JSON will be auto-detected.

rich cats.json

json1

CSV

Rich can display the contents of a CSV (or TSV) as a table. If the file ends with .csv or .tsv then CSV will be auto-detected.

rich deniro.csv

csv1

Rules

You can render a horizontal rule with --rule or -u. Specify a rule style with --rule-style. Set the character(s) to render the line with --rule-char.

rich "Hello [b]World[/b]!" --rule
rich "Hello [b]World[/b]!" --rule --rule-style "red"
rich "Hello [b]World[/b]!" --rule --rule-style "red" --rule-char "="

syntax1

Pager

Add --pager to display the content with a built in pager application.

Scroll the pager with cursor keys, page up/down, home, end. Alternatively use the scrollbar which will be visible to the right of the terminal. Or use the vi navigation (j, k, ctrl_d, ctrl-u).

rich __main__.py -n -g --theme monokai --pager

pager

Network

The rich command can read files from the internet you give it a URL starting with http:// or https://.

rich https://raw.githubusercontent.com/Textualize/rich-cli/main/README.md --markdown

network

Exporting

In addition to rendering to the console, rich can write an HTML file. This works with any command. Add --export-html or -o followed by the output path.

rich README.md -o readme.html

After running this command you should find a "readme.html" in your current working directory.

Rich Printing

If you add the --print or --p option then Rich will treat the first argument as console markup which allows you to insert styles with a markup similar in design to bbcode.

rich "Hello, [bold magenta]World[/]!" --print

printing1

Soft wrapping

Rich will word wrap your text by default by inserting newlines where appropriate. If you don't want this behavior you can enable soft wrapping with --soft.

Reading from Stdin

Where rich accepts a path, you can enter - which reads the content from stdin. You may want this if you are piping output from another process.

Note that when rich isn't writing directly to the terminal it will disable ansi color codes, so you may want to add --force-terminal or -F to tell rich you want to keep ansi codes in the output.

cat README.md | rich - --markdown --force-terminal

General Options

There are a number of additional switches you may add to modify the content rendered to the terminal. These options are universal and apply to all of the above features.

Style

You can set a style to apply to the output with --style or -s. The styles are specified with this syntax.

rich "Hello, [b]World[/b]!" --print --style "on blue"

style1

Alignment

You can align output to the left, center, or right with the --left, --center, or --right options, or their single letter counterparts: -l, -c, or -r.

rich "Hello [b]World[/b]!" --print --center

alignment1

Width

You can set the width of the output with --width or -w and the desired width. Note that the default behavior is to wrap text.

rich "I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration." -p -w 40

width

Text Justify

You can set how rich will justify text with --text-left, --text-right, --text-center, and --text-full; or the single letter equivalents: -L, -R, -C, and -F.

The difference between --left and --text-left may not be obvious unless you specify the width of the output. The --left, --center, and --right options will center the block of text within the terminal dimensions. Whereas, the --text-left, --text-center, and --text-right options define how text is rendered within that block.

In the following examples, we specify a width of 40 (-w 40) which is center aligned with the -c switch. Note how the -R, -C and -F apply the text justification within the 40 character block:

rich "I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration." -p -w 40 -c -L
rich "I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration." -p -w 40 -c -R
rich "I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration." -p -w 40 -c -C
rich "I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration." -p -w 40 -c -F

Padding

You can apply padding around the output with --padding or -d.

rich "Hello [b]World[/b]!" -p -c --padding 3 --style "on blue"

padding1

Panel

You can draw a panel around content with --panel or -a, which takes one of a number of styles.

rich "Hello, [b]World[/b]!" -p -a heavy

panel1

rich-cli's People

Contributors

darrenburns avatar davidbrochart avatar didierrlopes avatar dtrodrigues avatar henryiii avatar liiight avatar olivierphi avatar treyhunner avatar vperrollaz avatar wasi-master avatar willmcgugan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rich-cli's Issues

Recommended way to process text/tags within a file? (ex. `cat file` which contains [B]text[/] )

Hey there, I started playing around with this last night and wanted to use it to spruce up some of my terminal text that I have generated via Jobber (similar to cron) which gets output into files on a ram drive, then when I open the terminal it just does cat file. I wanted to add some tags into the text for color and bold, etc to start out, but I was not able to get it to actually process those tags to display.

This is the overall setup:

# -- script to check for updates that runs every 10-15 minutes

if [[ $UPDATESTATUS == false ]]; then
    echo 'UPDATE_COMPLETE=true' >/home/mosthated/.updatestatus
    sudo apt-get update -qq
    UPDATES=$(sudo apt-get dist-upgrade --simulate | grep Inst | awk '{print $2}')
    COUNT=$(printf "%s\n" "$UPDATES" | grep -v "^$" | wc -l)
else
    exit 0
fi

UPDATECOUNT="[bold green]${COUNT}[/] update(s) available"
kvset updates "${UPDATECOUNT}" # -- outputs the above string to ramdrive

Then in my .zshrc I have:

cat /mnt/ramdisk/.kv/updates 

The things I tried to do were as follows:

rich /mnt/ramdisk/.kv/updates --print # just printed out the path
cat /mnt/ramdisk/.kv/updates | rich - # displayed the text in the file, but just as plain text showing the [tags], not processing them
rich cat /mnt/ramdisk/.kv/updates --print
rich $(cat /mnt/ramdisk/.kv/updates) --print

Is there a proper way to accomplish this, or is this out of scope of the intended usage of the cli?
Thanks,
-MH

rich-cli needs a --force-terminal

We will need a --force-terminal switch (plus single letter -f if its free) which forces terminal output. To allow users to pip to a file while allowing ansi codes (for pagers etc).

.csv files presented as a table?

Hi Will, when running the rich command and passing it a csv file, I am not getting the table output format as shown in the Readme file.....is there a specific command or option I am missing ? I am on Mac OS...

see output below when I point rich to one of this repo's csv files.....

rich https://raw.githubusercontent.com/Textualize/rich-cli/main/test_data/deniro.csv

"Year", "Score", "Title"
1968, 86, "Greetings"
1970, 17, "Bloody Mama"
1970, 73, "Hi, Mom!"
1971, 40, "Born to Win"
1973, 98, "Mean Streets"
1973, 88, "Bang the Drum Slowly"
1974, 97, "The Godfather, Part II"
1976, 41, "The Last Tycoon"
1976, 99, "Taxi Driver"
1977, 47, "1900"
1977, 67, "New York, New York"
1978, 93, "The Deer Hunter"
1980, 97, "Raging Bull"
1981, 75, "True Confessions"
1983, 90, "The King of Comedy"
1984, 89, "Once Upon a Time in America"
1984, 60, "Falling in Love"
1985, 98, "Brazil"
1986, 65, "The Mission"
1987, 100, "Dear America: Letters Home From Vietnam"
1987, 80, "The Untouchables"
1987, 78, "Angel Heart"
1988, 96, "Midnight Run"
1989, 64, "Jacknife"
1989, 47, "We're No Angels"
1990, 88, "Awakenings"
1990, 29, "Stanley & Iris"
1990, 96, "Goodfellas"
1991, 76, "Cape Fear"
1991, 69, "Mistress"
1991, 65, "Guilty by Suspicion"
1991, 71, "Backdraft"
1992, 87, "Thunderheart"
1992, 67, "Night and the City"
1993, 75, "This Boy's Life"
1993, 78, "Mad Dog and Glory"
1993, 96, "A Bronx Tale"
1994, 39, "Mary Shelley's Frankenstein"
1995, 80, "Casino"
1995, 86, "Heat"
1996, 74, "Sleepers"
1996, 38, "The Fan"
1996, 80, "Marvin's Room"
1997, 85, "Wag the Dog"
1997, 87, "Jackie Brown"
1997, 72, "Cop Land"
1998, 68, "Ronin"
1998, 38, "Great Expectations"
1999, 69, "Analyze This"
1999, 43, "Flawless"
2000, 43, "The Adventures of Rocky & Bullwinkle"
2000, 84, "Meet the Parents"
2000, 41, "Men of Honor"
2001, 73, "The Score"
2001, 33, "15 Minutes"
2002, 48, "City by the Sea"
2002, 27, "Analyze That"
2003, 4, "Godsend"
2004, 35, "Shark Tale"
2004, 38, "Meet the Fockers"
2005, 4, "The Bridge of San Luis Rey"
2005, 46, "Rent"
2005, 13, "Hide and Seek"
2006, 54, "The Good Shepherd"
2007, 21, "Arthur and the Invisibles"
2007, 76, "Captain Shakespeare"
2008, 19, "Righteous Kill"
2008, 51, "What Just Happened?"
2009, 46, "Everybody's Fine"
2010, 72, "Machete"
2010, 10, "Little Fockers"
2010, 50, "Stone"
2011, 25, "Killer Elite"
2011, 7, "New Year's Eve"
2011, 70, "Limitless"
2012, 92, "Silver Linings Playbook"
2012, 51, "Being Flynn"
2012, 29, "Red Lights"
2013, 46, "Last Vegas"
2013, 7, "The Big Wedding"
2013, 29, "Grudge Match"
2013, 11, "Killing Season"
2014, 9, "The Bag Man"
2015, 60, "Joy"
2015, 26, "Heist"
2015, 61, "The Intern"
2016, 11, "Dirty Grandpa"

Request: process other switches when --help is passed

Since rich uses itself to display the output of --help, it would be useful to process the switches affecting the output format when crafting the --help output.

$ unset LESS
$ rich --force-terminal --no-wrap -W $(( COLUMNS-4 )) --help | less -RJ

image

Have `rich-cli` render highlighted code to PNGs

This is a feature request: given that rich-cli can highlight code in the terminal and write its output to HTML files, I thought it would be nice to have it output to an image format like PNG.

Provide help for pager

rich-cli is really great, and pager mode (--pager) is useful, but it's not obvious (to me anyway) what key bindings are available when using pager mode. It would be helpful if there were some documentation somewhere, and/or rich-cli would provide a help summary if the user typed ? in pager mode.

better support for jsx or adding jsx as a lexer?

Syntax highlighting of HTML closing tags in .js files seems to be wrong however, and I understand that this is due to the way Pygment / lexer handles js file more so than rich-cli per-se, but with js libraries such as React being so prevalent nowadays would this be an interesting area of improvement?

Related to that -- maybe we could add a jsx as a lexer as well?

Screenshot from 2022-04-25 13-51-33

Screenshot from 2022-04-25 13-47-29

New switch for --dockerfile

Dockerfiles are nicely formatted when running rich Dockerfile. Thanks! πŸ‘

But when the dockerfile is named something else, or when its ouput comes from stdin, it seems to not be possible to request Dockerfile rendering, as there is no --dockerfile flag (similar to how it works for --markdown).

What is currently working (i.e. gives nice color coded output):

rich Dockerfile

What is currently not working (renders the file just like cat):

# Named something else than just Dockerfile:
rich Dockerfile.service1

# When used as "file extension":
rich service1.Dockerfile

# When coming from stdin:
cat Dockerfile | rich -

# Since previous example might just looks silly, here is an example where we do "useful" stuff before passing it to rich-cli:
cat Dockerfile | sed 's/FROM \S*/FROM node:16/gi' | rich -

Add --head and --tail options to rich-cli

We need a way of showing a fixed number of lines from either the head or the tail of a fail.

i.e. rich foo.py --head 10 would show the first ten lines.

Note that this needs to be done after the file has been highlighted. It's not enough to apply this to the file prior to highlighting.

Allow delimiters for output from Stdin

On running the following command

ps |rich - -m

I got the following output

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PID TTY          TIME CMD                                                                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
6697 pts/1    00:00:00 bash 6722 pts/1    00:00:36 fish 50260 pts/1    00:00:00 ps 50261 pts/1    00:00:00 rich

So rich-cli is able to inspect the first line (header) correctly, but not the subsequent lines.

Without rich-cli, I got the following output:

$ ps
    PID TTY          TIME CMD
   6697 pts/1    00:00:00 bash
   6722 pts/1    00:00:37 fish
  50453 pts/1    00:00:00 ps

Tested with commands like netstat.

One possible solution could be to allow the users to specify the delimiters.

I am wondering whether this could be a bug, since rich-cli is able to detect the first line.

Feature request: support setting `--hyperlinks` via the `RICH_HYPERLINKS` environment variable

Hi team, I've just discovered Rich and this CLI and I'm a big fan. Thanks for making this such an awesome tool!

While using it, I quickly noticed a desire to set-and-forget the --hyperlinks flag so I would not need to pass it every time. So making this an official request and have opened a PR for your review:

This is the only change:

envvar="RICH_HYPERLINKS",

Hopefully you can take a look when you have a chance. Thanks!

Allow "no box" around code blocks for Markdown

Hi @willmcgugan,

Thanks for making Rich!

It would be nice to allow no bounding box around code blocks for Markdown. The bounding box (lines) makes for easy reading but prevents copy/pasting of the code block content, as that also copies the lines of the box. Maybe there is already a set of options that do this, but I have not found them (yet).

Thanks :)

Add tail -f option

I use a software (written in Python) which logs messages to a file. I cannot change the fact that it redirects the logs to a file but I can enrich the code as described here.

Question would it be feasible to add a tail -f-like option to rich-cli?

Currently this does not work: tail -f app.log | rich - --print --force-terminal

Thank you.

Align title to the left in markdown

Hi,

Concerning the markdown preview, I see that the titles are centered. I would like to have them on the left instead. How can I achieve this?

Best regards

head / tail operations are slow on larger files

Howdy -

I wanted to preface this with: If I missed a contributor guideline or anything, please let me know. I did check other issues and did not see one relevant to this.

I am somewhat new to using rich-cli (but am familiar with rich) and recently attempted to parse a somewhat large CSV file (~119Mb, 483k lines).
I did not expect the whole CSV to load quickly, but I was somewhat surprised that running --head and --tail took as long as they did. Obviously they won't behave like GNU tail / head, but I took a jab at a minimal / naive change to this and was able to get it much faster. It's around this here
if you want I am happy to open a PR. I'll also just put a code block of what I did. I did take the somewhat naive approach to file parsing (rather than parsing the buffer stream per line, which would be more efficient for tail) to avoid making a huge change.

  • head is just using the existing generator to parse out x rows and filtering out None values. Since the list gets iterated ~ twice, this means the second iteration that adds indexes is also way faster.

  • tail is using a collections.deque example recipe (which, while still going through the whole file, does not store the whole file in memory).


    rows = iter(reader)
    if has_header:
        header = next(rows)
        for column in header:
            table.add_column(column)

    if head is not None:
        table_rows = list(
            filter(
                None,
                (next(rows, None) for _ in range(head)),
            )
        )

    elif tail is not None:
        table_rows = deque(rows, tail)

    else:
        table_rows = list(rows)


These are naive benchmarks, but comparing the two (where rich command is the install CLI, and python3 ./src/rich_cli having my changes:

Head

β””> time python3 ./src/rich_cli --head 500 large_csv.csv &> /dev/null                                           [πŸ‘Ύ 3.10.5]➜
python3 ./src/rich_cli --head 500 large_csv.csv &> /dev/null  0.83s user 0.47s system 94% cpu 1.369 total

β””> time rich --head 500 large_csv.csv &> /dev/null                                                             [πŸ‘Ύ 3.10.5]➜
rich --head 500 large_csv.csv &> /dev/null  2.81s user 0.60s system 99% cpu 3.443 total

Tail

β””> time rich --tail 500 large_csv.csv &> /dev/null                                                             [πŸ‘Ύ 3.10.5]➜
rich --tail 500 large_csv.csv &> /dev/null  2.95s user 0.63s system 99% cpu 3.604 total

β””> time python3 ./src/rich_cli --tail 500 large_csv.csv &> /dev/null                                           [πŸ‘Ύ 3.10.5]➜
python3 ./src/rich_cli --tail 500 large_csv.csv &> /dev/null  1.93s user 0.53s system 96% cpu 2.545 total

Anyway, let me know if you want me to do anything here!

Exposing Text.from_ansi in rich-cli

I recently had to "clean" a log file from ANSI codes.
Couldn't find a quick solution for this in bash, so I turned to rich-cli, but wit no luck either.
Thankfully rich (the library) has just the right trick for that. I can use Text.from_ansi and print it however I want (stripped from codes).

I was wondering if this Text.from_ansi would be useful to expose here in this tool.

It could maybe have a --ansi option and then for my use case one could use it like so:

rich --ansi text_with_ansi_codes.txt > text_stripped.txt

Not sure if this option would be useful for anything else (you could probably only adjust alignment, style and maybe export to html/svg) πŸ€” ?

Anyway, does this make sense?

"Could not determine delimiter" when trying to render TSV via stdin

Rendering TSV (tab-separated values) works when passing a file name:

rich temp.tsv

But it fails for the same file when passing as stdin (-) with error "Could not determine delimiter":

cat temp.tsv | rich - --csv

Apparently the CSV/TSV sniffer does not work correctly and the detection via the file extension (.tsv) makes it work (excel-tab dialect of csv parser) when passing the file name, but not when passing the same data via stdin (-).

Exporting colourful SVGs gives XML error

I was wondering if I could get a little help.. 😬

I'm trying to figure out a semi-generalised way for people to generate nice-looking SVGs showing command-line help output from ewels/rich-click.

One approach that I tried is to set force_terminal=True or color_system="256" on the rich-click console object. This maintains colour through a pipe to rich-click nicely, however it kills the SVGs..

See for example test.svg (doesn't render in GitHub comments). In the browser (Chrome, Safari), this renders with the following warning at the top:

image

Passing the file through the w3c validator tool gives slightly more detail:

Error Line 45, Column 184: illegal XML character U+1b
…-r1" x="0" y="20" textLength="1946.8">οΏ½[1m      β€¦

Any ideas on how to either (a) fix this problem or (b) generate the SVGs in a better way?

Thanks in advance!

Multiple emoji encoding issues on Windows

When trying to use rich to print files with emoji on Windows, there are some encoding issues.

Below are 2 cases I encountered.

Garbled text instead of emoji

When running rich broken-emoji.md (broken-emoji.md - a text file with nothing but the 😊 emoji in it) on Windows (in Windows Terminal), I get the following:

😊

If I run Get-Content broken-emoji.md or run rich inside WSL, I get the emoji printed as expected.

Rich fails to print entirely

When running rich cannot-print.md (cannot-print.md - only contains the 🀝 emoji) on Windows, I get:

unable to read .\cannot-print.md: 'charmap' codec can't decode byte 0x9d in position 3: character maps to <undefined>

Running it in WSL or using Get-Content cannot-print.md in the same terminal window gives me the emoji as expected.

Expected Results

As this works in the same terminal both with Powershell's Get-Content, and when using WSL to run rich-cli, I'd expect it to work in Windows as well.

Environment

OS: Windows 10 (build 19044.1889)
Terminal: Windows Terminal (version 1.14.2281.0) running PowerShell
Rich CLI: 1.8.0
Python: 3.10.1

Add a demo option

Add the option --demo to run the demo of rich, a basic run of python -m rich.

When you install rich-cli with pipx, you have not direct access to the virtualenv within the rich module. Running the demo will be a quick test of the terminal.

With Cygwin, rich-cli --pager display is empty

I use Windows Terminal, with a Cygwin shell in it.

Using rich on, for example, json.py from your rich repository, everything seems fine.

When I add the --pager option, the terminal is blank.

If I use Windows Terminal with a CMD shell, it works fine.

Let me know if you need any additional information.

Support line-delimited JSON

I have a log file where each line is a JSON record. It would be handy to be able to use rich to pretty-print the log file to make it more easily human-parseable from the command line.

unable to read %FILENAME%: 'charmap' codec can't decode byte %BYTE% in position %POSITION%: character maps to <undefined>

Hi,

Environment

  • Windows 10
  • Python 3.9
  • Windows Terminal and/or CMD
  • Any file (.txt, .html, .md, ...) with UTF8 characters

Problem

Rich-cli is throwing an error when trying to display UTF-8 encoded files:

unable to read %FILENAME%: 'charmap' codec can't decode byte %BYTE% in position %POSITION%: character maps to <undefined>

Image

image

Explanation

If a source file to be displayed contains ASCII encoded characters
qweqwe asdasd
everything is fine

If a source file to be displayed contains UTF8 encoded characters
őđčćž Ε ΔΔŒΔ†Ε½
error occurs.

Misc

  • Console is in UTF8 mode
  • System has "PYTHONIOENCODING=utf-8" environment variable set

Question

Can rich display UTF8 files?

Inconsistent header treatment for csv tables

Hello!

I'm sorry I'm not sure exactly what's going on here, so I'll get to it. Using Zsh:

$ rows=( Package,Version,Latest,Project 'tomli,2.0.0,2.0.1,~/Code/zpy' 'click,8.0.1,8.0.3,~/Code/archbuilder_iosevka' 'pep517,0.11.0,0.12.0,~/Code/archbuilder_iosevka' 'ruamel.yaml,0.17.17,0.17.21,~/Code/archbuilder_iosevka' 'tomli,1.2.1,2.0.1,~/Code/archbuilder_iosevka' )
$ rich --csv - <<<${(F)rows}

image

$ rows=( 'Package,Version,Latest,Project' 'tomli,2.0.0,2.0.1,~/Code/zpy' 'click,8.0.1,8.0.3,~/Code/archbuilder_iosevka' 'pep517,0.11.0,0.12.0,~/Code/archbuilder_iosevka' 'ruamel.yaml,0.17.17,0.17.21,~/Code/archbuilder_iosevka' 'tomli,1.2.1,2.0.1,~/Code/archbuilder_iosevka' )
$ rich --csv - <<<${(F)rows}

Same result as above

$ rows=( 'tomli,2.0.0,2.0.1,~/Code/zpy' 'click,8.0.1,8.0.3,~/Code/archbuilder_iosevka' 'pep517,0.11.0,0.12.0,~/Code/archbuilder_iosevka' 'ruamel.yaml,0.17.17,0.17.21,~/Code/archbuilder_iosevka' 'tomli,1.2.1,2.0.1,~/Code/archbuilder_iosevka' )
$ rich --csv - <<<${(F)rows}

image

What determines whether the first row gets treated as a header?

Thanks for any help!

`rich --print` always adds an extra newline

I'd expect rich --print to always preserve the lines of the input file. However rich --print always seems to add a newline. It would be great if no newlines were added during rich --print. I'd also recommend the addition of a -n flag that works like the same flag in echo to remove automatic line endings.

> echo -n | wc -l
       0
> echo | wc -l
       1
> echo -n | rich - --print | wc -l
       1
> echo | rich - --print | wc -l
       2
> echo -n | rich - --print --soft | wc -l
       1
> echo | rich - --print --soft | wc -l
       2

conda-forge package

FYI rich-cli is now available on conda-forge, it can be installed using conda or mamba:

mamba install -c conda-forge rich-cli

Let me know if you want to be added as a maintainer of this package, and I'll add you.

How to set background color inside strings should be documented better

I searched for a long time on how you should set the background color inside a string.

Setting the whole background using --style, e.g. --style="on yellow" is clearly documented.

But it took me experimentation to figure out that you can use the syntax [on yellow] inside strings. For example:

image

I think this should be documented better.

Thanks for a great tool!

Bails out

rich  ~/.emacs.d/config.org
unable to read /home/xircon/.emacs.d/config.org: no lexer for filename 'config.org' found

Org files are just text. Perhaps it should just "have a go" πŸ˜„

Rich as a drop-in replacement for "more"/"less"

This is a feature request, sort of.

I think that rich is awesome, and I intuitively wanted to use rich as a replacement of less, which I usually use to have a quick look at text files from my terminal.

However, plain rich behaves more like cat in that it prints all the stuff to the console and then exits. However - this is of course a personal opinion, but maybe the majority of rich users think the same way - I'd much prefer to have rich use --pager by default.

So my question is: Wouldn't it make sense to have rich use the pager by default when in an interactive terminal, and fall back to plain cat-behaviour when used e.g. in pipes or something?

Sure, I could solve this with an alias. But I really think that maybe this could be a useful default behaviour for the majority.

Thanks for the great work on rich and the other related toolkits! πŸš€

Unable to install latest textual and rich-cli together

Since rich-cli pins textual to 0.1.18 it's not possible to install Textual as suggested in the README:

$ pip install --index-url=https://pypi.org/simple "textual[dev]"
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: textual[dev] in ./.local/lib/python3.7/site-packages (0.1.18)
  WARNING: textual 0.1.18 does not provide the extra 'dev'
Requirement already satisfied: rich<13.0.0,>=12.3.0 in ./.local/lib/python3.7/site-packages (from textual[dev]) (12.6.0)
$ python -m textual
python: No module named textual.__main__; 'textual' is a package and cannot be directly executed

Reused option name: -j is used for both JSON and emoji options

The -j option name is listed for both the "Display as JSON" and "Enable emoji code" options in the code and documentation (including the splash screen image in the README). Click's option decorator silently allows one to overwrite the other. As a result, the JSON display option currently has no single-letter option name. To use it, one has to call --json.

Since the JSON option is older than the emoji option, I propose capitalizing the single-letter emoji option to -J. This would be a breaking change. I'll submit a PR for you to consider if this seems like a good approach.

RFE: Allow spacebar to advance pager

Every time I use --pager my fingers bang helplessly on the spacebar until I remember that I have to use pageup/pagedown, etc. Any chance you could allow the spacebar to advance the pager as well so that the behavior matches other common pagers?

Request: turn markdown tables into Rich tables

I wanted to show a nicely formatted visual table within my terminal for a screencast I'm recording. I thought "I could use Rich for that". But then I remembered that I already have a markdown table that represents exactly what I'd like to show, so I decided to try using rich-cli with my markdown file instead.

I tried rich -m markdown_file_with_table.md and it just showed all the | and - smooshed together.

Here's an example markdown table:

| Features    | rich | markdown | rich-cli |
|-------------|:----:|:--------:|:--------:|
| has tables  | x    | x        |          |
| easy to use | x    | x        | x        |

And output:

Features rich markdown rich-cli
has tables x x
easy to use x x x

Some themes not working on the Windows Terminal

Some --themes are not working on my Windows Terminal (using Windows 11) although they seem to work on WSL on the same machine.

Some of the themes not working include gruvbox-light, dracula, and friendly_grayscale (all taken from the pygments documentation here).

Below you can find a screenshot showing the Windows terminal on the left and WSL on the right, both running rich-cli 1.3.0:

  1. I ran rich without specifying a theme;
  2. I ran rich with --theme dracula and the Windows terminal renders it with the same styling as if I had invented a random theme name;
  3. I ran rich with another theme that both terminals seem to recognise, although the colours don't match 100% (which is fine).

_rich_cli_not_working

Running python -m rich.palette gives this output:

_rich_cli_not_working_palette

select/copy/find during --pager?

I've been trying rich --pager as a less pager replacement. I've realized that I can't select words while in that mode, which prevents copy. Is there a way to allow selecting of words? (and ideally some sort of find?)

RFE: page yo' self (`rich --help --pager`)

I'm getting old-man eyes and my font sizes are growing faster than my eyebrows

Could this:

rich --help --pager

display the same output as --help, but using the --pager?

Unable to read files with no lexer

When trying to load a file that does not have a lexer (without specifying a lexer override via -x), the file fails to load:

$ rich us1.env
unable to read us1.env: no lexer for filename 'us1.env' found

I would like to suggest a fallback to text lexer, perhaps with a warning message, instead of not displaying the file

Allow theme and lexer for RST and Markdown printing

As it stands currently, rich-cli prints the rst/markdown document without considering the lexer or theme argument

Markdown: https://github.com/Textualize/rich-cli/blob/main/src/rich_cli/__main__.py#L564
RST: https://github.com/Textualize/rich-cli/blob/main/src/rich_cli/__main__.py#L570

What I suggest is to get the --theme argument and pass it to the code_theme keyword argument of the Markdown and RestructuredText class, and optionally the --lexer argument to the default_lexer parameter of the RestructuredText class

I may open a PR later if @willmcgugan is intrested

Use file extension for CSV guessing instead of csv.Sniffer

Whenever I teach csv.Sniffer I always give a big caveat that it rarely works well on small CSV files.

For this file the delimiter is guessed at t (instead of ,)

Features,rich,markdown,rich-cli
has tables,x,x
easy to use,x,x,x

And for this one an error is shown because the delimiter can't be guesed:

Features,rich,markdown,rich-cli,GitHub
has tables,x,x,x
easy to use,x,x,x,x

I think the csv and tsv file extensions should be used (as a fallback at least) to assume comma-delimiting or tab-delimiting.

Is it possible to format outputs of commands executed?

For example, the mosquitto_sub command can print data received through MQTT. It has a built-in formatting option using the -F flag. But I would like to have a better looking output that would be possible with Rich CLI. What's the best way to use it?

I did the following and it worked so I assumed mosquitto would work too:

echo '{"test":"OK"}' | rich - --json
{
  "test": "OK"
}

I tried the following:
mosquitto_sub -h test.mosquitto.org -t "181818/json" | rich - --json but did not get any output.
Without | rich - --json I got this: {"test":"OK"}

No lexer fo csv

Hi all,

I can't seem to remedy this. I uninstalled/reinstalled and get the same thing. Any help would be greatly appreciated.

> rich try.csv
unable to read try.csv: no lexer for filename 'try.csv' found

Thanks!!

Some rst sections' content omitted from output

$ git clone https://github.com/pypa/setuptools
$ rich setuptools/docs/pkg_resources.rst

The last bunch of sections have headers but no content in the output, though content exists in the input.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.