import time


def shellTitle(shell, moreinfo=False):
    """Given a shell instance, build the text title to represent it."""

    # Get name
    nameText = shell._info.name

    # Build version text
    if shell._version:
        versionText = "v{}".format(shell._version)
    else:
        versionText = "v?"

    # Build gui text
    guiText = shell._startup_info.get("gui")
    guiText = guiText or ""
    if guiText.lower() in ["none", ""]:
        guiText = "without gui"
    else:
        guiText = "with " + guiText + " gui"

    # Build state text
    stateText = shell._state or ""

    # Build text for elapsed time
    elapsed = time.time() - shell._start_time
    hh = elapsed // 3600
    mm = (elapsed - hh * 3600) // 60
    ss = elapsed - hh * 3600 - mm * 60
    runtimeText = "runtime: %i:%02i:%02i" % (hh, mm, ss)

    # Build text
    if not moreinfo:
        text = nameText
    else:
        text = "'%s' (%s %s) - %s, %s" % (
            nameText,
            versionText,
            guiText,
            stateText,
            runtimeText,
        )

    # Done
    return text