iinsertNameHere / catnap

A highly customizable systemfetch written in nim
https://catnap-fetch.xyz
MIT License
127 stars 8 forks source link

[Suggestions]: General suggestion for improvements #132

Open DrunkenAlcoholic opened 1 month ago

DrunkenAlcoholic commented 1 month ago

Excellent job on catnap, I have a few suggestions based on fetch utilities I have writen in both Pascal and Nim languages, I am also self taught, so I am no means giving you expert advice, and I am still very much a Noob when it comes to Nim, Saying that... I know enough to be able to suggest improvement to speed up catnap, in fact I have written a fetch in Nim to test a few things.


* The used memory is debatable in how its being calculated, while the implementation you have used is pretty much acceptable, htop and free use something like "iUsedMem = iMemTotal - (iMemFree + iBuffers + iCached) + (iShmem - iSReclaimable)" and yes I have  Nim if you wanted to calculate the same way. 

* Compiling flags, I noticed a good amount of improvements just adding a few flags to the compiler my current command for compiling Nim projects is 
```Nim
nim c  --passC:-flto --checks:off --panics:on --mm:arc --threads:on --opt:speed -d:danger src/nymph.nim

It probably doesn't need threads flag, as I haven't actually used multi-threading in the code, but I could improve my current 2ms execution time(with png image) down even further

I did upload my code as an example fetch utility, in case you did want to get some ideas from that. Saying that, these are just some random suggestions and ideas, if anything it may trigger something new for you to implement into catnap, if not then just ignore and delete this thread. https://github.com/DrunkenAlcoholic/Nymph

iinsertNameHere commented 1 month ago

Hey, thx for you feedback! I was actually planing to get rid of VUI for some time. Thx for the reference to tpix! The optimizations are also worth taking a look at.

iinsertNameHere commented 1 month ago
DrunkenAlcoholic commented 1 month ago

here is an example how the kitty protocol works to display png image, First prefix starts with escape code, the "_G" then you can add your key/value pairs, I have hard coded it "a=T" which tells the kitty protocol that we are transmitting, then the "f=100" is the type we are transmitting, in this case 100 = png then the payload which is the png encode to base64 string, then the escape code with "\" to end. you also need to send it in chunks of 4096 chars for the image, to let the kitty protocol know you set m=1 as part of the prefix that there are more to come, then on your last chunk set m=0. see the code below, also go and read the kitty graphics protocol https://sw.kovidgoyal.net/kitty/graphics-protocol/. this is just to display the image, you might want to also look at resizing the image before encoding it to base64, or do as I did in my example Nim fetch, hard code the logo as a base64 string into the program, but this will limit user being able to resize the image.

import base64, os

const 
  ESC = "\x1b"
  GrCmd = "a=T,f=100"

proc writeChunk(chunk: string, last: bool) =
  let payload = ESC & "_G" & GrCmd & ",m=" & (if last: "0" else: "1") & ";" & chunk & ESC & "\\"
  stdout.write payload
  stdout.flushFile()

proc main() =
  if paramCount() < 1:
    quit "Usage: " & getAppFilename() & " <png_file>"

  let 
    data = readFile(paramStr(1))
    encoded = base64.encode(data)

  for i in countup(0, encoded.high, 4096):
    let 
      chunk = encoded[i .. min(i + 4095, encoded.high)]
      last = i + 4096 > encoded.high
    writeChunk(chunk, last)

when isMainModule:
  main()
yuki-was-taken commented 1 month ago

I'm not sure if enabling the dangerous flag and disabling checks for compilation is good for normal use. The rest of the flags look alright though.

DrunkenAlcoholic commented 1 month ago

The assumption is you would have already compiled it at least once in a "debug" mode to know there are no issues, how ever the minimum you should compile it with is the "release" flag to turn on optimizations, "danger" does the same thing as "release" except it also turns off runtime checks. but valid points, if you feel having the debug data as part of the binary is useful for the user, then by all means keep it in, but don't expect fast execution times.