This reminds me of a general muse 'it would be nice if...' thought: it 
would be nice if there was a way of defining more featureful dialogs, so 
that in this case for instance, you could browse for a file (or put a 
pathname in), and also add a file offset, or something like that.

I am aware that such a thing would tend to suffer from featuritis, however.

    J^n


On Tuesday, June 11, 2024 at 12:53:51 AM UTC+1 [email protected] wrote:

> Sometimes it is useful to view the first N bytes of a file without reading 
> the whole file.  The file might be much too large to load into a text 
> editor or a Leo node, for example. The Linux command *head* will show you 
> just the start of a file, but Windows doesn't have that command out of the 
> box.  Often it's better to look at the hex bytes rather than the text, 
> especially if the file is binary.  Say, for example, that you want to see 
> if a file is a zip file or not, or if the EXIF data is embedded in a .jpg 
> picture file.
>
> Here is a Leo command that loads a file using the file dialog.  It creates 
> a new node at the end of your outline and fills it with a classic hex view 
> of the first 4096 bytes.
>
> I have this script as an *@command* node in my myLeoSettings.leo file, 
> and I also have a custom menu item for it.  I don't use it all the time but 
> it's invaluable when it is needed.
>
> Here is the script:
>
> """Display as hex the first 4096 bytes of a file in body of a new node."""
>
> data = ''
>
> def format_bytes(bytes):
>     """Format byte data into classic hex bytes display."""
>     text = ''
>     asc = ' ' * 3  # For translation of bytes to ascii characters
>     n = 0
>     for i, b in enumerate(bytes):
>         text += f'{b:02x}'
>         asc += chr(b) if b > 0x1F else '.'
>         if i > 0 and i % 10 == 9:
>             # Complete the current line
>             text += asc
>             text += '\n'
>             asc = ' ' * 3
>             n += 1
>             if n % 8 == 0:
>                 # Insert a blank line for readability
>                 text += '\n'
>         else:
>             text += ' '
>     return text
>
> filetypes = [('Any', '*.*'),]
> path = g.app.gui.runOpenFileDialog(c, 'Choose File To Sample', 
> filetypes=filetypes)
> g.es(path)
>
> if path:
>     try:
>         with open(path, 'rb') as f:
>             data = f.read(4096)
>     except IOError as e:
>         g.es(e)
>
> if data:
>     # Create and select target node
>     p_last = c.lastVisible()
>     target = p_last.insertAfter()
>     target.h = f'first bytes of {path}'
>     target.b = f'{format_bytes(data)}'
>     target.setDirty()
>     c.selectPosition(target)
>     while c.canMoveOutlineLeft():
>         c.moveOutlineLeft()
>     c.redraw()
>
> Here is (just the first part) of the bytes of an image file of mine.  We 
> can see the signature of a .jpg file. and that it uses the *sRGB* color 
> map.
>
> ff d8 ff e0 00 10 4a 46 49 46   ÿØÿà..JFIF
> 00 01 01 00 00 01 00 01 00 00   ..........
> ff e2 0c 58 49 43 43 5f 50 52   ÿâ.XICC_PR
> 4f 46 49 4c 45 00 01 01 00 00   OFILE.....
> 0c 48 6c 63 6d 73 02 10 00 00   .Hlcms....
> 6d 6e 74 72 52 47 42 20 58 59   mntrRGB XY
> 5a 20 07 ce 00 02 00 09 00 06   Z .Î......
> 00 31 00 00 61 63 73 70 4d 53   .1..acspMS
>
> 46 54 00 00 00 00 49 45 43 20   FT....IEC 
> 73 52 47 42 00 00 00 00 00 00   sRGB......
> 00 00 00 00 00 00 00 00 f6 d6   ........öÖ
> 00 01 00 00 00 00 d3 2d 6c 63   ......Ó-lc
> 6d 73 00 00 00 00 00 00 00 00   ms........
> 00 00 00 00 00 00 00 00 00 00   ..........
> 00 00 00 00 00 00 00 00 00 00   ..........
> 00 00 00 00 00 00 00 00 00 00   ..........
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/5c3a21fa-2fc5-4001-bec3-c33178f91448n%40googlegroups.com.

Reply via email to