GTKDialog: Refresh progressbar

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
johnmcenroy
Posts: 2
Joined: Fri 26 May 2017, 17:09

GTKDialog: Refresh progressbar

#1 Post by johnmcenroy »

Hello
Similar part of this code is in my little utility. How can progressbar be
updated when using function format32 ?
Thanks.

Code: Select all

function format32 ()
 {
  mkfs.vfat -F32 -n USB -v /dev/sdb1
 }


MAIN_DIALOG='
<window title="ISO2USB" icon-name="gtk3-demo" resizable="false" decorated="true" width-request="500" height-request="750"> 
<vbox>
<hbox height-request="100">                       
  <frame>
   <progressbar>
    <label>Writing ...</label>
    <input> </input>
   </progressbar>
  </frame>
 </hbox>
</vbox

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

Re: GTKDialog: Refresh progressbar

#2 Post by misko_2083 »

johnmcenroy wrote:Hello
Similar part of this code is in my little utility. How can progressbar be
updated when using function format32 ?
Thanks.

Code: Select all

function format32 ()
 {
  mkfs.vfat -F32 -n USB -v /dev/sdb1
 }


MAIN_DIALOG='
<window title="ISO2USB" icon-name="gtk3-demo" resizable="false" decorated="true" width-request="500" height-request="750"> 
<vbox>
<hbox height-request="100">                       
  <frame>
   <progressbar>
    <label>Writing ...</label>
    <input> </input>
   </progressbar>
  </frame>
 </hbox>
</vbox
Probably like next.
It's pretty much accurate, not 100% but very close.

Code: Select all

#!/bin/bash

usb_w()
{
strace -q -ewrite mkfs.vfat -F32 -n USB /dev/sdb1 2>&1 | awk '{
        count += $NF
            if (count % 10 == 0) {
               percent = count / total_size * 100
               printf "%3d\n", percent
               printf "Formating... "
               printf "%3d%%\n", percent
            }
         }
         END { print "" }' total_size=$(fdisk -l /dev/sdb1 | awk '/Disk.*sectors/{print $7}') count=0
echo done
}
export -f usb_w

export MAIN_DIALOG='
<vbox>
  <frame Progress>
    <text>
      <label>Formating /dev/sdb1</label>
    </text>
    <progressbar>
      <label>Formating sdb1</label>
      <input>/bin/bash -c usb_w</input>
      <action type="exit">Ready</action>
    </progressbar>
  </frame>
  <hbox>
   <button cancel></button>
  </hbox>
 </vbox>
'

gtkdialog --program=MAIN_DIALOG
Be carefull with what you are formating!
I wiped the the wrong partition with dd once. :(

johnmcenroy
Posts: 2
Joined: Fri 26 May 2017, 17:09

#3 Post by johnmcenroy »

Thank you very much misko_2083. And can progressbar be updated by pushing button to activate function usb__w() ?
Button > usb_w > progressbar. Thanks.
#!/bin/bash

usb_w()
{
strace -q -ewrite mkfs.vfat -F32 -n USB /dev/sdb1 2>&1 | awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d\n", percent
printf "Formating... "
printf "%3d%%\n", percent
}
}
END { print "" }' total_size=$(fdisk -l /dev/sdb1 | awk '/Disk.*sectors/{print $7}') count=0
echo done
}
export -f usb_w

export MAIN_DIALOG='
<vbox>
<frame Progress>
<text>
<label>Formating /dev/sdb1</label>
</text>
<progressbar>
<label>Formating sdb1</label>
<input>???</input>
<action type="exit">Ready</action>
</progressbar>
<button>
<label>Update Progressbar (usb_w)</label>
<action>usb_w</action>
</button>

</frame>
<hbox>
<button cancel></button>
</hbox>
</vbox>
'

gtkdialog --program=MAIN_DIALOG

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#4 Post by misko_2083 »

johnmcenroy wrote:Thank you very much misko_2083. And can progressbar be updated by pushing button to activate function usb__w() ?
Button > usb_w > progressbar. Thanks.
Yes

Code: Select all

#!/bin/bash

export temp_file="$(mktemp -u --tmpdir formatusb.XXXXXXXX)"
mkfifo "$temp_file"

trap "rm -f $temp_file" EXIT

umount /dev/sdb1

function format32()
{
strace -q -ewrite mkfs.vfat -F32 -n USB -v /dev/sdb1 2>&1 | awk '{
        count += $NF
            if (count % 10 == 0) {
               percent = count / total_size * 100
               printf "%3d\n", percent
               printf "%s %3d%%\n", msg, percent
            }
         }
         END { print "" }' total_size=$(fdisk -l /dev/sdb1 | awk '/Disk.*sectors/{print $7}') count=0 msg="Formating..." \
        | grep -v "^$" # skip empty lines

if [[ "${PIPESTATUS[0]}" -ne 0 ]]
 then   
   echo Error
 else
    echo 100
    echo Done
fi
}
export -f format32

export MAIN_DIALOG='
<vbox>
   <frame Progress>
      <text>
          <label>Formating /dev/sdb1</label>
      </text>
      <progressbar>
         <label>Ready</label>
         <input>bash -c "tail -f -n +1 $temp_file 2>/dev/null" &</input>
         <action type="refresh">Ready</action>
      </progressbar>
      <button>
          <label>Format /dev/sdb1</label>
          <action>bash -c "format32 2>&1 > $temp_file" &</action>
      </button>
   </frame>
<hbox>
   <button ok></button>
</hbox>
</vbox>
'

3<>$temp_file

gtkdialog --program=MAIN_DIALOG

exec 3>&-

Post Reply