Show sloc count on file pages #4110

Open
opened 2025-11-02 05:38:34 -06:00 by GiteaMirror · 18 comments
Owner

Originally created by @boyter on GitHub (Oct 14, 2019).

Description

Showing the SLoC (source lines of code) count for pages similar to how GitHub does would be very useful. This is in addition to the existing LoC (lines of code) count the same at GitHub.

Doing this in an accurate way is possible using https://github.com/boyter/scc/ and since it counts the LoC and SLoC in a single pass there is no performance impact. In fact since gitea currently does a strings.Split on newlines to get this metric it should be considerably faster.

I tried a quick implementation of this and it was reasonably simple to add the above functionality (see screenshots) but you could get additional count's of comments and blank lines as well. All I had to do was import the library, modify routers/repo/view.go, templates/repo/view_file.tmpl and options/locale/locale_en-US.ini to get this going. Although I suspect sloc is something that does not need to be internationalised(?)

Id be happy to assist with this and get it to PR status. I think it would be a good addition into gitea and brings its functionality into line with github. The code may also be useful to assist with guessing languages, but I have no looked at how that currently works so cannot comment in depth.

Screenshots

Example of how this looks in github. Note github gets the line count wrong because of how it deals with newlines.

image

The below is a quick example I put together using gitea and scc, please note that the line count matches the existing functionality, but includes the SLoC.

image

Originally created by @boyter on GitHub (Oct 14, 2019). ## Description Showing the SLoC (source lines of code) count for pages similar to how GitHub does would be very useful. This is in addition to the existing LoC (lines of code) count the same at GitHub. Doing this in an accurate way is possible using https://github.com/boyter/scc/ and since it counts the LoC and SLoC in a single pass there is no performance impact. In fact since gitea currently does a `strings.Split` on newlines to get this metric it should be considerably faster. I tried a quick implementation of this and it was reasonably simple to add the above functionality (see screenshots) but you could get additional count's of comments and blank lines as well. All I had to do was import the library, modify `routers/repo/view.go`, `templates/repo/view_file.tmpl` and `options/locale/locale_en-US.ini` to get this going. Although I suspect sloc is something that does not need to be internationalised(?) Id be happy to assist with this and get it to PR status. I think it would be a good addition into gitea and brings its functionality into line with github. The code may also be useful to assist with guessing languages, but I have no looked at how that currently works so cannot comment in depth. ## Screenshots Example of how this looks in github. Note github gets the line count wrong because of how it deals with newlines. ![image](https://user-images.githubusercontent.com/612151/66726565-e44fb200-ee85-11e9-99f1-76e8eaf06a5c.png) The below is a quick example I put together using gitea and scc, please note that the line count matches the existing functionality, but includes the SLoC. ![image](https://user-images.githubusercontent.com/612151/66726698-a69f5900-ee86-11e9-851b-d022b985f490.png)
GiteaMirror added the type/proposaltype/enhancement labels 2025-11-02 05:38:34 -06:00
Author
Owner

@lunny commented on GitHub (Oct 14, 2019):

@boyter Could you send a PR to fix this?

@lunny commented on GitHub (Oct 14, 2019): @boyter Could you send a PR to fix this?
Author
Owner

@boyter commented on GitHub (Oct 14, 2019):

Yes. I already have done 90% of the work. The only catch is that I could not see any way to get the name of the language. No idea if that is in there somewhere, but running it though the one in scc is no great chore.

Ill clean it up and send a PR.

@boyter commented on GitHub (Oct 14, 2019): Yes. I already have done 90% of the work. The only catch is that I could not see any way to get the name of the language. No idea if that is in there somewhere, but running it though the one in `scc` is no great chore. Ill clean it up and send a PR.
Author
Owner

@stale[bot] commented on GitHub (Dec 13, 2019):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 weeks. Thank you for your contributions.

@stale[bot] commented on GitHub (Dec 13, 2019): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 weeks. Thank you for your contributions.
Author
Owner

@boyter commented on GitHub (Jan 5, 2020):

Ah the joy of talking a long break. Everything you were working on gets closed :)

Ill start work on this again in a few days.

@boyter commented on GitHub (Jan 5, 2020): Ah the joy of talking a long break. Everything you were working on gets closed :) Ill start work on this again in a few days.
Author
Owner

