如何创建带有多个进度条的wget GUI?


21

我正在编写自己的脚本以使用来下载多个项目(同时,每个命令一个项目)wget,我需要一个进度条。我目前使用zenity --progress进度条,但问题是下载时会产生多个进度条。

我要实现的是类似于Nautilus中的文件传输对话框的内容:

Ubuntu的文件传输对话框

任何人都可以针对要实现此目标的程序或语言为我指明正确的方向。

注意: 我仅具有shell脚本的基本知识,而没有其他知识。


你试过了axel-kapt吗?sudo apt-get install axel-kapt

赏金已经接近尾声了。如果您对我的回答感到满意,请不要忘记分配它。
谷氨酰胺

没问题,伙计。祝您脚本工作顺利!
谷氨酰胺

Answers:


28

使用YAD的解决方案


总览

Zenity已经有一段时间没有更新了。幸运的是,有一个名为YAD的fork ,具有许多附加功能。它尚未到达官方存储库,但是您可以从webupd8 PPA安装它:

sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install yad

添加到YAD的功能之一是多​​进度条对话框。以下是YAD手册中有关多个进度条的摘录:

Multi progress bars dialog options
--bar=LABEL[:TYPE]
  Add progress bar. LABEL is a text label for progress bar. TYPE is a progress bar type. Types are:
  NORM for normal progress bar, RTL for inverted progress bar and PULSE for pulsate progress bar.
--vertical
  Set vertical orientation of progress bars.
--align=TYPE
  Set alignment of bar labels. Possible types are left, center or right. Default is left.

Initial values for bars set as extra arguments. Each lines with progress data passed to stdin must be started
from N: where N is a number of progress bar. Rest is the same as in progress dialog.

因此,具有多个进度条的非常简单的脚本可能如下所示:

for i in {1..100}; do
   printf "1:$i\n2:$i\n3:$i\n"
   sleep 0.2
done | yad --multi-progress --bar="Bar 1":NORM --bar="Bar 2":NORM --bar="Bar 3":NORM

结果如下所示:

简单的Yad多进度脚本


我们可以通过以下方式向栏添加描述#

for i in {1..100}; do
   printf "1:$i\n2:$i\n3:$i\n"
   printf "1:#Progress 1 is at $i percent\n2:#Progress 2 is at $i percent\n3:#Progress 3 is at $i percent\n"
   sleep 0.2
done | yad --multi-progress --bar="Bar 1":NORM --bar="Bar 2":NORM --bar="Bar 3":NORM

结果:

在此处输入图片说明


如果要在wget下载脚本中实现此功能,则必须首先做一些事情:

  • 找到一种从wget输出中提取下载进度和其他有趣数据的方法
  • 找到一种遍历多个文件的方法
  • 找到一种方法将所有我们需要的信息传递给一个Yad对话框

我发现这个项目非常有趣,所以我坐下来编写了一个脚本,该脚本可以完成上述所有工作。


yad_wget

这是我想出的:

#!/bin/bash

# NAME:         yad_wget
# VERSION:      0.2
# AUTHOR:       (c) 2014 Glutanimate
# DESCRIPTION:  graphical frontend to wget in form of a yad script
# FEATURES:     - display progress of multiple simultaneous downloads
#               - set maximum number of simultaneous downloads
# DEPENDENCIES: yad
#
#               Install yad on Ubuntu with:
#
#                   sudo add-apt-repository ppa:webupd8team/y-ppa-manager
#                   sudo apt-get update
#                   sudo apt-get install yad
#
# LICENSE:      GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)
#
# NOTICE:       THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 
#               EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 
#               PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR 
#               IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
#               AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND 
#               PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
#               YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
#
#               IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY 
#               COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS 
#               PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, 
#               INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE 
#               THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED 
#               INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE 
#               PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER 
#               PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#
# USAGE:        yad_wget <space-delimited URLs>
#               Closing the yad dialog will terminate all downloads in progress

# Variables and settings

MAXDLS="5" # set maximum number of simultaneous downloads

URILIST="$@" # gets list of URIs from stdin
USAGE="$0 <space-delimited URLs>"

# Set GUI variables up
TITLE="YAD wget downloader"                 # dialog title
TEXT="<b>Downloads</b> in progress:"        # dialog text
ICON="emblem-downloads"                     # window icon (appears in launcher)
IMAGE="browser-download"                    # window image (appears in dialog)

