f3 icon indicating copy to clipboard operation
f3 copied to clipboard

Add leading 0s to align columns

Open matthieu-vergne opened this issue 5 years ago • 3 comments

Hi,

I am just starting to use this tool on some SD cards. I think it could have a better display by adding leading zeros to the counter in file names.

Current state

Creating file 1.h2w ... OK!                           
Creating file 2.h2w ... OK!                           
Creating file 3.h2w ... OK!                           
Creating file 4.h2w ... OK!                           
Creating file 5.h2w ... OK!                           
Creating file 6.h2w ... OK!                           
Creating file 7.h2w ... OK!                           
Creating file 8.h2w ... OK!                           
Creating file 9.h2w ... OK!                           
Creating file 10.h2w ... OK!                           
Creating file 11.h2w ... OK!                           
Creating file 12.h2w ... OK!                           
Creating file 13.h2w ... OK!                           
Creating file 14.h2w ... OK!    

Expected state

Creating file 01.h2w ... OK!                           
Creating file 02.h2w ... OK!                           
Creating file 03.h2w ... OK!                           
Creating file 04.h2w ... OK!                           
Creating file 05.h2w ... OK!                           
Creating file 06.h2w ... OK!                           
Creating file 07.h2w ... OK!                           
Creating file 08.h2w ... OK!                           
Creating file 09.h2w ... OK!                           
Creating file 10.h2w ... OK!                           
Creating file 11.h2w ... OK!                           
Creating file 12.h2w ... OK!                           
Creating file 13.h2w ... OK!                           
Creating file 14.h2w ... OK!    

Motivations

The advantages would be small but appreciable:

  • the list of created files is more elegant because completely aligned
  • the list of read files is more elegant because completely aligned
  • it makes the report slightly easier to read because everything is aligned
  • the files on the drive are always properly ordered by name independently of the file system, since ASCII and natural ordering give the same order with leading zeros

I don't see any disadvantage beside the time spent programming it. I assume the additional computation is small enough to be irrelevant.

How to do it

I am not a C programmer, so I don't make a pull request, but I tried to figure out whether it was something easy to do before to suggest it. Since it seems so (see below), I think it would be cheap enough to motivate it.

I assume you can know the number of required digits through the decimal logarithm of the free space or something like that. So basically you could compute the number of required digits only once in this method, then pass it to this one and this one, then replace %li by %0xxli where xx is the number of digits received.

I did not touch C since years, so I hope it makes sense.

matthieu-vergne avatar Mar 16 '19 18:03 matthieu-vergne

Could you manually test if this would NOT break compatibility with H2testw? There is a number of users of f3write/f3read that do care about this compatibility.

AltraMayor avatar Mar 20 '19 00:03 AltraMayor

The change you are advocating isn't that big, but it's not so straighforward because ls_my_files() no longer can return an array of longs. ls_my_files() will need to return an array of strings and sort by numbers to avoid edge cases.

AltraMayor avatar Mar 20 '19 00:03 AltraMayor

Could you manually test if this would NOT break compatibility with H2testw? There is a number of users of f3write/f3read that do care about this compatibility.

I am not that familiar with other tools, like H2testw, so I don't know. I am not part of the people who would want such compatibility, I let you judge the relevance of such criteria.

What I do know, though, is that there is plenty of ways to deal with compatibility issues. You may do what you want and add an option "--h2testw-friendly" to enforce compatibility. Or in the contrary make it compatible by default and add options to customize the behaviour. So I don't think that compatibility would be an issue per se.

Of course I am aware that it takes time and dedication to do additional stuff. I would have made a PR if I had enough expertise in C.

The change you are advocating isn't that big, but it's not so straighforward because ls_my_files() no longer can return an array of longs. ls_my_files() will need to return an array of strings and sort by numbers to avoid edge cases.

I am not sure to understand why you should change something else.

Basically, your files are identified by the number in their name. So you have a mapping between a number and a file name. Whether or not you add zeros in the name does not change the fact that you need to map the very same number to the name of the very same file. All you should need is, when passing from the number to the file name, to add the right number of zeros and, when passing from the file name to the number, to remove them. This should, in theory, not impact the rest of your code in any way beside storing this fixed length.

In theory, it should impact 2 functions only (the functions passing from one to the other) and not the rest of your program.

  • If you want to go for the simplest one, not caring about any design question, then you don't even need to add parameters: just compute the size once, store it in a global, and use this global in the 2 functions. That should be the minimal change to make it work.
  • If you care about design and want to avoid globals, then numbers and names are ordered: you can just use two arrays in the same order and use one or the other depending on what you need.
  • You may prefer otherwise to make a map (1) (2).
  • If you prefer to consume processor rather than RAM, then you need your two functions to share the same state, which means create an object. In object-oriented programming, like C++ or Java, you may create an object with these 2 functions, which stores the length of the numbers and reuse it in both functions. Then you keep this object in your program to reuse it when you need to pass from one to the other. Since it is coded in C, you don't have objects, but you can use similar ways.

I have not dig in all your code, but if it is properly modularized, then you should only have the 2 functions to adapt, not the rest of the program. Then, choosing one or another design is just a matter of preference. If it happens that your code has more dependency, then maybe some refactoring would be useful. I help in other projects to improve design, but unfortunately I am a Java guy, so I can't help in C beside sharing theoretical stuff.

matthieu-vergne avatar Mar 20 '19 21:03 matthieu-vergne