@nuno-silva commented on GitHub (Dec 29, 2021):

friendly ping on this :)

@nuno-silva commented on GitHub (Dec 29, 2021): friendly ping on this :)
Author
Owner

@wxiaoguang commented on GitHub (Apr 25, 2023):

Oops, sorry for wrong close

@wxiaoguang commented on GitHub (Apr 25, 2023): Oops, sorry for wrong close
Author
Owner

@yardenshoham commented on GitHub (Apr 29, 2023):

Is sloc just loc minus number of empty lines?

@yardenshoham commented on GitHub (Apr 29, 2023): Is `sloc` just `loc` minus number of empty lines?
Author
Owner

@wxiaoguang commented on GitHub (Apr 29, 2023):

https://github.com/wxiaoguang/playground/blob/main/test-highlight.cpp

69-line comments, 69 sloc.

But I don't understand whether it is helpful to end users (why people should care about it and how could it help readers).
I never paid attention on the sloc ....

@wxiaoguang commented on GitHub (Apr 29, 2023): https://github.com/wxiaoguang/playground/blob/main/test-highlight.cpp 69-line comments, 69 sloc. But I don't understand whether it is helpful to end users (why people should care about it and how could it help readers). I never paid attention on the sloc ....
Author
Owner

@boyter commented on GitHub (Apr 30, 2023):

I guess it can be helpful to judge if a file is worthy of close inspection.

Its just another data point. However if you are going to include it you should attempt to have it as accurate as possible.

@boyter commented on GitHub (Apr 30, 2023): I guess it can be helpful to judge if a file is worthy of close inspection. Its just another data point. However if you are going to include it you should attempt to have it as accurate as possible.
Author
Owner

@wxiaoguang commented on GitHub (Apr 30, 2023):

Could you elaborate about how to "judge if a file is worthy of close inspection"? It's better to show some real cases. Personally I can't imagine a case how the SLoC works to help ..... because IMO people seldom fill a file with plenty of new lines, then SLoC roughly equals to LoC, the same magnitude.

@wxiaoguang commented on GitHub (Apr 30, 2023): Could you elaborate about how to "judge if a file is worthy of close inspection"? It's better to show some real cases. Personally I can't imagine a case how the SLoC works to help ..... because IMO people seldom fill a file with plenty of new lines, then SLoC roughly equals to LoC, the same magnitude.
Author
Owner

@boyter commented on GitHub (May 1, 2023):

Could you elaborate about how to "judge if a file is worthy of close inspection"? It's better to show some real cases. Personally I can't imagine a case how the SLoC works to help ..... because IMO people seldom fill a file with plenty of new lines, then SLoC roughly equals to LoC, the same magnitude.

You are of course welcome to your opinion. My point remains however, if you are going to display sloc, it should be as accurate as you can get it since some people do get value out of it.

@boyter commented on GitHub (May 1, 2023): > Could you elaborate about how to "judge if a file is worthy of close inspection"? It's better to show some real cases. Personally I can't imagine a case how the SLoC works to help ..... because IMO people seldom fill a file with plenty of new lines, then SLoC roughly equals to LoC, the same magnitude. You are of course welcome to your opinion. My point remains however, if you are going to display sloc, it should be as accurate as you can get it since some people do get value out of it.
Author
Owner

@wxiaoguang commented on GitHub (May 1, 2023):

I just like to learn from real cases, without a real case, it's difficult to understand the real world.

@wxiaoguang commented on GitHub (May 1, 2023): I just like to learn from real cases, without a real case, it's difficult to understand the real world.
Author
Owner

@boyter commented on GitHub (May 1, 2023):

@wxiaoguang I personally use it as another data point.

Ideally if you have it with the count of lines/comments/code/blanks you actually get more information. I think github displays the sloc because it gives an idea of how dense the file is. For example a file with lots of comments should have a lower sloc. Or a file with sloc matching the number of lines indicates a very dense file.

Its actually more useful if you can see it in a list, as you can help pinpoint files that might be worth refactoring or investigating compared to others in a codebase.

@boyter commented on GitHub (May 1, 2023): @wxiaoguang I personally use it as another data point. Ideally if you have it with the count of lines/comments/code/blanks you actually get more information. I think github displays the sloc because it gives an idea of how dense the file is. For example a file with lots of comments should have a lower sloc. Or a file with sloc matching the number of lines indicates a very dense file. Its actually more useful if you can see it in a list, as you can help pinpoint files that might be worth refactoring or investigating compared to others in a codebase.
Author
Owner

