Comments (5)
Hi @marslo
Your understanding of both of those is correct, yes.
For your issue with bat
displaying the file name as "STDIN", you can use the --file-name
argument in bat
to have it change the name in the header. Let me know if the modified code below works for you:
rg --vimgrep --with-filename 'TODO' --color never |
cut -d':' -f1 |
uniq |
xargs -r -I{} bash -c "sed -ne '/TODO:/,/^\s*$/p' {} |
bat -l groovy --file-name='{}'"
from bat.
I think having an extra opt-in command line flag to disable printing the header for binary and/or empty files would be fine. It would be especially useful when using bat
in bash scripts, like with what you're doing here.
It's nice to have a feature to setup header manually if content is from stdin to easier identify where the output comes from
The --file-name
option may be helpful here. It's even used to detect the language type :)
For your specific example of finding TODOs in source code files, ripgrep might be a more performant approach compared to fd
+sed
:
rg --vimgrep --with-filename 'TODO' | cut -d':' -f1 | uniq | xargs bat
There's also batgrep if you want a bit more of a tailored experience:
from bat.
Hi @eth-p ,
for me, it's just show ALL contents of files contains TODO
, for my understanding:
rg --vimgrep --with-filename 'TODO' | cut -d':' -f1 | uniq
: list all filepath containsTODO
xargs bat
: bat all contents
using sed -ne '/TODO:/,/^\s*$/p'
, means print lines between TODO
line to first empty line ^\s*$
, since almost of all TODO
is added before function, so using sed
will show whole function body
base on your example, following commands resolved STDIN <EMPTY>
issue:
rg --vimgrep --with-filename 'TODO' --color never |
cut -d':' -f1 |
uniq |
xargs -r -I{} bash -c "sed -ne '/TODO:/,/^\s*$/p' {} |
bat -l groovy"
However, it still didn't show the exact file name... i.e.:
![TODO](https://private-user-images.githubusercontent.com/5822057/322729307-bc543729-fa89-4619-b760-8a1116d1467a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgxNTMwNzgsIm5iZiI6MTcxODE1Mjc3OCwicGF0aCI6Ii81ODIyMDU3LzMyMjcyOTMwNy1iYzU0MzcyOS1mYTg5LTQ2MTktYjc2MC04YTExMTZkMTQ2N2EucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTJUMDAzOTM4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YjcwMWVhZGRmM2M0MDVkYTVjZGRkMzUzNjJmODc1Nzk3MjE0OWYxODk2Nzk3NDljYmZjNDE5YzVmMWRjMGM0MCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.YJvr37qU68sN0WafHed7T33dpXfMUVo_7iTGgzUaxT8)
here is my batconfig
$ cat ~/.config/bat/config | sed -r '/^(#.*)$/d' | sed -r '/^\s*$/d'
--theme="gruvbox-dark"
--style="numbers,changes,header"
--italic-text=always
--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"
--map-syntax "*.ino:C++"
--map-syntax ".ignore:Git Ignore"
--map-syntax='*.conf:INI'
--map-syntax='/etc/apache2/**/*.conf:Apache Conf'
--map-syntax "**/jenkinsfile/**/*:Groovy"
more on sed && sed+bat:
from bat.
ops... forgot to let you know, your solution works like charm, you can close this issue.
thanks again.
![image](https://private-user-images.githubusercontent.com/5822057/327589788-99775290-bf00-4e06-9bc1-96f2d28ba3e4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgxNTMwNzgsIm5iZiI6MTcxODE1Mjc3OCwicGF0aCI6Ii81ODIyMDU3LzMyNzU4OTc4OC05OTc3NTI5MC1iZjAwLTRlMDYtOWJjMS05NmYyZDI4YmEzZTQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTJUMDAzOTM4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ODgwZDM3YjFjOGNhNjk1NzFlNDkzNWIwNDVkNzM1NDRkMWMyOGUzY2Y3MTlmMDk3ZDZhMTIxOTVkNWUxMDk3NCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.jcCH53_QuGv2bcl2y02PqTXzxsYEDgvo4SkZxrfR9Aw)
from bat.
btw to show the correct line number in the file, it can be handled by nl {}
and without -l groovy
to let bat automatic use:
rg --vimgrep --with-filename 'TODO:' --color never |
cut -d':' -f1 |
uniq |
xargs -r -I{} bash -c "sed -ne '/TODO:/,/^\s*$/p' < <(nl {}) |
bat --style="grid,changes,header" --file-name='{}'"
Since we haven lot of files/dotfiles has not extension, so, to identify the language automatically, I'm using the following function ( a little bit ugly, but I don't how to handle via complex shell logic in xargs
, so using while read
instead of ):
function showTODO() {
local option='--style="grid,changes,header"'
while [[ $# -gt 0 ]]; do
case "$1" in
-p | --plain ) option+=" $1" ; shift 1 ;;
-* ) option+=" $1 $2" ; shift 2 ;;
esac
done
rg --vimgrep --with-filename 'TODO:' --color never |
cut -d':' -f1 |
uniq |
while read -r _file; do
# identify language automatically
local lang='';
lang="$(sed -r 's/^.+\.(.+)$/\1/' <<< "${_file}")";
if ! bat --list-languages | command grep -iE -q "[,:]${lang}|${lang},"; then
# check shebang and reset to empty if shebang not found
lang=$(sed -rn 's/^#!.*[/\ ](\w+)$/\1/p' < <(head -n1 "${_file}"));
fi
sed -ne '/TODO:/,/^\s*$/p' < <(nl "${_file}") |
eval "bat -l ${lang:-groovy} ${option} --file-name=\"${_file}\"" ;
done
if bat can detect filetype via shebang
line ( i.e.: #!/usr/bin/env bash
or #!/bin/bash
, via ^#!.*[/\ ](\w+)$
), then the simple version will be the best answer.
![Screenshot 2024-05-02 at 16 36 43](https://private-user-images.githubusercontent.com/5822057/327613797-e69bff29-b7cf-45e4-90db-6df0ebd0f750.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgxNTMwNzgsIm5iZiI6MTcxODE1Mjc3OCwicGF0aCI6Ii81ODIyMDU3LzMyNzYxMzc5Ny1lNjliZmYyOS1iN2NmLTQ1ZTQtOTBkYi02ZGYwZWJkMGY3NTAucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTJUMDAzOTM4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NzI3ZDU3NmRjNjgxZjRkNDM5NGFkNWVkNzhiMDQyZjM1YjRjMWI2M2QzM2I3NzExMTMyNzZjYzlhNGU4Zjc2YiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.vuo49eTFnKb3TlXGjL05jGwkL5532t8MRBv9Qz3ZTJ4)
btw, just curious why bat
will using _fzf_path_completion
as completion function by default.
from bat.
Related Issues (20)
- bat - Apple Silicon Support HOT 1
- Update README to include GNOME/GTK version of Dark Mode macOS alias. PR:#2963 HOT 1
- Using `batcat --pager "less -R" </path_to_file>` doesen't convert ANSI characters in Kali HOT 1
- Syntax Request: Astro HOT 3
- Maybe patch the newest lisp syntaxes? HOT 1
- Support Font Ligatures HOT 8
- Add Flag to Avoid Treating NUL Separated Input as Binary HOT 11
- [bat error]: 'REPLY': No such file or directory (os error 2) HOT 1
- no license for bundled github-sublime-theme
- no license for bundled sublime-snazzy theme HOT 1
- no license for bundled visual-studio-dark-plus theme
- no license text for bundled Nord-sublime theme HOT 1
- mis-behaviour of `--style=plain` while itegrating with `fzf` HOT 2
- musl asset: inconsistent between output of `file` and `ldd` HOT 2
- Request for a 'statically compiled binary' for aarch64 HOT 5
- Unable to compile bat on Windows from Git HOT 2
- Support ignoring the system-wide configuration file HOT 4
- Format of the man page of `bat` (`assets/manual/bat.1.in`) HOT 1
- (Feature request) Add an option to disable the line numbers HOT 2
- Failure to read config in MSYS2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bat.