Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash comments formatter #62

Merged
merged 9 commits into from
Mar 17, 2023
Merged

Add bash comments formatter #62

merged 9 commits into from
Mar 17, 2023

Conversation

ctmbl
Copy link
Contributor

@ctmbl ctmbl commented Dec 14, 2022

Really this isn't a need to this repo, but I wrote this python script for bump.sh and found it quite cool so we can use it if you'd like 👍

PS: sorry for the one commit PR 🥲

EDIT:
Actually I though the title was explicit enough but it isn't so here is what the script does.
It's a simple python script with a shebang to execute it directly.
It needs a file path as only argument and will check that it exists and isn't a directory or symlink.
This file should be a bash (or whatever other script that use # comments) and my script will look for # - beginning lines (I use such comments to separate bash script sections) to format them:

  • 80 character lines (configurable)
  • center the section text
  • ask the user to confirm for each formatting

@ctmbl ctmbl added enhancement New feature or request Priority: Unknown The priority of this issue is still unknown labels Dec 14, 2022
@ctmbl ctmbl requested review from gbrivady, amtoine and vSt11 December 14, 2022 00:32
@ctmbl ctmbl self-assigned this Dec 14, 2022
@amtoine
Copy link
Member

amtoine commented Dec 14, 2022

a one-commit PR is fine 😉

@ctmbl, could you please give a quick use case of that script to understand what it does? 😋

@ctmbl
Copy link
Contributor Author

ctmbl commented Dec 14, 2022

@amtoine That's right sorry I didn't explain the purpose! see my first message edit

amtoine
amtoine previously approved these changes Dec 15, 2022
Copy link
Member

@amtoine amtoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amtoine That's right sorry I didn't explain the purpose! see my first message edit

thanks a lot @ctmbl 😮

this script is quite cool then 🤩
i was able to transform

#!/usr/bin/env bash
# - my first section
echo "first section"


# - my second section
echo "second section"


# - a very long section title that will span the whole line
echo "very long section"

# - my last section
echo "last section"

into

#!/usr/bin/env bash
# ------------------------------ my first section ------------------------------
echo "first section"


# ----------------------------- my second section ------------------------------
echo "second section"


# ---------- a very long section title that will span the whole line -----------
echo "very long section"

# ------------------------------ my last section -------------------------------
echo "last section"

with a simple

> ./scripts/bash-comments-lint.py foo.sh

really neat 👌

comments

  • as said in the threads, i think a little bit of argparse or equivalent would be nice.
    i like using argparse in all my python scripts, even if it's only for --help 😉
  • also in the threads, adding options to bypass the confirmations and change the line length would be appreciated
  • maybe your script is a little bit hard to read, not a lot of comments and a lot of regex / string manipulations going on

but in the end, the script does the job very nicely so, if you feel these are out of scope, i can open an issue for the points above and move the requested changes to another PR 😋

scripts/bash-comments-lint.py Outdated Show resolved Hide resolved
scripts/bash-comments-lint.py Outdated Show resolved Hide resolved
@ctmbl
Copy link
Contributor Author

ctmbl commented Dec 20, 2022

@amtoine I just implemented what you suggested and it looks pretty good!
Don't hesitate to point out anything that can be improve according to you 😋

amtoine
amtoine previously approved these changes Dec 20, 2022
Copy link
Member

@amtoine amtoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's kinda cool 👌

i think a --auto flag would be cooool to compute the perfect witdth of a section title automatically based on the length of the comments 🤩
for instance, the following nushell command on the script i talk about in my first review gives 55, the length of the longest comment 💪

open foo.sh
| lines
| find --regex "^# -"
| str replace "^# -* " ""
| str replace " -*$" ""
| each {split chars | length}
| math max

that's just an idead of course 😉

@ctmbl
Copy link
Contributor Author

ctmbl commented Dec 20, 2022

i think a --auto flag would be cooool to compute the perfect witdth of a section title automatically based on the length of the comments star_struck

I see but how to compute this perfect length? 🤔
knowing that the longest comment is 55 doesn't say how long the section should be 😕
Also this length is more about convention (I was thinking of python PEP8 when I chose 80) than anything else, if a comment is too long maybe its the comment fault not the length? but maybe I'm missing something?

@amtoine
Copy link
Member

amtoine commented Dec 20, 2022

I see but how to compute this perfect length? thinking knowing that the longest comment is 55 doesn't say how long the section should be confused Also this length is more about convention (I was thinking of python PEP8 when I chose 80) than anything else, if a comment is too long maybe its the comment fault not the length? but maybe I'm missing something?

i do not know exactly, this was just an idea, quite out of the scope of this PR 😉
the script is fine for now, maybe i'll open an issue some day if i have a clearer idea 😋

gbrivady
gbrivady previously approved these changes Dec 21, 2022
Copy link
Member

@gbrivady gbrivady left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly a bash expert, but the Python stuff looks fine to me

@amtoine amtoine requested a review from atxr December 21, 2022 18:38
@ctmbl
Copy link
Contributor Author

ctmbl commented Jan 6, 2023

the script is fine for now, maybe i'll open an issue some day if i have a clearer idea yum

That seems right to me!

@ctmbl ctmbl added Priority: Low The Issue will be address only if other tasks are done and removed Priority: Unknown The priority of this issue is still unknown labels Jan 6, 2023
@amtoine
Copy link
Member

amtoine commented Jan 7, 2023

@atxr or @vSt11,
we would need a review from one of you two to merge this 😉

atxr
atxr previously approved these changes Mar 15, 2023
Copy link
Contributor

@atxr atxr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script is a nice idea!
I'll probably use it for other projects :)
🚀

@ctmbl
Copy link
Contributor Author

ctmbl commented Mar 15, 2023

tks ! 🚀

@ctmbl ctmbl requested a review from Laudut March 15, 2023 17:18
Laudut
Laudut previously approved these changes Mar 15, 2023
Copy link
Contributor

@Laudut Laudut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whow ! Nice Job !

Maybe 2 points :

  • You delete most of special characters in formatting ( &"'(-)=°+? ). So "# - essential ??? " become "# --- essential ----".
  • In line 42 you use again a "new_line.replace( ... )" and recalculates something you had already calculated in line 30

@ctmbl
Copy link
Contributor Author

ctmbl commented Mar 16, 2023

@Laudut

Thanks for the review! actually both of your remarks are really relevant, let me answer them:

You delete most of special characters in formatting ( &"'(-)=°+? ). So "# - essential ??? " become "# --- essential ----".

That's true, but given that this script is supposed to format Sections comments I guess you don't use these characters in sections?

In line 42 you use again a "new_line.replace( ... )" and recalculates something you had already calculated in line 30

ooops 🙃 surely I was too lazy to do things properly!
I'll modify it given that I noted yesterday that the script also formats lines that are... already formatted, so I'll do a quick patch for both of these issues!

Once again thanks for this great review!

@ctmbl ctmbl dismissed stale reviews from Laudut, atxr, gbrivady, and amtoine via 1fcd166 March 16, 2023 13:10
@ctmbl
Copy link
Contributor Author

ctmbl commented Mar 16, 2023

@Laudut here is the patch! 😉

Copy link

@Elymme Elymme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. More comments to explain everything would suit me better but that's fine.
Adding the expected type and/or format of the arguments before a function would be nice though

@ctmbl
Copy link
Contributor Author

ctmbl commented Mar 16, 2023

@Elymme tks for the review!

What do you mean by " Adding the expected type and/or format of the arguments before a function would be nice though" do you mean like

def f(a: int, s: str, o: SomeObject):
  pass

?

@Elymme
Copy link

Elymme commented Mar 17, 2023

@ctmbl Yeah exactly that. I think it improves clarity.

@ctmbl
Copy link
Contributor Author

ctmbl commented Mar 17, 2023

@Elymme actually you're right, and my choice of variable name is really poor too.
However because the script is really short and that it has really only one function I'll leave it that way, if we were to improve it something sure it will be one of the main goal!

@ctmbl ctmbl merged commit 2013e8e into iScsc:main Mar 17, 2023
@ctmbl ctmbl deleted the bash-comments-linter branch March 17, 2023 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Priority: Low The Issue will be address only if other tasks are done
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants