def format_progressbar(progress, width):
    """
    Returns a string of a progress bar. This code has been copied from :py:meth:`deluge's format_progressbar <deluge.ui.console.utils.format_utils.format_progress>`.

    :param float progress: a value between 0-100.

    :returns: str, a progress bar based on width.
    :rtype: str

    **Usage**
    
    >>> format_progressbar( 87.6, 100 )
    '[######################################################################################~~~~~~~~~~~~]'
    
    """
    w = width - 2 # we use a [] for the beginning and end
    s = "["
    p = int(round((progress/100) * w))
    s += "#" * p
    s += "~" * (w - p)
    s += "]"
    return s