#URILIST="http://proof.ovh.net/files/100Mb.dat http://speedtest.wdc01.softlayer.com/downloads/test10.zip http://cachefly.cachefly.net/100mb.test"

# Usage checks

if [[ -z "$URILIST" ]]
  then
      echo "Error: No arguments provided"
      echo "Usage: $USAGE"
      exit 1
fi


# download file and extract progress, speed and ETA from wget
# we use sed regex for this
# source: http://ubuntuforums.org/showthread.php?t=306515&page=2&p=7455412#post7455412
# modified to work with different locales and decimal point conventions
download(){
    wget  "$1" 2>&1 | sed -u \
    "s/.* \([0-9]\+%\)\ \+\([0-9,.]\+.\) \(.*\)/$2:\1\n$2:# Downloading at \2\/s, ETA \3/"
    RET_WGET="${PIPESTATUS[0]}"             # get return code of wget
    if [[ "$RET_WGET" = 0 ]]                # check return code for errors
      then
          echo "$2:100%"
          echo "$2:#Download completed."
      else
          echo "$2:#Download error."
    fi
}


# compose list of bars for yad
for URI in $URILIST; do                     # iterate through all URIs
    FILENAME="${URI##*/}"                   # extract last field of URI as filename
    YADBARS="$YADBARS --bar=$FILENAME:NORM" # add filename to the list of URIs
done

IFS=" "
COUNTER="1"
DYNAMIC_COUNTER="1"

# main
# iterate through all URIs, download them in the background and 
# pipe all output simultaneously to yad
# source: http://pastebin.com/yBL2wjaY

for URI in $URILIST; do
    if [[ "$DYNAMIC_COUNTER" = "$MAXDLS" ]] # only download n files at a time
      then
          download "$URI" "$COUNTER"        # if limit reached wait until wget complete
          DYNAMIC_COUNTER="1"               # before proceeding (by not sending download() to bg)
      else
          download "$URI" "$COUNTER" &      # pass URI and URI number to download()
          DYNAMIC_COUNTER="$[$DYNAMIC_COUNTER+1]"
    fi
    COUNTER="$[$COUNTER+1]"                 # increment counter
done | yad --multi-progress --auto-kill $YADBARS --title "$TITLE" \
--text "$TEXT" --window-icon "$ICON" --image "$IMAGE"

# ↑ launch yad multi progress-bar window

这是它的样子:

在此处输入图片说明

           在此处输入图片说明

请务必通读所有注释以了解脚本的工作原理,如果您有任何疑问,请随时在下面的注释中提问。


编辑:

我增加了对设置最大同时下载数量的支持。例如MAXDLS="5"

在此处输入图片说明


这是一篇非常全面且有趣的文章,我喜欢到目前为止所看到的。感谢您抽出宝贵的时间来完善我的这个小项目。:)问题1:完成的文件的进度条会如何处理?他们是否会一直停留在对话框上,直到其他所有事情完成?Q2:从我所看到的,没有办法从对话框中取消单个操作,是吗?
Rey Leonard Amorato 2014年

1
为+1 yad;这是一个很棒的包装。要知道,--notificationyad不会在Ubuntu 13.04和以后的工作,因为错误1305835错误1119420(请按以下的错误链接并表决)。
帕迪·兰道

2
要拥有其他控件,您需要升级到GtkDialog,它可以执行几乎所有操作。但是,它涉及的更多。该PuppyLinux社会广泛使用它。
DocSalvager 2014年

1
@Glutanimate-避免混淆的简短说明:PuppyLinux论坛上的许多参考都引用了“ gtkdialog3”和“ gtkdialog4”。这些只是真实GtkDialog版本的简单同义词,甚至不一致。您抓到的0.8.3版本是最新的,并且非常稳定。(主持人:此处不再提供有关GtkDialog的更多信息。对此感兴趣的人请发布新问题。)
DocSalvager 2014年

1
@Glutanimate我已经使用yad了几个月,它的功能给我留下了深刻的印象。但是,您的回答绝对比我自己的进度条辉煌和光明。我会很快将您的wget进度条码改编成我自己的应用程序:askubuntu.com/questions/900319/…
WinEunuuchs2Unix
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.