-- |This module provides a ''progress bar'' widget which stores a-- progress value between 0 and 100 inclusive and supports a text-- label. Use the 'schedule' function to modify the progress bar's-- state from a thread.moduleGraphics.Vty.Widgets.ProgressBar(ProgressBar,newProgressBar,setProgress,setProgressTextAlignment,setProgressText,addProgress,getProgress,onProgressChange)whereimportControl.MonadimportGraphics.VtyimportGraphics.Vty.Widgets.CoreimportGraphics.Vty.Widgets.EventsimportGraphics.Vty.Widgets.TextimportGraphics.Vty.Widgets.AlignmentdataProgressBar=ProgressBar{progressBarAmount::Int,onChangeHandlers::HandlersInt,progressBarText::String,progressBarTextAlignment::Alignment}instanceShowProgressBarwhereshowp=concat["ProgressBar { ",", "++(show$progressBarAmountp),", ... }"]-- |Create a new progress bar with the specified completed and-- uncompleted attributes, respectively. The foreground of the-- attributes will be used to show the progress bar's label, if any.newProgressBar::Attr->Attr->IO(WidgetProgressBar)newProgressBarcompleteAttrincompleteAttr=dochs<-newHandlerst<-plainText""letinitSt=ProgressBar0chs""AlignCenterwRef<-newWidgetinitSt$\w->w{growHorizontal_=const$returnTrue,render_=\thissizectx->do-- Divide the available width according to the-- progress valueprog<-progressBarAmount<~~thistxt<-progressBarText<~~thisal<-progressBarTextAlignment<~~thisletcomplete_width=fromEnum$(toRationalprog/toRational(100.0::Double))*(toRational$fromEnum$region_widthsize)full_width=fromEnum$region_widthsizefull_str=takefull_width$mkStrtxtalmkStrsAlignLeft=s++replicate(full_width-lengthtxt)' 'mkStrsAlignRight=replicate(full_width-lengthtxt)' '++smkStrsAlignCenter=concat[half,s,half,iflengthhalf*2<(full_width+lengthtxt)then" "else""]wherehalf=replicate((full_width-lengthtxt)`div`2)' '(complete_str,incomplete_str)=(takecomplete_widthfull_str,dropcomplete_widthfull_str)setTextWithAttrst[(complete_str,completeAttr),(incomplete_str,incompleteAttr)]rendertsizectx}setProgresswRef0returnwRef-- |Register a handler to be invoked when the progress bar's progress-- value changes. The handler will be passed the new progress value.onProgressChange::WidgetProgressBar->(Int->IO())->IO()onProgressChange=addHandler(onChangeHandlers<~~)-- |Set the progress bar's progress value. Values outside the allowed-- range will be ignored.setProgress::WidgetProgressBar->Int->IO()setProgresspamt=when(amt>=0&&amt<=100)$doupdateWidgetStatep$\st->st{progressBarAmount=amt}fireEventp(onChangeHandlers<~~)amt-- |Set the progress bar's text label alignment.setProgressTextAlignment::WidgetProgressBar->Alignment->IO()setProgressTextAlignmentpal=updateWidgetStatep$\st->st{progressBarTextAlignment=al}-- |Set the progress bar's text label.setProgressText::WidgetProgressBar->String->IO()setProgressTextps=updateWidgetStatep$\st->st{progressBarText=s}-- |Get the progress bar's current progress value.getProgress::WidgetProgressBar->IOIntgetProgress=(progressBarAmount<~~)-- |Add a delta value to the progress bar's current value.addProgress::WidgetProgressBar->Int->IO()addProgresspamt=docur<-getProgresspletnewAmt=cur+amtwhen(newAmt>=0&&newAmt<=100)$setProgresspnewAmt