@wxiaoguang commented on GitHub (May 1, 2023):

I think github displays the sloc because it gives an idea of how dense the file is. For example a file with lots of comments should have a lower sloc.

Nope, see my test: https://github.com/wxiaoguang/playground/blob/main/test-highlight.cpp : 69-line comments, 69 sloc.

Its actually more useful if you can see it in a list,

Since the "view file" is not a list page, so I can't get the point how it helps ....

@wxiaoguang commented on GitHub (May 1, 2023): > I think github displays the sloc because it gives an idea of how dense the file is. For example a file with lots of comments should have a lower sloc. Nope, see my test: https://github.com/wxiaoguang/playground/blob/main/test-highlight.cpp : 69-line comments, 69 sloc. > Its actually more useful if you can see it in a list, Since the "view file" is not a list page, so I can't get the point how it helps ....
Author
Owner

@boyter commented on GitHub (May 1, 2023):

@wxiaoguang That's just an indicator of poor implementation by github. It would be far more useful for it to show the code count which is 0 for that case. You could then know without needing to scroll on a large file if there was any point to it.

@boyter commented on GitHub (May 1, 2023): @wxiaoguang That's just an indicator of poor implementation by github. It would be far more useful for it to show the code count which is 0 for that case. You could then know without needing to scroll on a large file if there was any point to it.
Author
Owner

@wxiaoguang commented on GitHub (May 1, 2023):

That's just an indicator of poor implementation by GitHub.

Actually, there is no standard definition for SLoC, or LoC, I do not think GitHub's implementation is poor.

https://en.wikipedia.org/wiki/Source_lines_of_code

It would be far more useful for it to show the code count which is 0 for that case.

There are a lot of arguments about how to count SLoC, eg: should comment be counted? http://www.projectcodemeter.com/cost_estimation/help/GL_sloc.htm

And some articles just suggest to include comments: https://pvs-studio.com/en/blog/terms/0086/

And stackoverflow says : https://stackoverflow.com/questions/3769716/how-bad-is-sloc-source-lines-of-code-as-a-metric

Especially, for many languages, the comment does its work, for example, PHP (annotation), and even Golang: https://cs.opensource.google/go/x/exp/+/master:ebnflint/doc.go (and including: //go:build , //go:generate)


So, IMO, in most cases, the SLoC is just for fun, only used for large projects to know "how large it is": millions SLoC, etc. It doesn't bring real benefit for code viewing or review.

@wxiaoguang commented on GitHub (May 1, 2023): > That's just an indicator of poor implementation by GitHub. Actually, there is no standard definition for SLoC, or LoC, I do not think GitHub's implementation is poor. https://en.wikipedia.org/wiki/Source_lines_of_code > It would be far more useful for it to show the code count which is 0 for that case. There are a lot of arguments about how to count SLoC, eg: should comment be counted? http://www.projectcodemeter.com/cost_estimation/help/GL_sloc.htm And some articles just suggest to include comments: https://pvs-studio.com/en/blog/terms/0086/ And stackoverflow says : https://stackoverflow.com/questions/3769716/how-bad-is-sloc-source-lines-of-code-as-a-metric Especially, for many languages, the comment does its work, for example, PHP (annotation), and even Golang: https://cs.opensource.google/go/x/exp/+/master:ebnflint/doc.go (and including: `//go:build` , `//go:generate`) ---- So, IMO, in most cases, the SLoC is just for fun, only used for large projects to know "how large it is": millions SLoC, etc. It doesn't bring real benefit for code viewing or review.
Author
Owner

@boyter commented on GitHub (May 1, 2023):

@wxiaoguang I disagree with your assessment but your opinion is just as valid as anyone else's.

@boyter commented on GitHub (May 1, 2023): @wxiaoguang I disagree with your assessment but your opinion is just as valid as anyone else's.
Author
Owner

@GiteaBot commented on GitHub (Sep 8, 2023):

We close issues that need feedback from the author if there were no new comments for a month. 🍵

@GiteaBot commented on GitHub (Sep 8, 2023): We close issues that need feedback from the author if there were no new comments for a month. :tea:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#4110